Automata Theory CS F-08 Context-Free Grammars

Similar documents
Lecture 11 Context-Free Languages

Context-Free Grammars and Languages

Context Free Languages. Automata Theory and Formal Grammars: Lecture 6. Languages That Are Not Regular. Non-Regular Languages

Solutions to Problem Set 3

1. (a) Explain the procedure to convert Context Free Grammar to Push Down Automata.

Automata Theory CS F-04 Non-Determinisitic Finite Automata

60-354, Theory of Computation Fall Asish Mukhopadhyay School of Computer Science University of Windsor

CS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF)

Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where

Homework 4 Solutions. 2. Find context-free grammars for the language L = {a n b m c k : k n + m}. (with n 0,

TAFL 1 (ECS-403) Unit- III. 3.1 Definition of CFG (Context Free Grammar) and problems. 3.2 Derivation. 3.3 Ambiguity in Grammar

Computational Models - Lecture 4 1

Theory of Computation - Module 3

Context Free Languages and Grammars

(b) If G=({S}, {a}, {S SS}, S) find the language generated by G. [8+8] 2. Convert the following grammar to Greibach Normal Form G = ({A1, A2, A3},

Chapter 5: Context-Free Languages

Concordia University Department of Computer Science & Software Engineering

AC68 FINITE AUTOMATA & FORMULA LANGUAGES JUNE 2014

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

EXAMPLE CFG. L = {a 2n : n 1 } L = {a 2n : n 0 } S asa aa. L = {a n b : n 0 } L = {a n b : n 1 } S asb ab S 1S00 S 1S00 100

Automata Theory CS F-13 Unrestricted Grammars

Automata Theory. CS F-10 Non-Context-Free Langauges Closure Properties of Context-Free Languages. David Galles

CS A Term 2009: Foundations of Computer Science. Homework 2. By Li Feng, Shweta Srivastava, and Carolina Ruiz.

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Context-Free Grammar

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

Grammars and Context Free Languages

Pushdown automata. Twan van Laarhoven. Institute for Computing and Information Sciences Intelligent Systems Radboud University Nijmegen

Automata Theory Final Exam Solution 08:10-10:00 am Friday, June 13, 2008

FLAC Context-Free Grammars

Section 1 (closed-book) Total points 30

Context Free Languages (CFL) Language Recognizer A device that accepts valid strings. The FA are formalized types of language recognizer.

Note: In any grammar here, the meaning and usage of P (productions) is equivalent to R (rules).

VTU QUESTION BANK. Unit 1. Introduction to Finite Automata. 1. Obtain DFAs to accept strings of a s and b s having exactly one a.

CFG Simplification. (simplify) 1. Eliminate useless symbols 2. Eliminate -productions 3. Eliminate unit productions

FABER Formal Languages, Automata. Lecture 2. Mälardalen University

Context-free Grammars and Languages

Context-Free Grammars. 2IT70 Finite Automata and Process Theory

Grammars and Context Free Languages

Parsing. Context-Free Grammars (CFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 26

ÖVNINGSUPPGIFTER I SAMMANHANGSFRIA SPRÅK. 17 april Classrum Edition

Part 4 out of 5 DFA NFA REX. Automata & languages. A primer on the Theory of Computation. Last week, we showed the equivalence of DFA, NFA and REX

Finite Automata Theory and Formal Languages TMV026/TMV027/DIT321 Responsible: Ana Bove

Introduction to Theory of Computing

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

Chapter 1. Formal Definition and View. Lecture Formal Pushdown Automata on the 28th April 2009

The View Over The Horizon

Formal Languages, Grammars and Automata Lecture 5

CS375: Logic and Theory of Computing

Author: Vivek Kulkarni ( )

Computational Models - Lecture 4 1

download instant at Assume that (w R ) R = w for all strings w Σ of length n or less.

Theory of Computation (Classroom Practice Booklet Solutions)

Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs

Learning Context Free Grammars with the Syntactic Concept Lattice

Properties of Context-Free Languages

Notes for Comp 497 (454) Week 10

5/10/16. Grammar. Automata and Languages. Today s Topics. Grammars Definition A grammar G is defined as G = (V, T, P, S) where:

5 Context-Free Languages

Context-Free Grammars (and Languages) Lecture 7

St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Context-Free Grammars and Languages. Reading: Chapter 5

Homework 4. Chapter 7. CS A Term 2009: Foundations of Computer Science. By Li Feng, Shweta Srivastava, and Carolina Ruiz

In English, there are at least three different types of entities: letters, words, sentences.

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Notes for Comp 497 (Comp 454) Week 10 4/5/05

Recitation 4: Converting Grammars to Chomsky Normal Form, Simulation of Context Free Languages with Push-Down Automata, Semirings

Chapter 4. Regular Expressions. 4.1 Some Definitions

Theory of Computer Science

Pushdown Automata. Reading: Chapter 6

INSTITUTE OF AERONAUTICAL ENGINEERING

Einführung in die Computerlinguistik

Talen en Compilers. Johan Jeuring , period 2. October 29, Department of Information and Computing Sciences Utrecht University

Problem Session 5 (CFGs) Talk about the building blocks of CFGs: S 0S 1S ε - everything. S 0S0 1S1 A - waw R. S 0S0 0S1 1S0 1S1 A - xay, where x = y.

Context Free Grammars

Question Bank UNIT I

CS500 Homework #2 Solutions

Automata & languages. A primer on the Theory of Computation. Laurent Vanbever. ETH Zürich (D-ITET) October,

Part 3 out of 5. Automata & languages. A primer on the Theory of Computation. Last week, we learned about closure and equivalence of regular languages

5.A Examples of Context-Free Grammars 3.1 and 3.2 of Du & Ko s book(pp )

Computational Models - Lecture 4

Fundamentele Informatica II

Chapter 6. Properties of Regular Languages

LR2: LR(0) Parsing. LR Parsing. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

Computational Models - Lecture 3

Chomsky Normal Form and TURING MACHINES. TUESDAY Feb 4

Solution. S ABc Ab c Bc Ac b A ABa Ba Aa a B Bbc bc.

CS311 Computational Structures Regular Languages and Regular Expressions. Lecture 4. Andrew P. Black Andrew Tolmach

CISC 4090 Theory of Computation

Properties of Context-free Languages. Reading: Chapter 7

REGular and Context-Free Grammars

Theory Bridge Exam Example Questions

Context-Free Grammars. 2IT70 Finite Automata and Process Theory

Homework. Context Free Languages. Announcements. Before We Start. Languages. Plan for today. Final Exam Dates have been announced.

Theory of Computation

Harvard CS 121 and CSCI E-207 Lecture 10: Ambiguity, Pushdown Automata

Context-Free Grammars: Normal Forms

An automaton with a finite number of states is called a Finite Automaton (FA) or Finite State Machine (FSM).

CMSC 330: Organization of Programming Languages

Transcription:

Automata Theory CS411-2015F-08 Context-Free Grammars David Galles Department of Computer Science University of San Francisco

08-0: Context-Free Grammars Set of Terminals (Σ) Set of Non-Terminals Set of Rules, each of the form: <Non-Terminal> <Terminals & Non-Terminals> Special Non-Terminal Initial Symbol

08-1: Generating Strings with CFGs Start with the initial symbol Repeat: Pick any non-terminal in the string Replace that non-terminal with the right-hand side of some rule that has that non-terminal as a left-hand side Until all elements in the string are terminals

08-2: CFG Example S as S Bb B cb B ǫ Generating a string: S replace S with as as replace S wtih Bb abb replace B wtih cb acbb replace B wtih ǫ acb Final String

08-3: CFG Example S as S Bb B cb B ǫ Generating a string: S replace S with as as replace S wtih as aas replace S wtih Bb aabb replace B wtih cb aacbb replace B wtih cb aaccbb replace B wtih ǫ aaccb Final String

08-4: CFG Example S as S Bb B cb B ǫ Regular Expression equivalent to this CFG:

08-5: CFG Example S as S Bb B cb B ǫ Regular Expression equivalent to this CFG: a c b

08-6: CFG Example CFG for L = {0 n 1 n : n > 0}

08-7: CFG Example CFG for L = {0 n 1 n : n > 0} S 0S1 or S 0S1 01 S 01 (note can write: A α A β as A α β ) (examples: 01, 0011, 000111)

08-8: CFG Formal Definition G = (V,Σ,R,S) V = Set of symbols, both terminals & non-terminals Σ V set of terminals (alphabet for the language being described) R ((V Σ) V ) Finite set of rules S (V Σ) Start symbol

08-9: CFG Formal Definition Example: S 0S1 S 01 Set theory Definition: G = (V,Σ,R,S) V = {S,0,1} Σ V = {0,1} R ((V Σ) V ) = {(S,0S0),(S,01)} S (V Σ) = S

08-10: Derivation A Derivation is a listing of how a string is generated showing what the string looks like after every replacement. S AB A aa ǫ B bb ǫ S AB aab aabb abb abbb abb

08-11: Parse Tree A Parse Tree is a graphical representation of a derivation. S A B S AB aab aabb abb abbb abb a A b B ε b B ε

08-12: Parse Tree A Parse Tree is a graphical representation of a derivation. S A B a A b B S AB AbB aabb aaabb aaab aab a A A ε ε

08-13: Fun with CFGs Create a Context-Free Grammar for all strings over {a,b} which contain the substring aba

08-14: Fun with CFGs Create a Context-Free Grammar for all strings over {a,b} which contain the substring aba S AabaA A aa A ba A ǫ Give a parse tree for the string: bbabaa

08-15: Fun with CFGs Create a Context-Free Grammar for all strings over {a,b} that begin or end with the substring bba (inclusive or)

08-16: Fun with CFGs Create a Context-Free Grammar for all strings over {a,b} that begin or end with the substring bba (inclusive or) S bbaa S Abba A ba A aa A ǫ

08-17: L CFG The Context-Free Languages, L CFG, is the set of all languages that can be described by some CFG: L CFG = {L : CFG G L[G] = L} We already know L CFG L REG (why)? L REG L CFG?

08-18: L REG L CFG We will prove L REG L CFG in two different ways: Prove by induction that, given any regular expression r, we create a CFG G such that L[G] = L[r] Given any NFA M, we create a CFG G such that L[G] = L[M]

08-19: L REG L CFG To Prove: Given any regular expression r, we can create a CFG G such that L[G] = L[r] By induction on the structure of r

08-20: L REG L CFG Base Cases: r = a, a Σ

08-21: L REG L CFG Base Cases: S a r = a, a Σ

08-22: L REG L CFG Base Cases: r = ǫ

08-23: L REG L CFG Base Cases: S ǫ r = ǫ

08-24: L REG L CFG Base Cases: r =

08-25: L REG L CFG Base Cases: r = S SS

08-26: L REG L CFG Recursive Cases: r = (r 1 r 2 ) L[G 1 ] = L[r 1 ], Start symbol of G 1 = S 1 L[G 2 ] = L[r 2 ], Start symbol of G 2 = S 2

08-27: L REG L CFG Recursive Cases: r = (r 1 r 2 ) L[G 1 ] = L[r 1 ], Start symbol of G 1 = S 1 L[G 2 ] = L[r 2 ], Start symbol of G 2 = S 2 G = all rules from G 1 and G 2, plus plus new non-terminal S, and new rule: S S 1 S 2 New start symbol S

08-28: L REG L CFG Recursive Cases: r = (r 1 +r 2 ) L[G 1 ] = L[r 1 ], Start symbol of G 1 = S 1 L[G 2 ] = L[r 2 ], Start symbol of G 2 = S 2

08-29: L REG L CFG Recursive Cases: r = (r 1 +r 2 ) L[G 1 ] = L[r 1 ], Start symbol of G 1 = S 1 L[G 2 ] = L[r 2 ], Start symbol of G 2 = S 2 G = all rules from G 1 and G 2, plus new non-terminal S, and new rules: S S 1 S S 2 Start symbol = S

08-30: L REG L CFG Recursive Cases: r = (r 1) L[G 1 ] = L[r 1 ], Start symbol of G 1 = S 1

08-31: L REG L CFG Recursive Cases: r = (r 1) L[G 1 ] = L[r 1 ], Start symbol of G 1 = S 1 G = all rules from G 1, plus new non-terminal S, and new rules: S S 1 S S ǫ Start symbol = S (Example)

08-32: L REG L CFG II Given any NFA M = (K,Σ,,s,F) Create a grammar G = (V,Σ,R,S) such that L[G] = L[M] Idea: Derivations like backward NFA configurations, showing past instead of future Example for all strings over {a, b} that contain aa, not bb

08-33: L REG L CFG II M = (K,Σ,,s,F) G = (V,Σ,R,S) V Σ R S

08-34: L REG L CFG II M = (K,Σ,,s,F) G = (V,Σ,R,S) V = K Σ Σ = Σ R = {(q 1 aq 2 ) : q 1,q 2 K (and V ), a Σ,((q 1,a),q 2 ) } {(q ǫ) : q F} S = s (Example)

08-35: CFG Ambiguity A CFG is ambiguous if there exists at least one string generated by the grammar that has > 1 different parse tree Previous CFG is ambiguous (examples) S AabaA A aa A ba A ǫ

08-36: CFG Ambiguity Consider the following CFG: E E +E E E E E N N 0 1 2 3 4 5 6 7 8 9 Is this CFG ambiguous? Why is this a problem?

08-37: CFG Ambiguity E E +E E E E E N N 0 1 2 3 4 5 6 7 8 9 E E E + E E * E N E * E 3 N N E + E N N N 5 4 5 3 4

08-38: CFG Ambiguity E E +E E E E E N N 0 1 2 3 4 5 6 7 8 9 If all we care about is removing ambiguity, there is a (relatively) easy way to make this unambiguous (make all operators right-associative)

08-39: CFG Ambiguity E E +E E E E E N N 0 1 2 3 4 5 6 7 8 9 Non-ambiguous: E N N +E N E N E N 0 1 2 3 4 5 6 7 8 9 If we were writing a compiler, would this be a good CFG? How can we get correct associativity

08-40: CFG Ambiguity Ambiguous: E E +E E E E E N N 0 1 2 3 4 5 6 7 8 9 Unambiguous: E E +T E T T T T N N N 0 1 2 3 4 5 6 7 8 9 Can add parentheses, other operators, etc. (More in Compilers)

08-41: Fun with CFGs Create a CFG for all strings over {(,)} that form balanced parenthesis () ()() (()())((()()())) ((((()))))

08-42: Fun with CFGs Create a CFG for all strings over {(,)} that form balanced parenthesis S (S) S SS S ǫ Is this grammar ambiguous?

08-43: Fun with CFGs Create a CFG for all strings over {(,)} that form balanced parenthesis S (S) S SS S ǫ Is this grammar ambiguous? YES! (examples)

08-44: Fun with CFGs Create an unambiguous CFG for all strings over {(,)} that form balanced parenthesis

08-45: Fun with CFGs Create an unambiguous CFG for all strings over {(,)} that form balanced parenthesis S AS S ǫ A (S)

08-46: Ambiguous Languages A language L is ambiguous if all CFGs G that generate it are ambiguous Example: L 1 = {a i b i c j d j i,j > 0} L 2 = {a i b j c j d i i,j > 0} L 3 = L 1 L 2 L 3 is inherently ambiguous (Create a CFG for L 3 )

08-47: Ambiguous Languages L 1 = {a i b i c j d j i,j > 0} L 2 = {a i b j c j d i i,j > 0} L 3 = L 1 L 2 S S 1 S 2 S 1 AB A aab ab B cbd cd S 2 as 2 d acd C bcc bc What happens when i = j?

08-48: (More) Fun with CFGs Create an CFG for all strings over {a, b} that have the same number of a s as b s (can be ambiguous)

08-49: (More) Fun with CFGs Create an CFG for all strings over {a, b} that have the same number of a s as b s (can be ambiguous) S asb S bsa S SS S ǫ

08-50: (More) Fun with CFGs Create an CFG for L = {ww R : w (a+b) }

08-51: (More) Fun with CFGs Create an CFG for L = {ww R : w (a+b) } S asa S bsb S ǫ

08-52: (More) Fun with CFGs Create an CFG for all palindromes over {a,b}. That is, create a CFG for: L = {w : w (a+b),w = w R }

08-53: (More) Fun with CFGs Create an CFG for all palindromes over {a,b}. That is, create a CFG for: L = {w : w (a+b),w = w R } S asa S bsb S ǫ S a S b

08-54: (More) Fun with CFGs Create an CFG for L = {a i b j c k : j > i+k}

08-55: (More) Fun with CFGs Create an CFG for L = {a i b j c k : j > i+k} HINT: We may wish to break this down into 3 different langauges...

08-56: (More) Fun with CFGs Create an CFG for L = {a i b j c k : j > i+k} S ABC A aab A ǫ B bb B b C bcc ǫ

08-57: (More) Fun with CFGs Create an CFG for all strings over {0, 1} that have the an even number of 0 s and an odd number of 1 s. HINT: It may be easier to come up with 4 CFGs even 0 s, even 1 s, odd 0 s odd 1 s, even 0 s odd 1 s, odd 1 s, even 0 s and combine them...

08-58: (More) Fun with CFGs Create an CFG for all strings over {0, 1} that have the an even number of 0 s and an odd number of 1 s. S 1 = Even 0 s Even 1 s S 2 = Even 0 s Odd 1 s S 3 = Odd 0 s Even 1 s S 4 = Odd 0 s Odd 1 s S 1 0S 3 1S 2 S 2 0S 4 1S 1 S 3 0S 1 1S 4 S 4 0S 2 1S 3