Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
786 views
in Technique[技术] by (71.8m points)

parsing - How can I add parentheses as the highest level of precedence in a simple grammar?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

<fred> ::= -<fred> | (<comp>) | <darg>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...