-
July 28th, 2009, 08:38 AM
#1
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?
-
July 28th, 2009, 09:01 AM
#2
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.
-
July 28th, 2009, 09:34 AM
#3
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.
-
July 28th, 2009, 09:38 AM
#4
Re: For Loop Statement Pattern to use in Parser
especially if i want an indefinite number of whitespaces in the syntax.
You can remove the whitespaces before you start parsing.
-
July 28th, 2009, 09:54 AM
#5
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!
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
July 28th, 2009, 10:08 AM
#6
Re: For Loop Statement Pattern to use in Parser
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.
-
July 28th, 2009, 10:24 AM
#7
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.
Elrond
A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
-- George Steiner
-
July 28th, 2009, 10:38 AM
#8
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|