Grammar with rounding

I have this grammar and need to modify it so that brackets like: (-1) and - (1 * 5) are possible 1+ (2 * 5), as well as a unary unary minus sign.

Does anyone have any suggestions on how to do this?

<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 to 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> ::= * | / 
+4
source share
1 answer

Try the following:

 <expr> ::= <term> | <term> <op1> <expr> <term> ::= <unary> | <term> <op2> <unary> <unary> ::= <value> | <op1> <value> <darg> ::= <digit> | <darg> <digit> <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 <op1> ::= + | - <op2> ::= * | / <value> ::= <darg> | ( <expr> ) 

It worked for me.

+4
source

All Articles