Question

In: Computer Science

Given a grammar G, G = (Ν, Σ, Π, S), where Ν = { ... }...

Given a grammar G, G = (Ν, Σ, Π, S), where

Ν = { ... }

Σ = { ... }

Π = { ... }

S is ... For a string ... write

(a) A leftmost derivation

(b) A rightmost derivation

Solutions

Expert Solution

Leftmost derivation
• Leftmost nonterminal is replaced in each step
Rightmost derivation
• Rightmost nonterminal is replaced in each step
Example
• Grammar
S → AB, A → a, B → b
• Leftmost derivation for ab-

S ⇒ AB ⇒ aB ⇒ ab
• Rightmost derivation for ab-

S ⇒ AB ⇒ Ab ⇒ ab

For unambiguous

Consider the following grammar-

S → aS / ∈

The language generated by this grammar is-

L = { an , n>=0 } or a*

All the strings generated from this grammar have their leftmost derivation and rightmost derivation exactly same.

Let us consider a string w = aaa.

Leftmost Derivation-

S → aS

→ aaS (Using S → aS)

→ aaaS (Using S → aS)

→ aaa∈

→ aaa

Rightmost Derivation-

S → aS

→ aaS (Using S → aS)

→ aaaS (Using S → aS)

→ aaa∈

→ aaa

Clearly,

Consider the following grammar-

S → aB / bA

S → aS / bAA / a

B → bS / aBB / b

(Unambiguous Grammar)

Let us consider a string w = aaabbabbba

Now, let us derive the string w using leftmost derivation.

Leftmost Derivation-

S → aB

→ aaBB (Using B → aBB)

→ aaaBBB (Using B → aBB)

→ aaabBB (Using B → b)

→ aaabbB (Using B → b)

→ aaabbaBB (Using B → aBB)

→ aaabbabB (Using B → b)

→ aaabbabbS (Using B → bS)

→ aaabbabbbA (Using S → bA)

→ aaabbabbba (Using A → a)

2. Rightmost Derivation-

  • The process of deriving a string by expanding the rightmost non-terminal at each step is called as rightmost derivation.
  • The geometrical representation of rightmost derivation is called as a rightmost derivation tree.

Example-

Consider the following grammar-

S → aB / bA

S → aS / bAA / a

B → bS / aBB / b

(Unambiguous Grammar)

Let us consider a string w = aaabbabbba

Now, let us derive the string w using rightmost derivation.

Rightmost Derivation-

S → aB

→ aaBB (Using B → aBB)

→ aaBaBB (Using B → aBB)

→ aaBaBbS (Using B → bS)

→ aaBaBbbA (Using S → bA)

→ aaBaBbba (Using A → a)

→ aaBabbba     (Using B → b)

→ aaaBBabbba (Using B → aBB)

→ aaaBbabbba    (Using B → b)

→ aaabbabbba (Using B → b)

NOTES

  • For unambiguous grammars, Leftmost derivation and Rightmost derivation represents the same parse tree.
  • For ambiguous grammars, Leftmost derivation and Rightmost derivation represents different parse trees.

Here,

  • The given grammar was unambiguous.
  • That is why, leftmost derivation and rightmost derivation represents the same parse tree.

Related Solutions

For a grammar G with the productions where G = ( {S, A, B}, {a, b},...
For a grammar G with the productions where G = ( {S, A, B}, {a, b}, S, P ) with productions                                              S --> AB | bbbB,              A --> b | Ab,       B --> a.. 1.Show that the grammar G is ambiguous. 2.Give language L that is generated by G, L = L(G), in a formal expression (including a regular expression). 3.Can you construct an unambiguous grammar that is equivalent to G? Otherwise, show that G is inherently ambiguous.
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.
1. Consider the regular grammar G given below: S → aS|aA|bB|λ A → aA|bS B →...
1. Consider the regular grammar G given below: S → aS|aA|bB|λ A → aA|bS B → bB|aS (a) Give a derivation for the strings aabbba and baaba (b) Give an infinite language L such that L is not a subset of L(G) (c) Give a regular expression that describes the language L(G).
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.
Simplify the grammar G. Does L(G) contain ε ? S -> A B C | B...
Simplify the grammar G. Does L(G) contain ε ? S -> A B C | B a B A -> a A | B a C | a a a B -> b B b | a | D C -> C A | A C D -> ε
The position of a 50 g oscillating mass is given by x(t)=(2.0cm)cos(10t−π/4), where t is in...
The position of a 50 g oscillating mass is given by x(t)=(2.0cm)cos(10t−π/4), where t is in s. If necessary, round your answers to three significant figures. Determine: amplitude= 2cm period= 0.628s spring constant =5 N/m phase constant= -0.785 rad find initial coordinate of the mass and the initial velocity.
In a bubble chamber experiment, the decay of a Σ+ particle into a π+ particle and...
In a bubble chamber experiment, the decay of a Σ+ particle into a π+ particle and a neutron is observed when a 1.03 T magnetic field is applied perpendicular to the paths of the particles. The Σ+ and π+ particles leave curved tracks of radii 2.01 m and 0.588 m respectively. The rest masses are mΣ+ = 1189.4 MeV/c2 and mπ+ = 139.6 MeV/c2. If at the moment of decay, the angle between the momentum of the Σ+ and π+...
For pure Aloha system, prove that the throughput S=Ge-2G, where G is the traffic load, given...
For pure Aloha system, prove that the throughput S=Ge-2G, where G is the traffic load, given the assumption that the packet arrival is Poisson. Explain what RTS and CTS mechanisms, and explain how they solve the hidden node and exposed node problems.
The following hypotheses are given. H0 : π ≤ 0.81 H1 : π > 0.81 A...
The following hypotheses are given. H0 : π ≤ 0.81 H1 : π > 0.81 A sample of 140 observations revealed that p = 0.92. At the 0.10 significance level, can the null hypothesis be rejected? State the decision rule. (Round your answer to 2 decimal places.) Compute the value of the test statistic. (Round your answer to 2 decimal places.) What is your decision regarding the null hypothesis? Reject H0. Do not reject H0.
The following hypotheses are given. H0 : π ≤ 0.81 H1 : π > 0.81 A...
The following hypotheses are given. H0 : π ≤ 0.81 H1 : π > 0.81 A sample of 80 observations revealed that p = 0.86. At the 0.10 significance level, can the null hypothesis be rejected? State the decision rule. (Round your answer to 2 decimal places.) Reject H0 if z > _____. Compute the value of the test statistic. (Round your answer to 2 decimal places.) Value of the test statistic: _____ What is your decision regarding the null...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT