17.04.2013 Views

Exercícios linguagens livre de contexto com respostas

Exercícios linguagens livre de contexto com respostas

Exercícios linguagens livre de contexto com respostas

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

! " #<br />

$% & ' ( ) * + , ) -) . + / , /<br />

1) Desenvolva Gramáticas Livre <strong>de</strong> Contexto (GLC) que geram as seguintes <strong>linguagens</strong>:<br />

a) L = {a,b}*<br />

b) L = {w | w ε {a,b}* e w = w r , isto é, w é palíndromo e po<strong>de</strong>r ser lido <strong>de</strong> trás para frente<br />

e vice-versa}<br />

c) L = {a i b 2i | i ≥ 1}<br />

d) L = {a i b j c k | i ≠ j ou j ≠ k}<br />

2) Consi<strong>de</strong>re a GLC<br />

G=(V,T,P,S)<br />

V={S,A,B}<br />

T={0,1}<br />

P={S → A1B, A → 0A | ε, B → 0B | 1B | ε}<br />

Qual a linguagem gerada? Essa linguagem é regular ou <strong>livre</strong> <strong>de</strong> <strong>contexto</strong>? Justifique sua<br />

resposta.<br />

3) Construa uma GLC capaz <strong>de</strong> gerar todo o conjunto <strong>de</strong> Expressões Regulares válidas<br />

sobre o alfabeto {0,1}. Dica: utilize "e" para indicar a palavra vazia da expressão<br />

regular.<br />

4) Verifique se a seguinte gramática e ambígua ou não, justificando a resposta.<br />

G=(V,T,P,S)<br />

V={S}<br />

T={a,b}<br />

P={S → SS | aSa | bSb | ε}<br />

5) Se a gramática construída no exercício 3 for ambígua, construa uma nova gramática<br />

removendo a ambigüida<strong>de</strong>.<br />

6) Construa Autômatos <strong>com</strong> Pilha (AP) que reconheçam as seguintes <strong>linguagens</strong>:<br />

a) L = {a i b 2i | i ≥ 1}<br />

b) L = {a n b m c n+m | n,m ≥ 0}<br />

c) L = {a m b n | m ≥ n}


Respostas:<br />

1)<br />

a) G=(V,T,P,S)<br />

V={S}<br />

T={a,b}<br />

P={S→a | aS | b | bS | ε}<br />

b) G=(V,T,P,S)<br />

V={S}<br />

T={a,b}<br />

P={S→ aSa | bSb | a | b | ε}<br />

c) G=(V,T,P,S)<br />

V={S}<br />

T={a,b}<br />

P={S→ abb | aSbb}<br />

d) G=(V,T,P,S)<br />

V={S,A,B,C,D,E}<br />

T={a,b,c}<br />

P={S→ AB | CD, A→ aA | ε, B→ bBc | E | cD, C→ aCb | E | aA, D→ cD | ε, E→ bE | b}<br />

2)<br />

A linguagem é <strong>livre</strong> <strong>de</strong> <strong>contexto</strong> e regular.<br />

É <strong>livre</strong> <strong>de</strong> <strong>contexto</strong> porque po<strong>de</strong> ser representada por gramática <strong>livre</strong> <strong>de</strong> <strong>contexto</strong>.<br />

E é regular, pois é <strong>de</strong>notada pela seguinte expressão regular 0*1(0+1)*.<br />

(Note que se uma linguagem é regular, por <strong>de</strong>finição ela também é <strong>livre</strong> <strong>de</strong> <strong>contexto</strong>.)<br />

3)<br />

G=(V,T,P,S)<br />

V={S}<br />

T={0,1,(,),+,*,∅,e}<br />

P={S→ S+S | SS | S* | (S) | 0 | 1 | ∅ | e}<br />

4)<br />

Sim, é ambígua, pois possui duas árvores <strong>de</strong> <strong>de</strong>rivação para a palavra "aa".<br />

S<br />

S S<br />

a S a e<br />

e<br />

5) G=(V,T,P,E)<br />

S<br />

e<br />

S<br />

S<br />

a S a<br />

e


V={E,T,F}<br />

T={0,1,(,),+,*,∅,e}<br />

P={E→ E+T | T, T→ TF | F, F→ F* | (E) | 0 | 1 | ∅ | e}<br />

6)<br />

a)<br />

(b,B,e) (?,?,e)<br />

(a,e,BB)<br />

b)<br />

(b,e,X)<br />

(a,e,X)<br />

(b,e,X) (c,X,e)<br />

c)<br />

(a,e,B)<br />

(b,B,e)<br />

(c,X,e)<br />

(?,?,e) (?,?,e)<br />

(b,B,e)<br />

(?,e,e) (?,e,e)<br />

(c,X,e)<br />

(b,B,e)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!