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?
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.
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 --> λ
I need 3 & 4... free free to answer 1 & 2 if you want! Lenses...
I need 3 & 4... free free to answer 1 & 2 if you want! Lenses may be simple ones with two spherical curved surfaces on a piece of transparent material like glass or plastic, or much more complex and compounded of different elements each with sometimes a different material. The surfaces do not have to be spherical, and manufacturing techniques today allow combining these "aspheric" lenses in designs that produce exquisite detail in an image. Your cell phone camera...
Given are five observations for two variables, xi yi 1 3 2 8 3 5 4...
Given are five observations for two variables, xi yi 1 3 2 8 3 5 4 11 5 14 Round your answers to two decimal places. a. Estimate the standard deviation of ŷ* when x = 4. 2 b. Develop a 95% confidence interval for the expected value of y when x = 4. c. Estimate the standard deviation of an individual value of y when x = 4. d. Develop a 95% prediction interval for y when x =...
(a) In C++, write down the main method including a for loop. The body of the...
(a) In C++, write down the main method including a for loop. The body of the for loop includes the followings : i. an if condition ii. an array (of any type & of any size) iii. a structure variable (with at least two variables of any type) (b) Show the corresponding code contents in memory for the main method and draw the stack contents while the main method starts execution and reaches at the line prior to return from...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT