mardi 17 mars 2015

Variables not updating in recursive descent parser


I'm writing a recursive descent parser that recognizes this language:



<expr> ::= <term> | <term> "+" <expr> | <variable> "=" <expr>
<term> ::= <factor> | <term> "*" <factor>
<factor> ::= <constant> | <variable> | "(" <expr> ")"
<variable> ::= "x" | "y" | "z"
<constant> ::= <digit> | <digit> <constant>
<digit> ::= "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"


I'm assigning a variable a value works, but when I try to assign a variable to an expression containing a variable, it assigns the value 0 to both variables.


For example, I have an input expression y=3+3, which does indeed evaluate to y=6. If I try to write x=y+1, both x and y go to 0. I have a std::map<char, double> vars = {{'x', 0}, {'y', 0}, {'z', 0}};


My parse tree for x=y+1 should be



<expr>
/ | \
<var> '=' <expr>
/ / | \
vars['x'] <term> '+' <expr>
| |
<fact> <term>
| |
<var> <fact>
| |
vars['y'] <cons>
|
<digit>
|
1


To save from posting a lot of code, it can be found on my GitHub.


If needed, I can post my code, but it's over 300 lines of code. How can I fix this assignment?


Thanks, erip




Aucun commentaire:

Enregistrer un commentaire