Is it correct to state that the below grammar can handle the expression: a - b * c, by the below two different parse trees.
Grammar:
Expression = Expression "-"
Expression | Expression "*"
Expression | Factor
Factor = "a" | "b" | "c"
Parse tree #1:
Expression -> Expression - Expression
=> Factor - Expression
=> a - Expression
=> a - Expression * Expression
=> a - Factor * Expression
=> a - b * Factor
=> a - b * c
Parse tree #2:
Expression -> Expression * Expression
=> Expression - Expression * Expression
=> Factor - Expression * Expression
=> a - Expression * Expression
=> a - Factor * Expression
=> a - b * Factor
=> a - b * c
I feel that the second derivation above is completely wrong, as not considers the left-to-right scan of the input string. The decision should have been based on the lookahead symbol only.
If possible show by a C language code snippet, how the implementation of the second derivation, is correct, as the book titled: Compliers and Compiler Generators An Introduction With C++, by Patrick D. Terry, shows the above example, in Section 6.4, page #131
For me, the ambiguous grammar can only be shown by the below second derivation:
Parse tree #2:
Expression -> Expression - Expression
=> Expression - Expression * Expression
=> Factor - Expression * Expression
=> a - Factor * Expression
=> a - b * Expression
=> a - b * Factor
=> a - b * c