- E -> E + T | T.
- T -> T * F | F.
- F -> x.
A derivation for “x + x * x” would be (unless I’m wrong): E => E + T => T + T => F + T => x + T => x + T * F => x + F * F => x + x * F => x + x * x. So how is multiplication given priority over addition? It appears that the addition is produced first.
Asked By : Xpl0
Answered By : babou
E / | E + T | /| T T * F | | | F F x | | x x
And the AST for x + x * x is
+ / x * / x x
It is derivable from the parse tree by moving all operators up one edge on the preceding node, and then ignoring all non-terminals, making each linear downward path a single edge. Essentially the same shape. This AST is the expression tree defined by the priorities, from which the meaning of the expression can be derived (by homomorphism), from which it can be evaluated or compiled.
Best Answer from StackOverflow
Question Source : http://cs.stackexchange.com/questions/33929 Ask a Question Download Related Notes/Documents