views:

33

answers:

1

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> ::= * | /
+4  A: 

Try This:

<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> )

This worked for me.

Michael Eakins
Is this answer acceptable?
Michael Eakins