Context-Free Grammars

Slides:



Advertisements
Similar presentations
Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
Advertisements

1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Simplifying CFGs There are several ways in which context-free grammars can be simplified. One natural way is to eliminate useless symbols those that cannot.
About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.
1 Chomsky Normal Form of CFG’s Definition Purpose Method of Constuction.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory VII.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
Western Michigan University CS6800 Advanced Theory of Computation Spring 2014 By Abduljaleel Alhasnawi & Rihab Almalki.
CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.
CS5371 Theory of Computation
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Context-free.
Lecture Note of 12/22 jinnjy. Outline Chomsky Normal Form and CYK Algorithm Pumping Lemma for Context-Free Languages Closure Properties of CFL.
104 Closure Properties of Regular Languages Regular languages are closed under many set operations. Let L 1 and L 2 be regular languages. (1) L 1  L 2.
Normal forms for Context-Free Grammars
1 Module 32 Chomsky Normal Form (CNF) –4 step process.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
1 Properties of Context-free Languages Reading: Chapter 7.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Formal Grammars Denning, Sections 3.3 to 3.6. Formal Grammar, Defined A formal grammar G is a four-tuple G = (N,T,P,  ), where N is a finite nonempty.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف بهار 88.
Formal Languages Context free languages provide a convenient notation for recursive description of languages. The original goal of CFL was to formalize.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
Normal Forms for Context-Free Grammars Definition: A symbol X in V  T is useless in a CFG G=(V, T, P, S) if there does not exist a derivation of the form.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
A sentence (S) is composed of a noun phrase (NP) and a verb phrase (VP). A noun phrase may be composed of a determiner (D/DET) and a noun (N). A noun phrase.
1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n  0} is not regular. L 2 = {(), (()), ((())),...} is not regular.  some properties.
Classification of grammars Definition: A grammar G is said to be 1)Right-linear if each production in P is of the form A  xB or A  x where A and B are.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
CSCI 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Ambiguity.
Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.
Chapter 5 Context-Free Grammars
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
Context Free Grammar. Introduction Why do we want to learn about Context Free Grammars?  Used in many parsers in compilers  Yet another compiler-compiler,
CS 3240: Languages and Computation Context-Free Languages.
CMSC 330: Organization of Programming Languages Context-Free Grammars.
Lecture 11 Theory of AUTOMATA
CSCI 3130: Formal languages and automata theory Tutorial 4 Chin.
1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions.
Closure Properties Lemma: Let A 1 and A 2 be two CF languages, then the union A 1  A 2 is context free as well. Proof: Assume that the two grammars are.
1 Chapter 6 Simplification of CFGs and Normal Forms.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Context-Free Languages
Transparency No. 1 Formal Language and Automata Theory Homework 5.
Dept. of Computer Science & IT, FUUAST Theory of Computation 2 Regular Expressions Regular Expression.
1 Context Free Grammars Xiaoyin Wang CS 5363 Spring 2016.
About Grammars Hopcroft, Motawi, Ullman, Chap 7.1, 6.3, 5.4.
Theory of Languages and Automata By: Mojtaba Khezrian.
AUTOMATA THEORY. Chapter 05 CONTEX-FREE GRAMMERS AND LANGUAGES.
Compiler Chapter 5. Context-free Grammar Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
Context-Free Languages & Grammars (CFLs & CFGs) (part 2)
PROGRAMMING LANGUAGES
CONTEXT-FREE LANGUAGES
Normal Forms for CFG’s Eliminating Useless Variables Removing Epsilon
Complexity and Computability Theory I
7. Properties of Context-Free Languages
Recap Lecture 34 Example of Ambiguous Grammar, Example of Unambiguous Grammer (PALINDROME), Total Language tree with examples (Finite and infinite trees),
FORMAL LANGUAGES AND AUTOMATA THEORY
Lecture 14 Grammars – Parse Trees– Normal Forms
Jaya Krishna, M.Tech, Assistant Professor
Context-Free Languages
NORMAL FORMS FDP ON THEORY OF COMPUTING
7. Properties of Context-Free Languages
Chapter 6 Simplification of Context-free Grammars and Normal Forms
Finite Automata and Formal Languages
Context-Free Languages
Presentation transcript:

Context-Free Grammars 1. Formal Definition 2. Construction 3. Parse Tree 4. Ambiguity 5. Simplification of CFG 6. CNF & GNF

English Grammar sentence  noun_phrase predicate noun_phrase  article noun predicate  verb article  a | an | the noun  boy | dog verb  runs | walks a boy runs a dog walks

Context-Free Grammar A grammar G=( V, T, S, P ) is said to be context-free if all productions in P have the form A   , where AV, ( V T )*

Palindrome Language L={ w | w{0,1}* and w = wR } recursive definition basis , 0, 1 are palindromes. induction If w is a palindrome, so is 0w 0 and 1w1.

Palindrome Language L={ w | w{0,1}* and w = wR } definition with grammars or rules 1.  is a palindrome. 2. 0 is a palindrome. 3. 1 is a palindrome. 4. If w is a palindrome, so is 0w0. 5. If w is a palindrome, so is 1w1.

CFG & Palindrome Language L={ w | w{0,1}* and w = wR } 1.  is a P. 1. P   2. 0 is a P. 2. P  0 3. 1 is a P. 3. P  1 4. If w is a P, so is 0w0. 4. P  0P 0 事儿是一件事儿,只是换了另一种说法。或者采用了另一种语言。 5. If w is a P, so is 1w1. 5. P  1P 1

CFG of Palindrome Language L={ w | w{0,1}* and w = wR } CFG for palindromes on {0,1} R = ({S }, {0,1}, P, S ), P is defined as follow S   , S  0, S  1, S  0S 0, S  1S 1 Compact notation S   | 0 | 1 | 0S 0 | 1S 1

Example 7.1 L={ 0n1n | n  0 } R = ({S }, {0,1}, P, S ), P is defined as follow S   | 0S1

Example 7.2 L={ 0n1m | n  m } R = ({S,A,B,C }, {0,1}, P, S ), P is defined as follow S  AC | CB, C  0C1 |  A  A0 | 0 , B  1B | 1

Example 7.3 L={ w{0,1}* | w contains same number of 0’s and 1’s } R = ({S }, {0,1}, P, S ), P is defined as follow S   | 0S1 | 1S0 |SS

Example 7.4 L={w {0,1}*| n0(w)=n1(w) and n0(v)  n1(v) where v is any prefix of w } R = ({S }, {0,1}, P, S ), P is defined as follow S   | 0S1 | SS

Derivations and Recursive Inferences L={a2nbm | n  0, m  0 } R = ({S,A,B }, {a,b}, P, S ), P is defined as follow S  AB , A  |aaA , B  | Bb for w =aabb : SABaaABaaABbaaBbaaBbbaabb B Bb B  Bb S  AB A  aaA A   B  

Context-Free Language Let G=( V, T, S, P ) be context-free, then L(G) = {w | w T * and S  w } 

Left/Right Most Derivations L={a2nbm | n  0, m  0 } S  AB , A  |aaA , B  | Bb for w =aabb : SABaaABaaABbaaBbaaBbbaabb Left most : SABaaABaaBaaBbaaBbbaabb Right most : SABABbABbbAbbaaAbbaabb

Parse Tree Let G = ( V,T , S, P ) be a CFG. A tree is a parse tree for G if : 1. Each interior node is labeled by a variable in V 2. Each leaf is labeled by a symbol in T{}. Any -labeled leaf is the only child of its parent. 3. If an interior node is labeled A, and its children (from left to right) labeled x1,x2, ,xk, Then A  x1,x2, ,xk  P .

Parse Tree Example 7.5 L={ w | w{0,1}* and w = wR } S   | 0 | 1 | 0S 0 | 1S 1 S S 0 S 0 0 S 0 recursive inferences derivations 1 S 1 0 S 0  1 w=0110 w=00100

Ambiguity G = ({E, I }, {a, b, (, ), +, }, P, E ) EI |E +E |E E|(E), Ia | b Derivation for w = a a  a : EEEE+EEI+EEa+EE a+aa  EEEI+Ea+Ea+EEa+aa 

Ambiguity parse-tree for w = a a  a : E E E  E E  E E + E I I

Removing Ambiguity EI | E+E | EE | (E) , Ia | b ET|E+T, TF|TF, FI|(E) , Ia|b|Ia|Ib Left most derivation for w = a a  a : EETT+TF+T I+T a+Ta+TF a+FFa+IFa+aFa+aIa+aa ETTT(E)T(E+T)T(a+a)a 

Inherent Ambiguity What is inherent ambiguity A CFL L is said to be inherently ambiguous if every grammar that generates it is ambiguous. Example 7.6 Let L={ w | w{0,1}* and n0(w)=n1(w) } L is not inherently ambiguous ,because there is an unambiguous CFG : S   | 0S1 | 1S0 | 0S11S0 | 1S00S1

Example 7.7 L={anbncmdm | n1, m1} {anbmcmdn | n1, m1} The CFG for L is : SAB | C , AaAb | ab, BcBd | cd CaCd | aDd, DbDc | bc Let w= abcd , there are two left most derivations SAB abB abcd SC aDd abcd

Simplification of CFG Why & what : SA | B, A1CA | 1DE | , B1CB | 1DF, C1CC | 1DG | 0G, D1CD | 1DH | 0H, E0A, F0B, G, H1 -productions unit productions useless symbols and productions

Eliminating -productions * Variable A is said to be nullable if A  . Let G=(V,T,P,S) is a CFG If A → P, then A is nullable. If A → A1 A2  AkP, and Ai → P for i=1, ,k then A is nullable. Example 7.8 G : SAB, AaAA|, BbBB| A  A is nullable. SAB  S is nullable. B  B is nullable.

Eliminating unit productions Example 7.9 G : SA|B|0S1, A0A|0, B1B|1 S 0A|0|1B|1|0S1 A0A|0 B1B|1

Eliminating useless productions A symbol X is useful for a grammar G=(V,T,P,S), if there is a derivation for some wT* * * S  X  w * A symbol X is generating if X  w for some wT* * A symbol X is reachable if S  X for {,}(VT)*

Example 7.10 G : SAB|a, Ab S and A are generating , B is not. Eliminate B, that eliminate SAB, leaving Sa, Ab Now only S is reachable. So there leaves Sa. If eliminate non-reachable symbol first : SAB|a, Ab  SAB|a, Ab Then eliminate non-generating symbol : SAB|a, Ab  Sa, Ab

Example 7.11 G : SA | B, A1CA | 1DE |  B1CB | 1DF, C1CC| 1DG | 0G, D1CD | 1DH | 0H, E0A, F0B, G, H1 eliminating -productions the only one : A SA | B, A1CA | 1C | 1DE, B1CB | 1DF, C1CC | 1DG | 0G, D1CD | 1DH | 0H, E0A|0, F0B, G, H1

SA | B, A1CA | 1C | 1DE, B1CB | 1DF, C1CC | 1DG | 0G, D1CD | 1DH | 0H, E0A|0, F0B, G, H1 eliminating unit productions the only two : SA and SB S1CA | 1C | 1DE|1CB | 1DF, A1CA | 1C | 1DE, B1CB | 1DF, C1CC | 1DG | 0G, D1CD|1DH | 0H, E0A|0, F0B, G, H1

S1CA | 1C | 1DE|1CB | 1DF, A1CA | 1C | 1DE, B1CB | 1DF, C1CC | 1DG | 0G, D1CD|1DH | 0H, E0A|0, F0B, G, H1 eliminating useless symbols and productions S1DE ,A1DE , D1DH | 0H , E0A | 0, H1

Chomsky Normal Form(CNF) 1. A BC ; 2. A a . S1DE ,A1DE , D1DH | 0H , E0A | 0, H1 Chomsky normal form : SIE ,AIE, DIH|EH, EEA|0, IHD, H1 DIH|FH, EFA|0, F0

Chomsky Normal Form(CNF) Example 7.12 Convert following grammar to CNF SABa , Aaab , BAc

Greibach Normal Form(GNF) A  ax , where aT , xV Example 7.13 Convert following grammar to GNF SAB , AaA|bB|b , Bb Example 7.14 Convert following grammar to GNF S01S1|00

? ? ? eliminating -productions :   L ? Greibach normal form : A  a advantage ? Chomsky normal form : A  a |BC advantage ? left recursiveness A  A shortage ?