150 likes | 260 Views
COSC 3340: Introduction to Theory of Computation. University of Houston Dr. Verma Lecture 9. Context Free Languages. Strictly bigger class than regular languages. {a n b n | n >= 0}. context-free languages. . regular languages. . . {ww R |w in {a,b}*}.
E N D
COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9 UofH - COSC 3340 - Dr. Verma
Context Free Languages • Strictly bigger class than regular languages {anbn | n >= 0} context-free languages regular languages {wwR |w in {a,b}*} {arithmetic expressions} UofH - COSC 3340 - Dr. Verma
A simple grammar for some sentences <Sentence> <noun> <verb> <object> <noun> Alice | John <verb> eats | eat | ate <object> apple | orange | mango • The goal is to generate sentences in English over the English alphabet. Example: <Sentence> <noun><verb><object> Alice <verb><object> Alice eats<object> Alice eats apple UofH - COSC 3340 - Dr. Verma
Context-free grammars (CFGs) • Informally, a CFG is a finite set of rules. • Each rule is of the form: <nonterminal symbol> string over terminals and nonterminals. • Terminals:- symbols that the desired strings should contain. • Example: {a...z, ' ',...} • Nonterminals:- symbols to which rules can be applied. • Example: {<noun>, <verb>, ...} • A special nonterminal is called start symbol. • Example: <Sentence> UofH - COSC 3340 - Dr. Verma
A grammar for arithmetic expressions • EE + E | E * E | (E) | x | y • Start symbol - E. • Terminals? • Ans: {x, y, *, +, (, ), ''} • Nonterminals? • Ans: {E} • A derivation: EE + EE + E * Ex + E * Ex + x * Ex + x * y UofH - COSC 3340 - Dr. Verma
Another grammar for arithmetic expr’s EE + T | T TT * F | F F(E) | x | y A derivation for x + x * y ? EE + TT + TF + Tx + T x + T * Fx + F * Fx + x * Fx + x * y Why two different grammars for arithmetic expressions? UofH - COSC 3340 - Dr. Verma
Context Free Grammar Definition • A CFG G = (V, T, P, S) where VT = , • V -- A finite set of symbols called nonterminals • T -- A finite set of symbols called terminals • P is a finite subset of V X (VT)* called productions or rules • We write A w whenever (A, w) P. • SV -- start symbol UofH - COSC 3340 - Dr. Verma
Derivations and L(G) • One step derivation: • u v if u = xAy, v = xwy and A w in P • 0 or more steps derivation: • u * v if u = u0 u1 .... un = v (n 0) • L(G) = { w in T* | S * w }. • A language L is context-free if there is a CFG G with L(G) = L UofH - COSC 3340 - Dr. Verma
Example: SaSb | Derivation: SaSbaaSbbaabb. L(G) = ? • Ans: {anbn | n 0 } UofH - COSC 3340 - Dr. Verma
Parse trees • All derivations can be shown in the form of trees. • Order of rule application is lost. S a S b a S b UofH - COSC 3340 - Dr. Verma
Parse trees [contd.] In general, if we apply rule Aw0w1...wn, then we add nodes for wi as children of node labeled A A w0 w1 w2 wn UofH - COSC 3340 - Dr. Verma
EE + EE + E * Ex + E * Ex + x * Ex + x * y E E + E x E * E x y UofH - COSC 3340 - Dr. Verma
EE + TT + TF + Tx + Tx + T * Fx + F * Fx + x * Fx + x * y E E + T F T T * F y F x x UofH - COSC 3340 - Dr. Verma
Leftmost and Rightmost Derivations • Derivation is leftmost if the nonterminal replaced in every step is the leftmost nonterminal. • Consider EE + EE + x. • Is it leftmost derivation? • Derivation is rightmost if the nonterminal replaced in every step is the rightmost nonterminal. • Consider EE + Ex + E. • Is it rightmost derivation? UofH - COSC 3340 - Dr. Verma
Ambiguity • A CFG is ambiguous if there is a string with at least two leftmost derivations • Example: EE + E | E * E | (E) | x | y is ambiguous • A CFL is inherently ambiguous if every CFG that generates it is ambiguous. • Example: {anbncm | n, m0} {ambncn| n, m0} UofH - COSC 3340 - Dr. Verma