Question

In: Computer Science

Write down a Context Free Grammar with 8 productions including 4 variables and 3 terminals. i....

Write down a Context Free Grammar with 8 productions including 4 variables and 3 terminals. i. Check whether the Context Free Grammar is ambiguous or not. Justify your answer with necessary example. ii. Finally, construct the reduced grammar corresponding to your generated grammar.

Solutions

Expert Solution

First thing before studying ambiguity is to learn about basics of CFGs.

Context Free Grammars(CFGs) are classified on the basis of

1.Number of Derivation trees

2.Number of strings

Number of Derivation trees furthur divides CFG  into 2 types:

1.Ambiguous grammars

2.Unambiguous grammars

Now i believe you must have got intuition behind ambiguity problem,

A CFG is said to ambiguous if there exists more than one derivation tree for the given input string i.e., more than one LeftMost Derivation Tree (LMDT) or RightMost Derivation Tree (RMDT).

So here is an example using 8 or more productions, 4 variables 3 terminals,

 Alphabet Set ∑ = { 0,...,9, +, *, . ,{,} }

A -> B     
A - > AA   
A -> A + A
A -> A * A
A -> {A}
B ->  0 | 1 | ... | 9

Now let us assume string to be derived using above grammer:

7 * 9 + 2

I) First leftmost derivation           II) Second  leftmost derivation
         A=>A*A                            A=>A+A
         =>B*A                             =>A*A+A
         =>7*A+A                           =>B*A+A
         =>7*B+A                           =>7*A+A
         =>7*9+A                           =>7*B+A
         =>7*9+B                           =>7*9+A
         =>7*9+2                           =>7*9+2

Now answer to your second question :

We have-

  • Given grammar consists of the following operators-

+ , *,.

  • Given grammar consists of the following operands-

0,1,2,...9

The priority order is-

(0,1,2...9) > * > . > +

where-

  • . operator is left associative
  • + operator is left associative

Using the precedence and associativity rules, we write the corresponding unambiguous grammar as-

A → A + B / B/{A}

B → B . C / C

C → C* / H

H → 0/1/2/3/4/5/6/7/8/9

Unambiguous Grammar

May be this precisely answers your question.


Related Solutions

What makes a deterministic context free grammar deterministic?
What makes a deterministic context free grammar deterministic?
For each of the following grammars, first classify the grammar as Regular, Linear, Context-Free or Context-Sensitive,...
For each of the following grammars, first classify the grammar as Regular, Linear, Context-Free or Context-Sensitive, and then precisely describe the language generated by the grammar. (1) S → aRa R → aR | bR | є (2) S → A | B A → aaaA | є B → Bbb | b (3) S → RbR R → aRb | bRa | RR | bR | є (4)   S → aSbSaS | aSaSbS | bSaSaS | є    (5)...
Let G = (AN , AT , S, P) be a context-free grammar in Chomsky normal...
Let G = (AN , AT , S, P) be a context-free grammar in Chomsky normal form. Prove that if there exists a word w ∈ L(G) generated by a derivation that uses more than |P| + |AT | steps, then L(G) is infinite.
Consider the following context-free grammar G: E ® T + E ® * T i E...
Consider the following context-free grammar G: E ® T + E ® * T i E ® f i E ® * f + T ® + Questions: (5 points) Compute the Canonical LR(1) Closure set for state I0 for grammar G. (10 points) Compute (draw) the DFA that recognizes the Canonical LR(1) sets of items for grammar G. (5 points) Construct the corresponding Canonical LR(1) parsing table. (10 points) Compute (draw) the DFA for LALR(1). (5 points) Construct LALR(1)...
Prove that if G is a context-free grammar in which every variable occurs on the left...
Prove that if G is a context-free grammar in which every variable occurs on the left side of at most one production, then G is unambiguous.
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)}.
Consider the context-free grammar G = ( {S}, {a, b}, S, P) where P = {...
Consider the context-free grammar G = ( {S}, {a, b}, S, P) where P = { S -> aaSb | aab }. Construct a NPDA M that such that L(M) = L(G). I would like the transition graph for this NPDA please.
Let G be any context-free grammar. Show that the number of strings that have a derivation...
Let G be any context-free grammar. Show that the number of strings that have a derivation in G of length n or less, for any n > 0, is finite. Could you please answer with clear explanation. The question is from Elaine Rich's Automata, Computability and Complexity Chapter 11 Exercise 14.
Ex#3: Give a string of lengths, 4, 6, 8 generated by the following grammar G =...
Ex#3: Give a string of lengths, 4, 6, 8 generated by the following grammar G = (V, T, S. P). S → 0S S --> 0S1S S --> λ
Can following BNF convert to CFG (Context Free Grammar) <Boolean_expr> → <Boolean_expr>||<Boolean_term>|<Boolean_term> <Boolean _term> → <Boolean...
Can following BNF convert to CFG (Context Free Grammar) <Boolean_expr> → <Boolean_expr>||<Boolean_term>|<Boolean_term> <Boolean _term> → <Boolean _term> && <Boolean _factor>| <Boolean _factor> <Boolean _factor> →ID | !<Boolean _factor>| (<Boolean _factor>) |<relation_expr> <relation_expr> → ID==ID | ID !=ID | ID < ID | ID≤ID | ID>ID | ID>=ID The above BNF is for Boolean Expression and Relational Expression.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT