0% found this document useful (0 votes)
73 views11 pages

Solutions Languages Formal

The document discusses exercises related to formal languages and automata theory. It provides examples of generating grammars for specific languages and determining the type of grammars. It also includes exercises analyzing grammars and languages generated by grammars.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views11 pages

Solutions Languages Formal

The document discusses exercises related to formal languages and automata theory. It provides examples of generating grammars for specific languages and determining the type of grammars. It also includes exercises analyzing grammars and languages generated by grammars.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Formal Languages and Automata Theory

Formal Languages and Automata Theory


Exercises Languages and Formal Grammars
Unit 4

Authors:
Araceli Sanchis de Miguel
Agapito Ledezma Espino
Jose A. Iglesias Martínez
Beatriz García Jiménez
Juan Manuel Alonso Weber

* Several exercises are based on the ones proposed in the following books:
 Enrique Alfonseca Cubero, Manuel Alfonseca Cubero, Roberto Moriyón Salomón.
Teoría de autómatas y lenguajes formales. McGraw-Hill (2007).
 Manuel Alfonseca, Justo Sancho, Miguel Martínez Orga. Teoría de lenguajes,
gramáticas y autómatas. Publicaciones R.A.E.C. (1997).
 Pedro Isasi, Paloma Martínez y Daniel Borrajo. Lenguajes, Gramáticas y Autómatas.
Un enfoque práctico. Addison-Wesley (1997).
Formal Languages and Automata Theory

1. Create a grammar to generate the following languages:


a) { a, aa, aaa }
b) { a, aa, aaa, aaaa, aaaaa, …)
c) { λ, a, aa, aaa }
d) { λ, a, aa, aaa, aaaa, aaaaa, …)
The notation used to denote each one of the languages is:
a) { an | n  [1, 3] }
b) { an | n > 0 }
c) { an | n  [0, 3] }
d) { an | n  0 }

Solution:

a)
G=({a}, {S}, S, P} where:
P={S::=a | aa | aaa}

b)
G=({a}, {S, A}, S, P} where:
P={S::=A
A::= a | aA}

c)
G=({a}, {S}, S, P} where:
P={S::=  | a | aa | aaa}

d)

G=({a}, {S, A}, S, P} where:


P={S::=  |A
A:= a | aA}

2. Given the grammars G=({c,d}, {S,A,T}, S, Pi} where:


G1 G2 G3 G4 G5
T = {c} T = {c,d} T = {c} T = {c,d} T = {c,d}
NT = {S, A} NT = {S, A} NT = {S, A} NT = {S, A,T} NT = {S, A}
P1: SA P2: SA P3: SA P4: ScA P5: SA
AAA| c AcAd| cd AAcA| c Ad | cA| Td AAd| cA | c| d
TTd | d
Determine the associated language.

Solution:

L(G) = {x / S* x AND x* }


Formal Languages and Automata Theory

a) S::= | A
A::=AA | c
S

A 

AA c

AAA cA

cAA cc

cAAA ccA

ccAA ccc

L(G1)={ , c, cc, ccc, …. }= {, cn} with n=1, 2, 3, ……

b) S::= | A
A::=cAd | cd

A 

cAd cd

ccAdd ccdd

cccAddd cccddd

L(G2)={ , cd, ccdd, cccddd, …. }= {, cndn} with n=1, 2, 3, ……


Formal Languages and Automata Theory
c) S::= | A
A::=AcA | c

A 

AcA c

AcAcA ccA

AcAcAcA ccc
ccAcA
AcAcAcAcA
ccAcAcA ccccA

ccAcAcAcA ccccAcA ccccc

ccccAcAcA ccccccA

ccccccAcA ccccccc

ccccccAcAcA ccccccccA

ccccccccAcA ccccccccc
L(G3)={ , c, ccc, ccccc, ccccccc,…. }= {, cn} with n=1, 3, 5, ……=
={, c2n+1} with n=0,1, 2, 3, ……
Formal Languages and Automata Theory
d) S::=cA
A::=d | cA | Td
T::=Td | d

L(Gd)={cndm / n,m1}

cA

cd ccA cTd

cdd cTdd

ccd cccA ccTd


cddd cTddd
ccdd ccTdd
cdddd cTdddd
cccd ccccA cccTd
ccddd ccTddd
L(G4)={ cd, ccd, cccd, …..,cdd, ccdd,..... ccddd, ccddd, cdddd, …. }= {c ndm} with n,m1
Formal Languages and Automata Theory
e) S::= | A
A::=cd |Ad | cA

L(Ge)= {, cndm / n,m1}

A 

cd Ad cA

cdd Add cAd ccA


ccd cAd

ccdd cAdd ccAd cccA


cccd ccAd

cddd Addd cAdd

L(G5)={ , cd, cdd, cddd,...... ccd, ccdd, ccddd,…. }= {, cndm} with n,m>=1
Formal Languages and Automata Theory

3. Determine the type of the following grammars into the Chomsky Hierarchy. Justify your answer.
a) G=({a,b}, {A,B,S}, S, P),
P={S::=aA, A::=bB, A::=aA, A::=a, B::=λ}

b) G=({a,b,c}, {A,B,C,S}, S, P),


P={S::=aAb, S::=Ba, S::=λ, aAbC::=aAbB, aAbC::=aabC, BCc::=AaCc,
BCc::=BaAbc, C::=Ca, C::=a}

c) G=({ house, garden, cat}, {S, CASTLE, FOREST, TIGER }, S, P),


P={ S::=TIGER garden, S::=FOREST CASTLE, FOREST::=λ, garden
CASTLE TIGHER house::=garden FOREST TIGER house, cat CASTLE
FOREST::=cat FOREST house TIGER FOREST, FOREST::=TIGER house,
FOREST::=garden }

d) G=({x,y}, {C,A,B,S}, S, P),


P={S::=Cx, S::=Cy, S::=By, S::=Ax, S::=x, S::=y, A::=Ax, A::=Cx, A::=x,
B::=By, B::=yA, C::=xA}

e) G=({a,b,c}, {S,B}, S, P),


P={S::=abc, S::=aBSc, Ba::=aB, Bb::=bb}

Solution:

a) Type-0.

b) Type-1.

c) Type-0.

d) Type-2.

e) Type-0.

4. Given the gramar G,


G=({a,b,c}, {S,A,B}, S, P), P={S::=λ, S::=aAc, A::=aA, A::=Ac, A::=B, B::=b, B::=Bb}
It is required:
a) Specify the type of G in the Chomsky Hierarchy. Justify your answer.
b) Determine the language L generated by the grammar.
c) Construct two different derivation trees for a word in L(G).
d) Verify if the following sentential forms are valid in G, and write a derivation chain to generate
the valid ones.
d.1.- aaAcc
d.2.-ac
d.3.-ababBcc
d.4.-abbccc

Solution:

a) It id s Type-2 grammar.
b) L(G)={apbqcr, p=q=r=0 or p,q,r>0}.
c) word=aabcc
Formal Languages and Automata Theory

S S

A A

A A

A A

B B

a a b c c a a b c c

S → aAc → aaAc → aaAcc → aaBcc → aabcc


S → aAc → aAcc → aaAcc → aaBcc → aabcc

d)

d.1.- aaAcc: S → aAc → aaAc → aaAcc


d.2.- ac: not valid
d.3.- ababBcc: not valid
d.4.- abbccc: S → aAc → aAcc → aAccc → aBccc → aBbccc → abbccc
Formal Languages and Automata Theory
5. Obtain a type-0 grammar for the language L={anbncn / n1}.

Solution:

S::= abc| aBSc


Ba::=aB S
Bb::=bb

abc aBSc

aBaBScc
aBabcc

aBaBaBSccc
aaBbcc aBaBabccc

aabbcc aaBBabccc aBaBaBabcccc

aaBaBbccc aBaBaaBbcccc

aaaBBbccc aBaaBaBbcccc

aaaBbbccc aBaaaBBbcccc

aaBaaBBbcccc
aaabbbccc

aaaBaBBbcccc

aaaaBBBbcccc

aaaaBBbbcccc

aaaaBbbbcccc

aaaabbbbcccc
Formal Languages and Automata Theory
6. Obtain an equivalent well formed grammar for the following one:
G = ({a,b,c,d}, {X,Y,Z,O,P,Q,A}, Z, P),
P = { Z::=Z, Q::=OP, X::=aa, Z::=aX, Y::=aa, Z::=Ya, O::=b, Z::=aaa,
P::=QO, Q::=d, P::=c, O::=PQ}

Solution:

G' = ({a}, {X,Y, Z}, Z, P'), P' = {Z::=aaa, Z::=aX, Z::=Ya, X::=aa, Y::=aa}

7. Given the following left-linear grammar G, obtain an equivalent right-linear G’ grammar.


G=({0,1},{A,S},S,P)
P={ S ::= 1 | A1; A ::= S0}

Solution:

G LI = ({a,b}, {A,S,B}, S, P), P={S::=1|1B, B::=0A, A::=1|1B}

8. Given the gramar G:


G = ({e,f,g,z,a,b,d}, {Y, X, E, A, D, I, G}, A, P),
P = { A::=a
E::=b
A::=azb
A::=aX
E::=E
G::=g
X::=XE
D::=eI
X::=z
Y::=b
I::=fG
X::=Xb
E::=d }
a) Transform to CNF detailing the process followed.
b) Determine whether the words 'abz' y 'azdbb' are included in the language generated by G. If
this is the case, generate a parse tree for the included words. If not, justify the not inclusion in
the language.

Solution:

a)
G' '= ({a,b,d,z}, {A, B, C, D, E, F, X}, A, P''),
P'' = {
A::=a E::=d
A::=BC F::=b
A::=BX X::=XE
B::=a X::=XF
C::=DF X::=z
D::=z }
E::=b
Formal Languages and Automata Theory

b)

A → BX → aX → aXF → aXFF → aXEFF → azEFF → azdFF → azdbF → azdbb

9. Obtain a grammar in CNF equivalent to the following grammar:


G = ({a, b, c},{S, A, B, C, D, E), S, P)
P = { S::=AaB | Cbb | B
A::=Aa | cD
B::=a | Ba | λ
C::=Sa | a | abB
D::=aaA
E::=aa }

Solution:

G = ({a, b},{S, B,C,D,E,F,G), S, P)


P = { S ::= CD | a | BF | λ
B ::= a | BF
C ::= SF | a | FG | FE
D ::=EE
E ::= b
F ::= a
G ::= EB}

10. Given the gramar G calculate an equivalent grammar in GNF.


G = ({a,b}, {S}, S, P), donde P = {S::=aSb | SS | }

Solution:

G’ = ({a,b}, {S,X,B}S, P’})


P’ = {
S ::= aSB | aSbB | 
X ::= aSBX | aSBXX | aSB
B ::= b
}

You might also like