7 views

Uploaded by ef_irshad

save

- Lecture10_15329
- compilerlec4[1]
- Cse384 Compiler Design Laboratory Lab Manual
- cfl5
- Natural Language Grammatical Inference With Recurrent Neural Networks, Lawrence, IEEE-TKDE
- Object Recognition
- TOC Guidelines
- Properties of CFL
- PANINIS GRAMMAR
- Agile Scope Outline Template
- Priprema Za Finalni Test
- AS VERSUS LIKE
- Generative Grammar
- Inglês 6º Ano - Present Simple e Present Continuous
- First and Follow
- Free PDF Software Engineering Pressman
- Marathon
- Past Simple
- 1.2.1 Operaciones Con Números Reales
- UNIDAD 2 Y 3 FUNDAMENTOS DE PROGRAMACION.docx
- IF5011!02!2013-Representasi Informasi - Integer Dan String
- 8085A+INTEL6
- Lista Completa de Caracteres Ascii - Char
- Ch05 Solutions
- Ocaml Tutorial (Cornell)
- Fundamentos Basicos Del Lenguaje C
- Manual de Visual Basic
- Lenguaje C
- Amos Alpha Pascal
- 38483134-Fracciones
- Clave de Sol Nivel 1 Intermedios
- ALPHABETHUM: Fuente Unicode para lingüística e idiomas antiguos
- 3.3. Transformação de frações.pdf
- Studying Rhythm Traduccion
- Python 1 v2.pdf
- FÍSICA TRANSF DE UNIDADES.doc
- Norme 42
- Codigo BCD
- Empezando
- Rules of Typesetting
- Curso de SQL Server-Aulaclic
- 3. 6th Class Maths Track - A, Ratioonal Numbers
- I Get Along Without You Very Well - Stacey Kent version
- advance c++
- Note Name Worksheets
- GT Pascal Hay
- ManuScript Language.pdf
- Metric System

You are on page 1of 6

Dr. Susan Rodger

Section: Context-Free Languages (handout)

**Context-Free Languages (Read Ch. 3.1-3.2)
**

Regular languages:

** keywords in a programming language
**

names of identi

ers integers all misc symbols: .fg in C++ De.((a + b) . Not Regular languages: fa cb jn > 0g n n expressions . c) block structures .

S) is context-free if all productions are of the form A!x Where A2V and x2(V[) .R.nition: A grammar G=(V. De..

fa.t. Example: G=(fSg. L=L(G).S) S ! aSb j ab Derivation of aaabbb: S ) aSb ) aaSbb ) aaabbb L(G) = 1 .nition: L is a context-free language (CFL) i 9 context-free grammar (CFG) G s.bg.R.

b.Bg. L(G) = Example: G=(fS.R.cg.A. bg. S ) AcB ) AcBbb ) Acbb ) aAacbb ) aacbb Note: Next variable to be replaced is underlined. S ) AcB ) aAacB ) aacB ) aacBbb ) aacbb 2.bg.Example: G=(fSg.S) S ! aSa j bSb j a j b j Derivation of ababa: S ) aSa ) abSba ) ababa = fa. De.S.fa.P) S ! AcB A ! aAa j B ! Bbb j L(G) = Derivations of aacbb: 1.fa.

in each step of a derivation.nition: Leftmost derivation . replace the leftmost variable. De.

where A2V. n i A a1 a2 a an 3 2 .. S): root is labeled S leaves labeled x. where x2 [fg nonleaf vertices labeled A. A2V For rule A!a1 a2 a3 : : : a . A derivation tree for G=(V.in each step of a derivation. a 2([V[fg).nition: Rightmost derivation . R. replace the rightmost variable. Derivation Trees (also known as \parse trees") A derivation tree represents a derivation but does not show the order productions were applied.

S) S ! AcB A ! aAa j B ! Bbb j De.fa.R.b.Bg.A.Example: G=(fS.cg.

Yield (w) of derivation tree is such that w2L(G). Leaves from left to right in a derivation tree form the yield of the tree.subtree of derivation tree. The yield for the example above is Example of partial derivation tree that has root S: The yield of this example is which is a sentential form. Example of partial derivation tree that does not have root S: Membership Given CFG G and string w2 .nitions Partial derivation tree . If partial derivation tree has root S then it represents a sentential form. is w2L(G)? If we can .

R= S ! SS j aSa j b j L1 =L(G) = Is abbab 2 L(G)? Exhaustive Search Algorithm 3 . Is w syntactically correct? Example G=(fSg. Motivation G is grammar for C++. R. fa.bg. then we would know that w is in L(G). S).nd a derivation of w. w is C++ program.

S ) aSa ) aSSa 6.3. number of terminal symbols in a sentential form Example: Let L2 = L1 . i=1 1. S ) SS 2.: : : Examine all sentential forms yielded by i substitutions Example: Is abbab 2 L(G)? Theorem If CFG G does not contain rules of the form A! A!B where A. S ) aSa ) aba De. S ) SS ) bS 5. L2=L(G) where G is: S ! SS j aa j aSa j b Show baaba 62 L(G). S ) aSa ) aaSaa 7. S ) aa 4. S ) SS ) aSaS 3. S ) aSa 3. fg. S ) SS ) SSS 2. S ) aSa ) aaaa 8.B2V. Proof: Consider 1. For all i=1. then we can determine if w2 L(G) or if w62 L(G).2. S)b i=2 1. length of sentential forms 2. S ) SS ) aaS 4.

nition Simple grammar (or s-grammar) has all productions of the form: A ! ax 4 .

where A2V.a) can occur in at most one rule. Ambiguity De. a2 . and x2V AND any pair (A.

E).(. (with meaning that multiplication has higher precedence than addition) E ! E+T j T T ! TF j F F ! I j (E) I!ajb There is only one derivation tree for a+bc: De.nition: A CFG G is ambiguous if 9 some w2 L(G) which has two distinct derivation trees. R= E ! E+E j EE j (E) j I I!ajb Derivation of a+ba is: E ) E+E ) I+E ) a+E ) a+EE ) a+IE ) a+bE ) a+bI ) a+ba Corresponding derivation tree is: Another derivation of a+ba is: E ) EE ) E+EE ) I+EE ) a+EE ) a+IE ) a+bE ) a+bI ) a+ba Corresponding derivation tree is: Rewrite the grammar as an unambiguous grammar.b. Example Expression grammar G=(fE. R.+. fa.)g..Ig.

nition If L is CFL and G is an unambiguous CFG s. 5 . L=L(G).t. then L is unambiguous.

) < program> ::= int main () <block> < block> ::= f <stmt-list> g < stmt-list> ::= <stmt> j <stmt><stmt-list> j <decl> j <decl><stmt-list> < decl> ::= int <id> . <stmt-list> g ) int main () f int a . j double <id> . < return-stmt> ::= return <integer> . sum = a + b. But note that fsemantically correct C++ programsg L(G). <stmt-list> g ) complete C++ program More on CFG for C++ We can write a CFG G s. L(G)=fsyntactically correct C++ programsg. < stmt> ::= <asgn-stmt> j <cout-stmt> j <return-stmt> < asgn-stmt> ::= <id> = <expr> . Can't recognize redeclared variables: Can't recognize if formal parameters match actual parameters in number and types: 6 . etc.t. < expr> ::= <expr> + <expr> j <expr> <expr> j ( <expr> ) j <id> < cout-stmt> ::= cout <out-list> . return 0.Backus-Naur Form of a grammar: Nonterminals are enclosed in brackets <> For \!" use instead \::=" Sample C++ Program: int main () { int a. a = 40.. int sum. Must expand all nonterminals! So a derivation of the program test would look like: < program> ) int main () <block> ) int main () f <stmt-list> g ) int main () f <decl> <stmt-list> g ) int main () f int <id>. cout << "sum is "<< sum << endl. b = 6. int b. } \Attempt" to write a CFG for C++ in BNF (Note: <program> is start symbol of grammar.

- Lecture10_15329Uploaded byAnonymous 5lnqrjsDK
- compilerlec4[1]Uploaded byJone Kaky
- Cse384 Compiler Design Laboratory Lab ManualUploaded byM_Computer
- cfl5Uploaded byAshutosh Srivastava
- Natural Language Grammatical Inference With Recurrent Neural Networks, Lawrence, IEEE-TKDEUploaded bycristian1961
- Object RecognitionUploaded bySuganya Aruchamy
- TOC GuidelinesUploaded bySamiksha Goel
- Properties of CFLUploaded bykrishnareddy56
- PANINIS GRAMMARUploaded byapi-3783514
- Agile Scope Outline TemplateUploaded byChristine St Pierre-Saxon
- Priprema Za Finalni TestUploaded byJelena Milosavljevic
- AS VERSUS LIKEUploaded byRaquelcampinoyaitana
- Generative GrammarUploaded byseety2
- Inglês 6º Ano - Present Simple e Present ContinuousUploaded bysilvia_lfr
- First and FollowUploaded byRahul A. Rokade
- Free PDF Software Engineering PressmanUploaded byDoug
- MarathonUploaded byLuis Angel Arevalo Gomez
- Past SimpleUploaded byAnonymous GIkxBsi

- 1.2.1 Operaciones Con Números RealesUploaded bysgumo
- UNIDAD 2 Y 3 FUNDAMENTOS DE PROGRAMACION.docxUploaded byinggafabifi
- IF5011!02!2013-Representasi Informasi - Integer Dan StringUploaded byainihanifa
- 8085A+INTEL6Uploaded byhonquell123
- Lista Completa de Caracteres Ascii - CharUploaded byMariana
- Ch05 SolutionsUploaded byDarkAngel298
- Ocaml Tutorial (Cornell)Uploaded byAnnie Freebies
- Fundamentos Basicos Del Lenguaje CUploaded byATAYL
- Manual de Visual BasicUploaded bycyberdlc
- Lenguaje CUploaded byErick Villanueva
- Amos Alpha PascalUploaded byLev Lafayette
- 38483134-FraccionesUploaded byTulio Ccasani Enciso
- Clave de Sol Nivel 1 IntermediosUploaded byWester García
- ALPHABETHUM: Fuente Unicode para lingüística e idiomas antiguosUploaded byJotbe Bustamante
- 3.3. Transformação de frações.pdfUploaded byGabriel Pinheiro
- Studying Rhythm TraduccionUploaded byJohn Rodríguez Sánchez
- Python 1 v2.pdfUploaded byDeepakPrabhu
- FÍSICA TRANSF DE UNIDADES.docUploaded byCarlosJordanoVieraCalle
- Norme 42Uploaded bySilva Jorge
- Codigo BCDUploaded byErlanDuarte
- EmpezandoUploaded byARITOSOUL341
- Rules of TypesettingUploaded bybluestationwagon
- Curso de SQL Server-AulaclicUploaded bycloduñas
- 3. 6th Class Maths Track - A, Ratioonal NumbersUploaded bySamhithaReddy
- I Get Along Without You Very Well - Stacey Kent versionUploaded byJoseGuy
- advance c++Uploaded byapi-3738664
- Note Name WorksheetsUploaded byCarey Tyler-Lelond
- GT Pascal HayUploaded bychauthanhphan22
- ManuScript Language.pdfUploaded byRichyMejias
- Metric SystemUploaded byaerocristian12345