- #1
acadian
- 4
- 0
I'm looking to write a grammar in Backus-Naur form for postfix addition and subtraction expressions involving the variables A,B,C
Given the infix grammar:
<expr> ::= <expr> + <term> | <expr> - <term> | <term>
<term> ::= <term> * <factor> | <term> / <factor> | <factor>
<factor> ::= (<expr>) | <id>
<id> ::= A|B|C
I assume that I need to create a parse tree, first, using the above grammar for an infix expression such as A + B + C and than traverse the parse tree in reverse order and read out the postfix version to form a postfix grammar?
Would this be a valid way to go about this?
Given the infix grammar:
<expr> ::= <expr> + <term> | <expr> - <term> | <term>
<term> ::= <term> * <factor> | <term> / <factor> | <factor>
<factor> ::= (<expr>) | <id>
<id> ::= A|B|C
I assume that I need to create a parse tree, first, using the above grammar for an infix expression such as A + B + C and than traverse the parse tree in reverse order and read out the postfix version to form a postfix grammar?
Would this be a valid way to go about this?