1 / 10

CS310

CS310. Context Free Languages and Grammars Sections:2.1 page 99 September 27, 2006. Quick Review. y. z. x. Pumping Lemma

Download Presentation

CS310

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS310 Context Free Languages and Grammars Sections:2.1 page 99 September 27, 2006

  2. Quick Review y z x • Pumping Lemma • If A is a regular language, then there is a number p where, if s is any string in A of length at least p, then s may be divided into three pieces, s = xyz, satisfying the following conditions: • i >= 0, xyiz є L(M) • |y| > 0 • |xy| <= p s = xyz (x, z may be ε)

  3. Context Free Grammar • Another way to represent a language • Can represent more languages than FA • Produces a “Context Free Language” • Pushdown Automata: machine that recognizes a context free language • Trivia: • First used to describe human languages • Now used to parse computer languages (C, C++)

  4. Context Free Grammar • Example A -> 0A1 A -> B B -># Variables: A, B (may appear on LHS and RHS) Terminals: 0, 1, # (only appear on the RHS) Start variable: Variable on LHS of top rule Language: { 0n#1n | n >= 0 } Example: A -> 0A1 -> 00A11 -> 00#11

  5. Example • A -> 0A1 -> 00A11 -> 00#11 • derivation • write u -*> v if there is a derivation of the string v from u using the grammar, where u and v are strings of terminals and variables 0A1 -*> 00#11 • Parse Tree A A A B 0 0 0 # 1 1 1

  6. Exercise R -> XRX | S S -> aTb | bTa T -> XTX | X | ε X -> a | b Variables, terminals of G? Start variable? True or false? T -*> aba

  7. Formal Definition • A context free grammar (CFG) G is a 4-tuple (V, ∑, R, S) • V finite set of variables • ∑ finite set of terminals • R set of rules of form: • variable -> (string of variables and terminals) • S є V, start variable • The language of the grammar is: • L(G) = { w є∑* | S -*> w} • w is in ∑* and can be derived from S

  8. Example L = { w є {a, b}* | aa is a substring } Find a grammar that generates this language

  9. Example • Grammar G2 on page 101 • Show derivation for “a boy sees a flower” • Notice how this statement is non-creepy? • Show the parse tree

  10. Ambiguous Grammar • E -> E + E | E x E | E | a • Find parse tree for: a + a x a

More Related