Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.

Similar presentations


Presentation on theme: "Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b."— Presentation transcript:

1 Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b n : n = 0, 1, 2, …} - {w : w is palindrome word} We are going to define a new class of languages, called context-free languages that contain all regular languages and many more (including the 2 above)

2 Context-Free Grammar (preliminaries ) A context-free grammar is a kind of program Languages that are generated by context-free grammars are called context-free languages Context-free grammars are more expressive than finite automata: if a language L is accepted by a finite automata then L can be generated by a context-free grammar

3 My First Context-Free Grammar S  bA A  aA A  b  = {a,b} Elements in  are called terminals S and A are called variables

4 Context-Free Grammar (CFG) Definition. A context-free grammar (CFG) is a 4-tuple (V, , R, S), where:  is an alphabet (characters  are called terminals) V is a set (elements in NT are called variables) R is a subset of NT  (   NT)* S, the start variable, is one of the variables in NT V   =  If ( ,  )  R, we write       is called a rule

5 Derivations Definition. u yields v in one-step, written u  v, if: for some u,v in (V   )* the following 3 conditions hold: u = x  z v = x  z    in R Definition. u derives v, written u  * v, if: There is a chain of one-step yields of the form: u  u 1  u 2  …  v

6 Example (2)  = {a,b} V = {S} R = { S  aSb, S  e }

7 Context-Free Languages Definition. Given a context-free grammar G = (V, , R, S), the language generated or derived from G is the set: L(G) = {w   *: } S  * w Definition. A language L is context-free if there is a context-free grammar G = ( , NT, R, S), such that L is generated from G

8 Example (3)  = {a,b} NT = {S} R = { S  aS, S  Sb, S  e}

9 Example (4)  = {a,b} NT = {S} R = { S  aSa, S  bSb, S  e}

10 Parse Tree A parse tree of a derivation u  u1  u2  …  v is a tree in which: Each internal node is labeled with a variable If a rule A  A 1 A 2 …A n occurs in the derivation then A is a parent node of nodes labeled A 1, A 2, …, A n S a S a S S e b

11 Leftmost, Rightmost Derivations Definition. A leftmost derivation of a sentential form is one in which rules transforming the left-most nonterminal are always applied Definition. A rightmost derivation of a sentential form is one in which rules transforming the right-most nonterminal are always applied

12 Ambiguous Grammar S  A S  B S  AB A  aA B  bB A  e B  e Definition. A grammar G is ambiguous if there is a word w  L(G) having are least two different leftmost derivations Notice that the word a has at least two left-most derivations Some ambiguous grammars G can be disambiguated:  find an unambiguous grammar G’ such that L(G) = L(G’) Some languages cannot be disambiguated

13 Chomsky Normal Form Definition: A grammar is in Chomsky Normal Form if every rule is of the form:  A  BC (A, B, C variables; B and C are not the start variable)  A  a  S  e (S is the start variable) Theorem: Any CFG G can be converted into a grammar G’ in Chomsky Normal Form such that L(G) = L(G’)  Add new rule S 0  S (S 0 is the new start variable)  Remove rules of the form A  e, and for every rule B  A add a new rule: B   Remove rules of the form A  B and for every rule B  add a new rule: A   Remove rules A  … with n > 2 and add rules: A  A 1, A 1 , …, A n-1   Replace any rule: A  cA i with A  UA i, U  c See example 2.10


Download ppt "Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b."

Similar presentations


Ads by Google