1
$\begingroup$

I have the following basic BNF grammar:

<A>   -->  <id>  = <E>
<id>  -->  A | B | C | D | F
<E> --> <E> + <T> | <T>
<T> --> <T> * <F> | <F>
<F> --> <E> | <id>

Operator (*) should have higher precedence over (+). However, see this statement:

A = A + B + C + D * F

The leftmost derivation (and parse tree) indicate the opposite! i.e., (+) has higher precedence than (*) since it will show up lower in the parse tree!

What am I missing here?

$\endgroup$
1
  • $\begingroup$ Normally the last production would be <F> --> ( <E> ) | <id> in order to allow parenthesised expressions. As written, your grammar is highly ambiguous. $\endgroup$
    – rici
    Commented Jul 2, 2018 at 15:06

1 Answer 1

2
$\begingroup$

Due to the path

<E>  ->  <T>  ->  <F>  ->  <id>,

you can completely circumvent the "check" for whether there are *.

Think about it in a bottom up manner: only if there's no * nearby can you parse a +.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.