2. Consider the following grammar:
<assign> à <id> = <expr>
<id> à A | B | C
<expr> à <id> + <expr>
| <id> * <expr>
| ( <expr> )
| <id>
Show a parse tree and leftmost derivation for the following statements:
(a) A = ( A + B ) * C
(b) A = A * ( B + C )
3. [10 Points] Show that the following grammar is ambiguous.
<S> à <A>
<A> à <A> + <A> | <id>
<id> à a | b | c
4. [10 Points] Compute the weakest precondition for each of the following assignment statements with their given postconditions.
(a) a = 2 * (b – 1) – 1 {a > 0}
(b) a = a + 2 * b – 1 {a > 1}
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]...
Consider the following grammar (G1) for simple assignment statements. (The symbols in double quotation marks are terminal symbols.) assign → id “ = ” expr id → “A” | “B” | “C” expr → expr “ + ” expr | expr “ ∗ ” expr | “(” expr “)” | id a) Give a (leftmost) derivation for string A = B ∗ A + C. b) Give the parse tree for string A = B ∗ A + C. c)...
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.a Consider the following Grammar, <assign> à <id> = <expr> <id> à A | B |C <expr> à < expr> + <expr> | <expr> * <expr> | ( <expr> ) | <id> Derive the following statement using leftmost derivation. A = A * (B*(C+ A)) b. 2 a}. Consider the following grammar that expresses parenthesized expressions of digits, including both addition and multiplication. <expr> := <expr> + <expr> | <expr> * <expr> | (expr>) | <digit> <digit> := 0 | 1 | 2 |...
- 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>
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...
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...
Compute the weakest precondition for each of the following assignment statements and postconditions: a = 2 * (b - 1) - 1 {a > 0} b = (c + 10) / 3 {b > 6} Prove that the following grammar is ambiguous (show 2 trees): <S> → <A> <A> → <A> + <A> | <id> <id> → a | b | c
Use the grammar given below and show a parse tree and a leftmost derivation for each of the following statements. 1. A = A * (B + (C * A)) 2. B = C * (A * C + B) 3. A = A * (B + (C)) <assign> → <id> <expr> = <expr> → <id> + <expr> kid<expr> <expr>) ids