For Loop Statement Pattern to use in Parser
Hello, I'm creating a program that parses a for loop statement, i can do all other things that were need for a parser, now my problem is the pattern to check if the for loop statement that the user entered is correct.
Here is the pattern:
Code:
public static string forLoopPattern =
@"([for])" +
@"([(])" +
@"([int]\ \\)" +
@"([a-z])" +
@"([=])" +
@"([0-9])" +
@"([;])" +
@"([a-z])" +
@"([>,<,=,<=,>=,=<,=>])" +
@"([0-9])" +
@"([;])" +
@"([a-z])" +
@"([++,--])" +
@"([)])";
Is the pattern correct? If not what should i change in the pattern and how will i be able to check it if that pattern is working or correct?
Re: For Loop Statement Pattern to use in Parser
Considering that this is a valid for loop:
I don't think the above is quite right, no.
Re: For Loop Statement Pattern to use in Parser
In that case i should change that pattern. Just to make things clear, we were instructed that we can only have one statement so we will not need to include the curly braces for a block statement. So we can easily see how a parser or compiler works.
Should I include the symbols like '^', '?', '*' so i can satisfy other syntax? My only problem with that is i don't know how to use that symbols. I'm a bit confused on how to use it since i've been reading different articles in the web about creating patterns. can someone help me about that too?
especially if i want an indefinite number of whitespaces in the syntax.
for example:
Code:
for ( int x = 0 ; x < 5 ; x++ ) cout<<Hello World;
I want to satisfy that kind of syntax, just like a normal compiler would do. My problem is i don't know where to put \s, '*', '^' etc to make it work.
Re: For Loop Statement Pattern to use in Parser
Quote:
especially if i want an indefinite number of whitespaces in the syntax.
You can remove the whitespaces before you start parsing.
Re: For Loop Statement Pattern to use in Parser
I'm no expert on regular expressions, but I believe your pattern would also not allow
Code:
for (int myint5 = x2; myint5 < abc4; myint5++);
The reason I say that is the int variable contains a number (which doesn't appear to be allowed, the initial value of myint5 is set to a variable, and the limit is also a variable name (not just a number).
I'm not sure if your parser is being designed to allow all of this, or if it's just supposed to parse a basic for loop.
Good luck with this - looks interesting!
Re: For Loop Statement Pattern to use in Parser
Quote:
I'm not sure if your parser is being designed to allow all of this, or if it's just supposed to parse a basic for loop.
The answer to that is its just intended to parse a basic for loop...that's why all of my examples are very simple...hehe....the reason why we're creating a parser-like program is to apply one of the data structures we discussed and in that case, for our program we were required to apply one of many applications of trees, the Parse Tree to be exact.
Re: For Loop Statement Pattern to use in Parser
Probably, the thing you should focus on is that the basic for loop is:
Code:
for ( [some expression]; [some expression]; [some expression] ) [some expression]
You have to find out what is and is not allowed within each expression. For example, within the "for( )", you can and should have only 2 semi columns, which are the nice way to figure out how to separate you expression. So the internal expressions should not allow semi columns.
Re: For Loop Statement Pattern to use in Parser
semicolons, but yeah. Also, the second expression must be convertible to bool.
You could then parse each of the expressions independently, or simply output them as strings if that's good enough to satisfy the assignment.