I'm trying to write a parser for the c language which will be able to take care of expressions, assignments, if-else and while loops.
here are my rules :
expression -> expression op expression
expression -> ID
expression -> NUMBER
statement -> ID ASSIGN expression
statement -> IF expression THEN statement
statement -> WHILE expression THEN statement
Everything in caps is a token(terminal symbol)
When parsing the string "while h<=0 then t=1", it seems to consider "h" as an expression (using the rule expression->ID). So, the expression in "WHILE expression THEN statement" becomes "h". Obviously, I would want it to consider "h<=0" as the expression (using the rules expression -> expression op expression). How do I make sure that this happens?
Building on the previous post where you were asking about the ply.lex module, the snippet below seems like a partial implementation of a c-like grammar. I haven't used ply much, but one of the tricks seems to be that you need to define the grammar rules in the correct order.
Output from the snippet is: