I have this grammar and need to modify it to allow parenthesis like: (-1) and -(1*5) possibly 1+(2*5) as well as unary the unary minus sign.
Does anyone have any suggestions of how to do so?
<expr> ::= <term> | <expr> <op1> <term>
<term> ::= <darg> |<term> <op2> <darg>
<darg> ::= <digit> | <darg> <digit>
<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<op1> ::= + | -
<op2> ::= * | /
It seems like it would be something like this:
<expr> ::= <term> | <expr> <op1> <term>
<term> ::= <unary> |<term> <op2> <unary>
<unary> ::= <darg> | -<darg> | -<unary><darg>
<darg> ::= <digit> | <darg> <digit> | <paren>
<paren> ::= (<expr>)
<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<op1> ::= + | -
<op2> ::= * | /