Grammars and Context-free Languages; Chomsky Hierarchy

Similar documents
Grammars and Context Free Languages

Grammars and Context Free Languages

Formal Languages, Grammars and Automata Lecture 5

Context-Free Grammars and Languages. Reading: Chapter 5

Languages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write:

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

CISC 4090 Theory of Computation

Computability and Complexity

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

Lecture 11 Context-Free Languages

FLAC Context-Free Grammars

Context Free Grammars

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

Foundations of Informatics: a Bridging Course

Harvard CS 121 and CSCI E-207 Lecture 9: Regular Languages Wrap-Up, Context-Free Grammars

Introduction to Theory of Computing

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

CISC 4090 Theory of Computation

Section 1 (closed-book) Total points 30

CISC4090: Theory of Computation

Automata Theory CS F-08 Context-Free Grammars

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

Outline. CS21 Decidability and Tractability. Machine view of FA. Machine view of FA. Machine view of FA. Machine view of FA.

CA320 - Computability & Complexity

Context-Free Grammars and Languages

CA320 - Computability & Complexity

(NB. Pages are intended for those who need repeated study in formal languages) Length of a string. Formal languages. Substrings: Prefix, suffix.

Automata Theory CS F-13 Unrestricted Grammars

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

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

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

Computational Models - Lecture 4 1

Context Sensitive Grammar

Solutions to Problem Set 3

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Context-Free Grammar

(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},

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

Chapter 5: Context-Free Languages

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

CS Lecture 29 P, NP, and NP-Completeness. k ) for all k. Fall The class P. The class NP

Chomsky Normal Form and TURING MACHINES. TUESDAY Feb 4

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

Computational Models - Lecture 3

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

COMP-330 Theory of Computation. Fall Prof. Claude Crépeau. Lec. 10 : Context-Free Grammars

Theory of Computation - Module 3

The View Over The Horizon

Finite Automata and Formal Languages TMV026/DIT321 LP Useful, Useless, Generating and Reachable Symbols

The Parser. CISC 5920: Compiler Construction Chapter 3 Syntactic Analysis (I) Grammars (cont d) Grammars

MA/CSSE 474 Theory of Computation

This lecture covers Chapter 5 of HMU: Context-free Grammars

The Unsolvability of the Halting Problem. Chapter 19

CPS 220 Theory of Computation

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

Computational Models - Lecture 4

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

CPSC 313 Introduction to Computability

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

CS 275 Automata and Formal Language Theory. Disclaimer. Course Notes Part II: The Recognition Problem (II) Chapter II.1: The Chomsky Hierarchy (12.

INSTITUTE OF AERONAUTICAL ENGINEERING

5 Context-Free Languages

Computational Models - Lecture 4 1

Context-Free Grammars (and Languages) Lecture 7

AC68 FINITE AUTOMATA & FORMULA LANGUAGES JUNE 2014

CMPT-825 Natural Language Processing. Why are parsing algorithms important?

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

CSCI Compiler Construction

Sheet 1-8 Dr. Mostafa Aref Format By : Mostafa Sayed

Theory of Computation (II) Yijia Chen Fudan University

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.

Introduction and Motivation. Introduction and Motivation. Introduction to Computability. Introduction and Motivation. Theory. Lecture5: Context Free

Suppose h maps number and variables to ɛ, and opening parenthesis to 0 and closing parenthesis

Introduction to Computers & Programming

Parsing with Context-Free Grammars

Theory Bridge Exam Example Questions

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

St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Theory of Computation - Module 4

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

Theory of Computation (IV) Yijia Chen Fudan University

On Parsing Expression Grammars A recognition-based system for deterministic languages

CFLs and Regular Languages. CFLs and Regular Languages. CFLs and Regular Languages. Will show that all Regular Languages are CFLs. Union.

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

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

Grade 6 Math Circles October 20/21, Formalism and Languages: Beyond Regular Languages

CS375: Logic and Theory of Computing

UNRESTRICTED GRAMMARS

Context Free Languages and Grammars

CPSC 421: Tutorial #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)

Primitive recursive functions: decidability problems

Pushdown Automata. Reading: Chapter 6

SCHEME FOR INTERNAL ASSESSMENT TEST 3

6.8 The Post Correspondence Problem

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

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

Transcription:

Regular and Context-free Languages; Chomsky Hierarchy H. Geuvers Institute for Computing and Information Sciences Version: fall 2015 H. Geuvers Version: fall 2015 Huygens College 1 / 23

Outline Regular Regular H. Geuvers Version: fall 2015 Huygens College 2 / 23

Generating words Regular Example: S O E E λ aea beb O a b aoa bob Productions, always start with S S E aea abeba abba S E beb baeab babebab babaeabab babaabab S O bob bab S O bob baoab babab We can generate exactly the set of words w with w = w R (w is a palindrome) H. Geuvers Version: fall 2015 Huygens College 4 / 23

Context-free grammar Regular Let Σ be a finite alphabet. Def. A context-free grammar (CFG) G = V, S, P over Σ consists of V, a set of non-terminal symbols, S V, a start symbol, P, a set of production rules of the form Notation: X w, where X V and w (V Σ). is shorthand for three rules: E λ aea beb E λ, E aea, E beb. H. Geuvers Version: fall 2015 Huygens College 5 / 23

Context-free language Regular Using G, a language is generated using a relation ( produces ) defined as follows (where u, v, w are arbitrary elements of (Σ V ) ) X w implies uxv uwv u v, v w implies u w The language generated by G is L(G) = {w Σ S w}. A language L is context-free if L = L(G) for some context-free G. H. Geuvers Version: fall 2015 Huygens College 6 / 23

Leftmost derivations Regular There can be many ways to produce a word w L(G). G: S asb λ, B b Derivations of aabb: d 1 : S asb aasbb aabb aabb aabb d 2 : S asb asb aasbb aasbb aabb d 3 : S asb asb aasbb aabb aabb Derivation d 1 is leftmost, d 2 is rightmost, d 3 is neither. Def. A derivation is leftmost if in each step a rule is applied to the leftmost non-terminal. Lemma. For all grammars G and words w, w L(G) iff there is a leftmost derivation of w. Derivation/parse trees (on blackboard). H. Geuvers Version: fall 2015 Huygens College 7 / 23

Ambiguity Regular Def. A context-free grammar G is unambiguous if for each w L(G) there exists a unique leftmost derivation of w in G. Otherwise G is ambiguous. Equivalently: A context-free grammar is ambiguous if there is a word with two different parse trees. Example: dangling else S print E if E then S else S if E then S E a b H. Geuvers Version: fall 2015 Huygens College 8 / 23

Natural language Regular S = sentence noun-phrase verb-phrase. sentence noun-phrase verb-phrase object-phrase. noun-phrase name article noun name John Jill noun bicycle mango article a the verb-phrase verb adverb verb verb eats rides adverb slowly frequently adjective-list adjective adjective-list λ adjective big juicy yellow object-phrase adjective-list name object-phrase article adjective-list noun Jill frequently eats a juicy yellow mango. belongs to this language H. Geuvers Version: fall 2015 Huygens College 9 / 23

Regular Some context-free languages Is there a grammar G 2 such that L(G 2 ) = {a n b n n 0}? G 2 : S λ asb All possible derivations S asb aasbb a n Sb n λ ab aabb a n b n What is a grammar G 3 such that L(G 3 ) = {a n b n n > 0}? G 3 : S ab asb H. Geuvers Version: fall 2015 Huygens College 10 / 23

Regular More context-free languages Let Σ = {a, b, c} Claim: L = {a n b m c 2n+1 m, n 0} is context-free. Let us first show that L = {a n c 2n+1 m, n 0} is context-free. For L use S c ascc For L use S Bc ascc B λ bb Fact: {a n b n c n n 0} is not context-free. H. Geuvers Version: fall 2015 Huygens College 11 / 23

Regular languages Regular Let Σ be a finite alphabet. Def. A right-linear grammar G = V, S, P over Σ is a context-free grammar where all rules are of the following shape where X, Y V and w Σ X λ X wy Theorem. A language is regular language iff it can be generated by a right-linear grammar. Corollary. All regular languages are context-free. H. Geuvers Version: fall 2015 Huygens College 13 / 23

Regular Context-sensitive languages A context-free grammar has rules like: X w With X V, w (Σ V ). A context-sensitive grammar has rules like: αx β αwβ With X V, α, β, w (Σ V ), w λ. Context-sensitive grammars generate context-sensitive languages. H. Geuvers Version: fall 2015 Huygens College 15 / 23

Regular Context-sensitive languages Example: {a n b n c n n > 0} S abc asbc CB XB XB XC XC BC ab ab bb bb bc bc cc cc Note that this is a context-sensitive language. A crucial point is that CB BC and thereby, e.g. S aabcbc aabbcc aabbcc H. Geuvers Version: fall 2015 Huygens College 16 / 23

Regular Enumerable and computable languages An unrestricted grammar has rules like: u v With u, v (Σ V ). Recognized by Turing machines. Unrestricted grammars generate enumerable languages. A languages is computable if both L and L = Σ L are enumerable. Example: {a n n is not prime}. H. Geuvers Version: fall 2015 Huygens College 17 / 23

Regular {w w describes a grammar for L },... {a n n is prime},... {a n b n c n n > 0},... {a n b n n > 0},... {a n n > 0},... Regular Context-free Context-sensitive Computable Enumerable H. Geuvers Version: fall 2015 Huygens College 18 / 23

Trade-offs Regular Bigger classes of languages: More languages can be described. But, you can say less about them. w L? time memory L 1 = L 2? Regular yes w const. yes Context-free yes w 3 w 2 no Context-sensitive yes 2 w w k no Computable yes no Enumerable if w L no H. Geuvers Version: fall 2015 Huygens College 19 / 23

Regular Programming languages Most programming languages are context-free. There are tools to automatically build: a lexical analyzer ( lexer ) from regular expressions. a parser from a CFG. H. Geuvers Version: fall 2015 Huygens College 20 / 23

Lindenmayer systems Regular Model of organism growth. Example: A AB B A Start symbol A. Expand once per iteration 0 A 1 AB 2 ABA 3 ABAAB 4 ABAABABA... H. Geuvers Version: fall 2015 Huygens College 21 / 23

Lindenmayer systems Regular Drawing a Lindenmayer system: F: move forward +: rotate clockwise -: rotate counter clockwise [ ]: branch Example: F F + F F + F 0 F 1 F+F--F+F 2 F+F--F+F+F+F--F+F--F+F--F+F+F+F--F+F 3... H. Geuvers Version: fall 2015 Huygens College 22 / 23

Lindenmayer systems Regular S F [+S][ S] S F [[S] + S] + F [+FS] S F FF H. Geuvers Version: fall 2015 Huygens College 23 / 23