Show that the following grammar for expressions is ambiguous and provide an alternate unambiguous grammar that defines the same set of expressions.
<expr> ::= <term> | <factor> <term> ::= <factor> | <expr> + <term> <factor> ::= id | <expr> * <factor>
answer)
If two or more parse trees or left most or right most derivations exist from single string then the grammar is ambiguous.
string is id*id
leftmost derivation 1:
<expr>::=<term>
::=<expr>*<factor>
::=<factor>*<factor>
::=id*<factor>
::=id*id
leftmost derivation 2:
<expr>::=<term>
::=<expr>*<factor>
::=<term>*<factor>
::=<factor>*<factor>
::=id*<factor>
::=id*id
unambiguous grammar is:
<expr>::=<expr>+<term>|<term>
<term>::=<term>*<factor>|<factor>
<factor>::=id
Show that the following grammar for expressions is ambiguous and provide an alternate unambiguous grammar that...
1) Using the grammar in Example 3.2, show a completed
parse tree for each of the following statements: a) A = A * (B + (C
* A)) b) A = A * (B + (C))
2) Using the original grammar in Example 3.4, show a
completed parse tree for the statement: A = B + C + A
A Grammar for Simple Assignment Statements PLE 3.2 cassign><id> <expr> cidA BIC «ехpг» — sid + <ехpг» id cexpr> ( <expr>)...
Show that the following grammar is ambiguous. Hint: Show two different leftmost or rightmost derivations for the same string. Equivalently, you can show two different parse trees for the same string. <expr> ::= <expr> + <expr> | <expr> - <expr> | <expr> * <expr> | <expr> / <expr> | int int ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Using this grammar show that ambiguity is not acceptable...
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):
) Using the following grammar, show a parse tree and a leftmost derivation for the following sentence (make sure you do not omit parentheses in your derivation): Grammar <assign> → <id> = <expr> <id> → A | B | C <expr> → <expr> + <term> | <term> <term> → <term> * <factor> | <factor> <factor> → (<expr>) | <id> Derive C = (A+B)*(C+A)*(C+B)
In each case below, show that the grammar is ambiguous, and find an equivalent unambiguous gramnar. The symbol ^ represents Lambda. Please only do this problem if you are familiar with Formal Languages and Automata. S --> ABA A --> aA | ^ B --> bB | ^
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)...
Give an unambiguous grammar for the same language generated by
the grammar:
<fruit>* : -<yellow» | <red> <yellow» banana |mango | <empty> <red> ::- cherry | apple | <empty> "Same language" means that the unambiguous grammar can generate exactly the same set of strings as the ambiguous grammar. No more; no fewer. There will of course be a difference in how - by what NTSs and productions - at least some of those strings are generated
* : -
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]...
Given the following ambiguous context free grammar (3x20) 1. (a) Explain why the grammar is ambiguous (b) Find an equivalent unambiguous context-free grammar. (c) Give the unique leftmost derivation and derivation tree for the string s generated from the unambiguous grammar above. 2. Construct non-deterministic pushdown automata to accept the following language (20) 3. Convert the following CFG into an cquivalent CFG in Chomsky Normal Form (CNF) (20)-
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...