How can I add parentheses as the highest level of precedence in a simple grammar?
Asked Answered
B

2

2

I'm trying to add 2 things to my grammar:

  1. Unary minus sign, i.e. '-', and

  2. Parentheses

Here's my grammar so far:

<comp>  ::= <expr> | <comp> <op0> <expr>
<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
<op0>   ::= > | < | =< | => | =
<op1>   ::= + | -
<op2>   ::= * | /

I've tried everything and can't figure this out. How can I make the unary minus sign be at the highest level of precedence, followed by parentheses next and then the remaining operators as they are described?

Bondswoman answered 28/1, 2013 at 2:45 Comment(0)
F
1

I am adding a new variable named <new> with three new production rules in your present grammar in question to add Unary minus sign and Parentheses:

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

by adding Parentheses, you are adding tow new terminals in your grammar { (, ) }

Also, you can add <new> ::= ( <new> ) if you want to generate (-7), (7) and ((6+7)) like expressions.(these are valid expressions)

I would like to inform you that if you are writing compiler, use ambiguous grammar instead and add operator precedence in YACC tool that will allow efficient parsing

EDIT:

If you wants to add expression like -(7) and that is a valid expression. So <new> ::= -<new> instead of <new> ::= <drag>

Felicitation answered 28/1, 2013 at 19:0 Comment(4)
Let me know if you don't understand why I added new variable. I hope it would helpfulFelicitation
I wasn't sure where to place my new non-terminal. What you did makes sense, thanks!Bondswoman
Shouldn't the second alternative for <new> be -<new> so that a parenthesized expression can be negated?Overmeasure
@Overmeasure Yes, if you wants to add expression like -(7) and that is a valid expression. So <new> ::= -<new> instead of <new> ::= <drag> that goodFelicitation
D
2

Change <term> to use <fred> instead of <darg> and define

<fred> ::= -<fred> | (<comp>) | <darg>
Dependency answered 28/1, 2013 at 3:3 Comment(0)
F
1

I am adding a new variable named <new> with three new production rules in your present grammar in question to add Unary minus sign and Parentheses:

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

by adding Parentheses, you are adding tow new terminals in your grammar { (, ) }

Also, you can add <new> ::= ( <new> ) if you want to generate (-7), (7) and ((6+7)) like expressions.(these are valid expressions)

I would like to inform you that if you are writing compiler, use ambiguous grammar instead and add operator precedence in YACC tool that will allow efficient parsing

EDIT:

If you wants to add expression like -(7) and that is a valid expression. So <new> ::= -<new> instead of <new> ::= <drag>

Felicitation answered 28/1, 2013 at 19:0 Comment(4)
Let me know if you don't understand why I added new variable. I hope it would helpfulFelicitation
I wasn't sure where to place my new non-terminal. What you did makes sense, thanks!Bondswoman
Shouldn't the second alternative for <new> be -<new> so that a parenthesized expression can be negated?Overmeasure
@Overmeasure Yes, if you wants to add expression like -(7) and that is a valid expression. So <new> ::= -<new> instead of <new> ::= <drag> that goodFelicitation

© 2022 - 2024 — McMap. All rights reserved.