The questions in this section are based on the grammar given as the following:
prog -> assign | expr
assign -> id = expr
expr -> expr + term | expr - term | term
term -> factor | factor * term
factor -> ( expr ) | id | num
id -> A | B | C
num -> 0 | 1 | 2 | 3
(2a) What is the associativity of the * operator? (5 points)
(2b) What is the associativity of the + operator? (5 points)
(2c) For the * and + operators, do they have the same precedence, does the * operator have greater precedence than +, or does + have greater precedence than * ? (5 points)
(2e) Modify the grammar to add a new operator as it follows. (10
points)
A unary minus operator (-) that has precedence higher than any of
*, +, or binary minus.
(2d) Using this grammar show a leftmost derivation and a parse tree for the strings in d.1 (10 points) Show the parse tree as a graphical tree. We've done the first one (Example) for you as an example of the
desired output format. (Example) 1 + 2
2
prog => expr |
(2d) C = (1 + A) * B
The questions in this section are based on the grammar given as the following: prog ->...
The questions in this section are based on the grammar given as the following: prog -> assign | expr assign -> id = expr expr -> expr + term | expr - term | term term -> factor | factor * term factor -> ( expr ) | id | num id -> A | B | C num -> 0 | 1 | 2 | 3 (2a) What is the associativity of the * operator? (5 points) (2b) What...
Considering the following BNF grammar, answer the questions. <prog> - <assign> | <expr> <assign> = <id> = <expr> <expr> := <expr> + <term> | <expr> - <term> | <term> <term> := <factor> | <factor> * <term> <factor> ::= ( <expr> ) | <id> | <num> <id>::= ABC <num> := 0|1|2|3 2a - What is the associativity of the * operator? (5 points) 2b - For the * and + operators, do they have the same precedence, does the * operator...
Question Set 2 1. Given the following grammar dactor>-> ( <expr> ) a) What is the associativity of each of the operators? What is precedence of the operators? Show a leftmost derivation and parse tree for the following sentence: b) c) A-A(B(C A)) d) Rewrite the BNF grammar above to give precedence over and force to be right associative.
Question Set 2 1. Given the following grammar dactor>-> ( <expr> ) a) What is the associativity of each of the operators? What is precedence of the operators? Show a leftmost derivation and parse tree for the following sentence: b) c) A-A(B(C A)) d) Rewrite the BNF grammar above to give precedence over and force to be right associative.
6. (8 pts) Using grammar below show a Parse tree and leftmost derivation for a). A = A * (B+C) <assign> à<id> = <expr> <id> à A | B|C <expr>à <expr> + <term> | <term> <term> à <term> * <factor> |<factor> <factor> à ( <expr> ) |<id>
3. Using the grammar below, show a parse tree and a leftmost derivation for the statement. A = ( A + (B)) * C assign <idxpr expr>? <expr> <term> term <term factor factor (<expr>) l <term I <factor l <id> 4. Prove that the following grammar is ambiguous (Give sentence that has two parse trees, and show the parse trees):
1 Prog-DCs Strnts $ Terminal Regular Expression floatdcl intdcl I intdcl id print "p" id 6 Stmts- Stmt Simts 8 Strnt →id assign Val Expr 10 Expr → plus Val Expr assign " plus minus inum [0-91 fnum 0-9.0-91 blank ("" 2 I print id I minus Val Expr 12 13 Vai -id 14 15 I inum I fnum (Derivation and parse tree) Consider the above ac grammar, show the derivation and it parse tree of the following ac program
Add a unary minus operator that has higher precedence than either + or *, to the following the grammar (below): <assign> -> <id> = <expr> <id> -> A | B | C <expr> -> <expr> + < term> | <term> <term> -> <term> * <factor> | <factor> <factor> -> ( <expr> ) | <id> please dont copy and paste
Question 3: Given the following grammar: assign → id := expr expr → expr + term \ term term -term *factor lfactor factor-(expr) id Using the above grammar, show a leftmost derivation (first five steps) for the following assignment statement: A ((A B)+ C) a. [3 marks] b. Using the above grammar, show a rightmost derivation (first five steps) for the following assignment statement: A:-A+B+C)+A [3 marks] Draw the abstract syntax tree for each of the above statements [4 marks]...
- Using the grammar in Example 3.2, show a parse tree and a leftmost derivation for the following statement: B = C * (A * (B + C)). EXAMPLE 3.2 A Grammar for Simple Assignment Statements <assign> → <id> = <expr> <id> → A | B | C <expr> → <id> + <expr> | <id> * <expr> | ( <expr> ) | <id>