In: Computer Science
What's the AST(Abstract Syntax Tree) for the expression a+b*c-4 according to the following grammar, and what’s left most derivation of this expression?
expression → expression ‘+’ term | expression ‘-’ term | term
term → term ‘*’ factor | term ‘/’ factor | factor
factor → identifier | constant | ‘(‘ expression ‘)’
Given Information
expression → expression ‘+’ term | expression ‘-’ term | term
term → term ‘*’ factor | term ‘/’ factor | factor
factor → identifier | constant | ‘(‘ expression ‘)’
Abstract Syntax Tree: Each node represents an operator and children of the operator node represent operands.
All the unimportant details are neglected. We will directly draw a tree neglecting grammar rules.
a+b*c-4 is the given expression. Here * has precedence over + and -
+ and - are left-associative as they are right of expression or start symbol
Order of Evaluation
b*c
a+b*c
a+b*c-4
Here is the abstract tree

Left Most Derivation of a+b*c-4