Question

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...

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 )

Solutions

Expert Solution

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

  • expression: expression '+' term
  • term '+' term
  • factor '+' term
  • identifier '+' term
  • a '+' term
  • a '+' term '*' factor
  • a '+' factor '*' factor
  • a '+' identifier '*' factor
  • a '+' b '*' '(' expression ')'
  •   a '+' b '*' '(' expression '-' term ')'
  •   a '+' b '*' '(' expression '-' term ')'
  • a '+' b '*' '(' term '-' term ')'
  •   a '+' b '*' '(' factor '-' term ')'
  •    a '+' b '*' '(' identifier '-' term ')'
  •    a '+' b '*' '(' c '-' term ')'
  • a '+' b '*' '(' c '-' factor ')'
  •    a '+' b '*' '(' c '-' constant ')'
  •    a '+' b '*' '(' c '-' 4 ')'

Related Solutions

For the following infix expression, build the corresponding expression tree. 1.1 a*b 1.2 a+b*c 1.3 a+b*c/d-e...
For the following infix expression, build the corresponding expression tree. 1.1 a*b 1.2 a+b*c 1.3 a+b*c/d-e Perform pre-order and post-order traversal of the above binary expression trees. What relationship exists among these scans and prefix and postfix notation for the expression?
Give a context-free grammar for {w ∈ {b, c}∗| #c(w) = #b(w)}.
Give a context-free grammar for {w ∈ {b, c}∗| #c(w) = #b(w)}.
C++ : Find the syntax errors in the following program. For each syntax error, fix it...
C++ : Find the syntax errors in the following program. For each syntax error, fix it and add a comment at the end of the line explaining what the error was. #include <iostream> #include <cmath> using namespace std; int main(){ Double a, b, c; 2=b; cout<<"Enter length of hypotenuse"<<endl; cin>>c>>endl; cout>>"Enter length of a side"<<endl; cin>>a; double intermediate = pow(c, 2)-pow(a, 2); b = sqrt(intermediate); cout<<"Length of other side is:" b<<endline; return 0; }
For the following exercises, compute the value of the expression. C(12, 4)
For the following exercises, compute the value of the expression.C(12, 4)
#1 We are given the grammar rules A ➝ F B E B ➝ A C...
#1 We are given the grammar rules A ➝ F B E B ➝ A C These rules are only some of the rules of a larger grammar G, but we are not given the remaining rules of G. We are told that A is the start symbol of G and that the following holds: {ε, c, d} ⊆ FIRST(C) {ε, e} ⊆ FIRST(E) {ε, f, g} ⊆ FIRST(F) Recall that end of file is denoted EOF. The symbol ⊆...
4. According to the Environmental Kuznets Curves, what’s is the relationship between income and pollution?
4. According to the Environmental Kuznets Curves, what’s is the relationship between income and pollution?
Consider the following program written in C syntax: void swap(int a, int b) { int temp;...
Consider the following program written in C syntax: void swap(int a, int b) { int temp; temp = a; a = b; b = temp;} void main() { int value = 2, list[5] = {1, 3, 5, 7, 9}; swap(value, list[0]); swap(list[0], list[1]); swap(value, list[value]); } For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three calls to swap? Passed by value Passed by reference Passed...
Simplify the grammar G. Does L(G) contain ε ? S -> A B C | B...
Simplify the grammar G. Does L(G) contain ε ? S -> A B C | B a B A -> a A | B a C | a a a B -> b B b | a | D C -> C A | A C D -> ε
Draw the binary tree representation of the following arithmetic expression: "( ( ( 6 + 3...
Draw the binary tree representation of the following arithmetic expression: "( ( ( 6 + 3 ) * ( 3 - 2 ) ) / ( ( 3 + 10 ) + ( ( 8 - 3 ) - 2 ) ) * 9 )" -Give an O(n)-time algorithm for computing the depth of all positions of a tree T, where n is the number of nodes of T.
1.Write a new expression similar to the following :     E1= a+b*c+(d*e+f)*g a)Convert your expression to...
1.Write a new expression similar to the following :     E1= a+b*c+(d*e+f)*g a)Convert your expression to postfix form. b) Trace the algorithm Postfix on slide 38 using your expression. 2.Write a new expression similar to the following :    E2= 6*((5+(2+3)*8)+3) Convert your expression to postfix form. Trace EvalPostfix algorithm on slide 53 using your expression. 3.Write a program that receives some input values and     a ) Displays these values by pointers, c)Displays their memory addresses.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT