CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2009
    Location
    Somewhere in Asia
    Posts
    36

    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?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: For Loop Statement Pattern to use in Parser

    Considering that this is a valid for loop:
    Code:
    for (;;) {}
    I don't think the above is quite right, no.

  3. #3
    Join Date
    Jun 2009
    Location
    Somewhere in Asia
    Posts
    36

    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.

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

  5. #5
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    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

  6. #6
    Join Date
    Jun 2009
    Location
    Somewhere in Asia
    Posts
    36

    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.

  7. #7
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880

    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

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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
  •  





Click Here to Expand Forum to Full Width

Featured