[RESOLVED] parsing conditional statements
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] parsing conditional statements

  1. #1
    Join Date
    Nov 2009
    Posts
    11

    [RESOLVED] parsing conditional statements

    I need to write a program that will input a series of 'if else statements' and the code needs to validate the statements based on grammar rules defined.
    Now I realise this program is easy and maybe I am overthinking stuff but I just want to verify my strategy before I start.

    Basically I figure that I need to define the rules according to the tokens; then input the statements as string; break it into tokens; test each token to see if it is a valid token; and then check the syntax of the statement and if it is in the format of the main rule which defines an if statement.

    Am I correct? This was assigned and it seemed to sounded too easy initially.

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Re: parsing conditional statements

    It seems you are not using Lex and Yacc, and are trying a simple recursive approach, which is also what I have done several times.

    Beware that some grammar rules are easier to implement than others. The worse (and funniest) I have seen is the syntax used by Cobol where you can have "if foo > 10 and < 20" (instead of "if foo > 10 and foo < 20").

    Also, don't forget priorities of operators, e.g. if (a > 10 + 3 * 2) is different from if (a > 3 + 10 * 2). Multiplications have to be performed before additions.

    Something funny is the minus sign, because it can have two meanings: it is either a subtraction of two operators, either a negative number, e.g. you can if (a > -3), or if (a > b - 3), but not if (a > b - -3). you can even find a minus sign is the name of variables, e.g. if (client-id > 3) which is about the client-id variable, not about a subtraction of the client variable and the id variable. The star is also funny in C, because it can be used for a multiplication or for a pointer, e.g. if (a > b * c), (if a > *b), (if a > b * *c), etc.

    Good luck to you.
    Last edited by olivthill2; November 25th, 2009 at 04:08 AM.

  3. #3
    Join Date
    Nov 2009
    Posts
    11

    Re: [RESOLVED] parsing conditional statements

    thanks

  4. #4
    Join Date
    Nov 2009
    Posts
    11

    Re: [RESOLVED] parsing conditional statements

    Sorry to mark it resolved already; but I started coding it and it seems to be a rather tedious way of doing it. I read in the string (if statement) and break it into tokens and then validate each one. Now to check if it is a valid IF statement, how can I hard code this rule.
    Is there any suggestion how to do this?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center