Create Presentation
Download Presentation

Download Presentation

Context-Free Languages

Download Presentation
## Context-Free Languages

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**Giorgi Japaridze**Theory of Computability Context-Free Languages Chapter 2**2.1.a**Giorgi JaparidzeTheory of Computability What is a CFG A B A B 0A1 Terminals: 0,1 Variables: A,B Productions: Start variable: A A B 0A1 0B1 00A11 0011 Derivation: A B Parse tree: 0 1 A B 0 1 A **2.1.b**Giorgi JaparidzeTheory of Computability Our grammar simplified A 0A1 A 0A1 00A11 000A111 0000A1111 00001111 A What language does this grammar produce?**2.1.c**Giorgi JaparidzeTheory of Computability A more complex CFG S N’_V_N’ N’ N |N_who_V_N’ N men | women | children V like | hate | respect S N’ _ V _ N’ N respect N _ who _ V _ N’ children men hate N women**2.1.d**Giorgi JaparidzeTheory of Computability Formal definitions Acontext-free grammaris a 4-tuple (V,,R,S), where 1. V is a finite set called thevariables; 2. is a finite set, disjoint from V, called theterminals; 3. R is a finite set ofrules, with each rule being a pair of a variable and a string of variables and terminals; 4. S is an element of V called thestart variable. If u,v, and w are strings of variables and terminals and Aw is a rule, we say that uAv yields uwv, written uAv uwv. x * y means that x=y, or x y, or there are z1,…,zn such that x z1 … zn y. Thelanguage produced(defined, described) by the grammar is {w | S * w and w is a string of (only) terminals}. Acontext-free languageis a language produced by some CFG.**2.1.e**Giorgi JaparidzeTheory of Computability Designing context-free grammars Design a CFG that produces all regular expressions over the alphabet {0,1}: <RE> Design a CFG G that produces the union of the languages produced by two given CFGs G1 and G2. G1: A1 w1 … An wn G2: B1 u1 … Bm um**2.1.f**Giorgi JaparidzeTheory of Computability Context free versus regular languages Theorem. Every regular language is context-free, but not every context-free language is regular. Proof – omitted. Context-free languages Regular languages {0n1n | n0}**2.2**Giorgi JaparidzeTheory of Computability Pushdown automata Just as to regular languages correspond finite automata, to context-free languages correspond pushdown automata. This is a new model of computation. What makes a PDA different from NFA is that it has potentially infinite, stack-type (LIFO) memory, where unlimited number of things can be remembered (pushed) and read (popped). A given transition depends on not only the current symbol in the input, but also the symbol currently at the top of the stack. PDAs are still not the most powerful machines. This is because of their limited (LIFO) access to memory. We omit the topic of pushdown automata.**2.3.a**Giorgi JaparidzeTheory of Computability Just as for regular languages, there is a pumping lemma for context-free languages, used as a tool for proving that a given language is not context-free. We omit this topic, but just remember the following examples of simple NON-CONTEXT-FREE LANGUAGES: Non-context-free languages • {anbncn | n0} • {ww | w*} (as long as the alphabet has at least two symbols)**2.3.b**Giorgi JaparidzeTheory of Computability Regular vs context-free vs computer-recognizable languages Computer-recognizable languages Context-free languages Regular languages {anbn | n0} {anbncn | n0}