1 / 13

Introduction

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}.

nyoko
Download Presentation

Introduction

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. Introduction • Finite Automata accept all regular languages and only regular languages • Even very simple languages are non regular ( = {a,b}): - {anbn : 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  u1  u2  …  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. S a S S a b S e 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  A1A2…An occurs in the derivation then A is a parent node of nodes labeled A1, A2, …, An

  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 Definition. A grammar G is ambiguous if there is a word w  L(G) having are least two different leftmost derivations S  A S  B S  AB A  aA B  bB A  e B  e • 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 S0 S (S0 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  <C1|c1> …<Cn|cn> with n > 2 and add rules: A  <C1|c1> A1, A1  <C2|c2>, …, An-1  <Cn-1|cn-1> <Cn|cn> • Replace any rule: A  cAi with A  UAi, U  c See example 2.10

More Related