Docsity

CFG Practice Problems Solutions: Parsing Theory and Ambiguous Grammars - Prof. Jeffrey S. , Assignments of Programming Languages

Toggle sidebar
bg1
CMSC330: CFG Practice Problem Solutions
Disclaimer: Please let the TAs know if you find any problem or have any questions
regarding the solutions below.
1) L={anbn|nis even and n≥0}
In words: A sequence of an even number of a’s followed by the same number of b’s.
2)
a) One correct grammar is:
S→T W
T→U c
U→aU cc |V
V→V b |
W→W d |
b) Leftmost Derivation: S→T W →U cW →aU cccW →aV cccW →aV bcccW →
aV bbcccW →abbcccW →abbcccW d →abbcccd
Rightmost Derivation: S→T W →T W d →T d →Ucd →aU cccd →aV cccd →
aV bcccd →aV bbcccd →abbcccd
c) The parse tree is:
3)
a) Rewrite the language as L={an−mambman−m}
S→aSa |T
T→aT b |
b) The left and rightmost derivations are identical because there is never more than one variable to replace.
Derivation: S→aSa →aT a →aaT ba →aaaT bba →aaabba
c) The parse tree is:
1
pf3
pf4
pf5

Partial preview of the text

Download CFG Practice Problems Solutions: Parsing Theory and Ambiguous Grammars - Prof. Jeffrey S. and more Assignments Programming Languages in PDF only on Docsity!

CMSC330: CFG Practice Problem Solutions

Disclaimer: Please let the TAs know if you find any problem or have any questions

regarding the solutions below.

  1. L = {anbn^ | n is even and n ≥ 0 } In words: A sequence of an even number of a’s followed by the same number of b’s.

a) One correct grammar is: S → T W T → U c U → aU cc | V V → V b |  W → W d | 

b) Leftmost Derivation: S → T W → U cW → aU cccW → aV cccW → aV bcccW → aV bbcccW → abbcccW → abbcccW d → abbcccd

Rightmost Derivation: S → T W → T W d → T d → U cd → aU cccd → aV cccd → aV bcccd → aV bbcccd → abbcccd

c) The parse tree is:

a) Rewrite the language as L = {an−mambman−m} S → aSa | T T → aT b | 

b) The left and rightmost derivations are identical because there is never more than one variable to replace.

Derivation: S → aSa → aT a → aaT ba → aaaT bba → aaabba

c) The parse tree is:

a) NOTE: This exercise has been updated: The grammar does not have to be unambiguous.

A good strategy here is to make a separate production rule for each regular expression operator.

S → XBaB X → aY | bY Y → aY | bY |  B → bB | 

b) The grammar in (a) is ambiguous. Therefore, the following derivations and parse trees are only one instance of several possibilities.

Leftmost Derivation: S → XBaB → aY BaB → aaY BaB → aaaY BaB → aaaBaB → aaabBaB → aaabbBaB → aaabbaB → aaabba

Rightmost Derivation: S → XBaB → XBa → XbBa → XbbBa → Xbba → aY bba → aaY bba → aaaY bba → aaabba

c) The parse tree is:

  1. An NFA for the grammar is:

We can then build the corresponding DFA:

b) A grammar corresponding to the regular expression is:

S → T 0 T → 1 U |  U → 1 U | 0 U | 

c) All strings of the language end in 0. We can either have a string contraining only the 0 , or have it preceded by the 1(0|1)∗. So, in case the string is not 0 , we must have a 1 as initial symbol; then we’re allowed to have anything else in between (represented by (0|1)∗). Examples of strings in this language are 0 , 10 , 100 , 110 , 1010 , 1100 , 1110 ,.. .. This can be thought of as the binary representations of even numbers.

a) To help us in designing the grammar, we can re-write ambnam+n^ as ambnanam. Remembering that we must have m ≥ 0 and n ≥ 1 , a resulting grammar for the language is:

S → aSa | T T → bT a | ba

b) One way to think of this problem is to consider all possible terminal symbols for the first character read and then derive each corresponding rule. For example, if the first symbol read is an a, to balance out the expression (as to maintain m + n = p + q), we could require a c or d at the end of the string. Similar reasoning for the case in which b is the first symbol read provides us with the rule:

S → aSd | aT c | bU d | bV c | 

Each non-terminal describes a different situation. For example, for T , we can still have both a’s and b’s in the beginning of the string it represents, but we can only have c’s at its end (no more d’sr), since a c has been prevoulsy consumed at the end of the string. With this in mind, we come up with the remaining rules for the grammar:

T → aT c | bV c |  U → bU d | bV c |  V → bV c | 

c) If we reason in the same fashion as in problem, 9)a), by considering all of the possible initial symbols of the string and the corresponding rules, we can start out with the rule S:

S → aA | bS | 

Note that if we have a b as initial terminal symbol, the next symbol can be anything, therefore the derivation S → bS contained in the rule above. On the other hand, if our string is of the form aA, we can’t have a b as the initial terminal in A (otherwise we would have an ab sequence), so:

A → aA | 

d) S → aSa | bSb | aa | bb

  1. The union operation in the languages can be mapped to the OR ( | ) operation in the rules description. So let us describe {anbnambm^ | m, n ≥ 1 } using the non-terminal T , and {anbmambn^ | m, n ≥ 1 }, using the non-terminal V as below:

S → T | V T → U U U → aU b | ab V → aV b | aW b W → bW a | ba

a) We can show that the grammar is ambiguous by finding a string that has two leftmost or rightmost derivations. Equivalently, we could lso provide a string that has two different parse trees. Consider the string (); below are three different leftmost derivations for the it using the provided grammar:

  1. S → SS → (S)S → ()S → ()
  2. S → SS → S → (S) → ()
  3. S → (S) → ()

The ’s are noted above only for clarity; usually they are simply ommitted.

b) A nonambiguous grammar for balanced parentheses is

S → (S)S |