Normal Forms for Context-free Grammars

Similar documents
CS 275 Automata and Formal Language Theory

FABER Formal Languages, Automata and Models of Computation

Formal languages, automata, and theory of computation

a b b a pop push read unread

1.4 Nonregular Languages

Non Deterministic Automata. Linz: Nondeterministic Finite Accepters, page 51

Lecture 6 Regular Grammars

Convert the NFA into DFA

5.1 Definitions and Examples 5.2 Deterministic Pushdown Automata

Worked out examples Finite Automata

Formal Languages Simplifications of CFGs

Context-Free Grammars and Languages

CSC 311 Theory of Computation

Grammar. Languages. Content 5/10/16. Automata and Languages. Regular Languages. Regular Languages

First Midterm Examination

Designing finite automata II

Assignment 1 Automata, Languages, and Computability. 1 Finite State Automata and Regular Languages

Let's start with an example:

Non-Deterministic Finite Automata. Fall 2018 Costas Busch - RPI 1

Lexical Analysis Finite Automate

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

Theory of Computation Regular Languages. (NTU EE) Regular Languages Fall / 38

Nondeterminism and Nodeterministic Automata

Talen en Automaten Test 1, Mon 7 th Dec, h45 17h30

Closure Properties of Regular Languages

Theory of Computation Regular Languages

Regular Expressions (RE) Regular Expressions (RE) Regular Expressions (RE) Regular Expressions (RE) Kleene-*

Non-Deterministic Finite Automata

Compiler Design. Fall Lexical Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

1 Nondeterministic Finite Automata

Chapter Five: Nondeterministic Finite Automata. Formal Language, chapter 5, slide 1

CS 314 Principles of Programming Languages

Deterministic Finite Automata

12.1 Nondeterminism Nondeterministic Finite Automata. a a b ε. CS125 Lecture 12 Fall 2016

Revision Sheet. (a) Give a regular expression for each of the following languages:

Minimal DFA. minimal DFA for L starting from any other

Section: Other Models of Turing Machines. Definition: Two automata are equivalent if they accept the same language.

Non Deterministic Automata. Formal Languages and Automata - Yonsei CS 1

Java II Finite Automata I

Converting Regular Expressions to Discrete Finite Automata: A Tutorial

First Midterm Examination

CS103B Handout 18 Winter 2007 February 28, 2007 Finite Automata

Chapter 2 Finite Automata

Homework 3 Solutions

CS 330 Formal Methods and Models

Speech Recognition Lecture 2: Finite Automata and Finite-State Transducers

12.1 Nondeterminism Nondeterministic Finite Automata. a a b ε. CS125 Lecture 12 Fall 2014

Finite Automata. Informatics 2A: Lecture 3. John Longley. 22 September School of Informatics University of Edinburgh

CS375: Logic and Theory of Computing

Automata and Languages

Lexical Analysis Part III

Intermediate Math Circles Wednesday, November 14, 2018 Finite Automata II. Nickolas Rollick a b b. a b 4

Thoery of Automata CS402

Regular expressions, Finite Automata, transition graphs are all the same!!

Parse trees, ambiguity, and Chomsky normal form

Table of contents: Lecture N Summary... 3 What does automata mean?... 3 Introduction to languages... 3 Alphabets... 3 Strings...

Formal Languages and Automata

Speech Recognition Lecture 2: Finite Automata and Finite-State Transducers. Mehryar Mohri Courant Institute and Google Research

Tutorial Automata and formal Languages

a,b a 1 a 2 a 3 a,b 1 a,b a,b 2 3 a,b a,b a 2 a,b CS Determinisitic Finite Automata 1

For convenience, we rewrite m2 s m2 = m m m ; where m is repeted m times. Since xyz = m m m nd jxyj»m, we hve tht the string y is substring of the fir

Anatomy of a Deterministic Finite Automaton. Deterministic Finite Automata. A machine so simple that you can understand it in less than one minute

1 From NFA to regular expression

Non-deterministic Finite Automata

CMSC 330: Organization of Programming Languages

Some Theory of Computation Exercises Week 1

CS375: Logic and Theory of Computing

More general families of infinite graphs

CS 373, Spring Solutions to Mock midterm 1 (Based on first midterm in CS 273, Fall 2008.)

Finite Automata. Informatics 2A: Lecture 3. Mary Cryan. 21 September School of Informatics University of Edinburgh

Scanner. Specifying patterns. Specifying patterns. Operations on languages. A scanner must recognize the units of syntax Some parts are easy:

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. NFA for (a b)*abb.

CMSC 330: Organization of Programming Languages. DFAs, and NFAs, and Regexps (Oh my!)

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. Comparing DFAs and NFAs (cont.) Finite Automata 2

Agenda. Agenda. Regular Expressions. Examples of Regular Expressions. Regular Expressions (crash course) Computational Linguistics 1

CS 275 Automata and Formal Language Theory

Harvard University Computer Science 121 Midterm October 23, 2012

Overview HC9. Parsing: Top-Down & LL(1) Context-Free Grammars (1) Introduction. CFGs (3) Context-Free Grammars (2) Vertalerbouw HC 9: Ch.

CS 310 (sec 20) - Winter Final Exam (solutions) SOLUTIONS

Recursively Enumerable and Recursive. Languages

Parsing and Pattern Recognition

NFA DFA Example 3 CMSC 330: Organization of Programming Languages. Equivalence of DFAs and NFAs. Equivalence of DFAs and NFAs (cont.

Model Reduction of Finite State Machines by Contraction

Deterministic Finite-State Automata

CHAPTER 1 Regular Languages. Contents

Finite Automata-cont d

On Determinisation of History-Deterministic Automata.

CS415 Compilers. Lexical Analysis and. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CS 275 Automata and Formal Language Theory

I. Theory of Automata II. Theory of Formal Languages III. Theory of Turing Machines

11.1 Finite Automata. CS125 Lecture 11 Fall Motivation: TMs without a tape: maybe we can at least fully understand such a simple model?

More on automata. Michael George. March 24 April 7, 2014

Automata Theory 101. Introduction. Outline. Introduction Finite Automata Regular Expressions ω-automata. Ralf Huuck.

Chapter 1, Part 1. Regular Languages. CSC527, Chapter 1, Part 1 c 2012 Mitsunori Ogihara 1

Nondeterministic Automata vs Deterministic Automata

CS 301. Lecture 04 Regular Expressions. Stephen Checkoway. January 29, 2018

CSCI 340: Computational Models. Kleene s Theorem. Department of Computer Science

Lecture 09: Myhill-Nerode Theorem

5. (±±) Λ = fw j w is string of even lengthg [ 00 = f11,00g 7. (11 [ 00)± Λ = fw j w egins with either 11 or 00g 8. (0 [ ffl)1 Λ = 01 Λ [ 1 Λ 9.

Hybrid Control and Switched Systems. Lecture #2 How to describe a hybrid system? Formal models for hybrid system

Transcription:

Norml Forms for Context-free Grmmrs 1

Linz 6th, Section 6.2 wo Importnt Norml Forms, pges 171--178 2

Chomsky Norml Form All productions hve form: A BC nd A vrile vrile terminl 3

Exmples: S AS S AS S S AAS A SA A SA A A Chomsky Norml Form Not Chomsky Norml Form 4

Conversion to Chomsky Norml Form Exmple: S AB A B Ac Not Chomsky Norml Form 5

6 Ac B A AB S Introduce vriles for terminls: c A B A AB S c c c,,

7 Introduce intermedite vrile: c A B A AB S c c c A B A B V AV S c c 1 1 V 1

8 Introduce intermedite vrile: c A B V V A B V AV S c c 2 2 1 1 V 2 c A B A B V AV S c c 1 1

9 Finl grmmr in Chomsky Norml Form: c A B V V A B V AV S c c 2 2 1 1 Ac B A AB S Initil grmmr

In generl: From ny context-free grmmr not in Chomsky Norml Form we cn otin: An equivlent grmmr in Chomsky Norml Form 10

he Procedure First remove: Nullle vriles Unit productions 11

For every symol : Add production In productions: replce with New vrile: 12

Replce ny production A C C 1 2 C n with A V 1 C V 1 C 2 1 V 2 V n2 C n1 C n New intermedite vriles: V1, V2,, V n 2 13

heorem: For ny context-free grmmr G such tht the empty string is not in L(G), there is n equivlent grmmr in Chomsky Norml Form Linz, 6 th, heorem 6.6, pge 172. 14

Oservtions Chomsky norml forms re good for prsing nd proving theorems It is very esy to find the Chomsky norml form of ny context-free grmmr 15

Greich Norml Form All productions hve form: A V V 1 2 V k k 0 symol vriles 16

Exmples: S cab S S A A B S B Greich Norml Form Not Greich Norml Form 17

Conversion to Greich Norml Form: S S S S S S Greich Norml Form 18

heorem: For ny context-free grmmr G such tht the empty string is not in L(G), there is n equivlent grmmr in Greich Norml Form Linz, 6 th, heorem 6.7, pge 176. Proof not given ecuse it is too complicted. 19

Oservtions Greich norml forms re very good for prsing It is hrd to find the Greich norml form of ny context-free grmmr 20

An Appliction of Chomsky Norml Forms 21

he CYK Memership Algorithm J. Cocke, D. H. Younger, nd. Ksmi Input: Grmmr in Chomsky Norml Form G String w Output: find if w L(G) 22

Considers every possile consecutive susequence of letters nd sets K [i,j] if the sequence of letters strting from i to j cn e generted from the non-terminl K. Once it hs considered sequences of length 1, it goes on to sequences of length 2, nd so on. 23

For susequences of length 2 nd greter, it considers every possile prtition of the susequence into two hlves, nd checks to see if there is some production A-> BC such tht B mtches the first hlf nd C Mtches the second hlf. If so, it records A s mtching the whole susequence. Once this process is completed, the sentence is recognized y the grmmr if the entire string is mtched y the strt symol. 24

Input exmple: he Algorithm Grmmr : G S AB A BB A B AB B String : w 25

[0:1] [1:2] [2:3] [3:4] [4:5] [0:2] [1:3] [2:4] [3:5] [0:3] [1:4] [2:5] [0:4] [1:5] [0:5] 26

27

S A A B B AB BB AB A A B B B 28

S A A B B AB BB AB A A B S,B A B A B 29

S A A B B AB BB AB A S,B A S,B A B A S,B B A B A S,B S,B 30

herefore: L(G) ime Complexity: 3 w Oservtion: he CYK lgorithm cn e esily converted to prser 31

Pushdown Automt PDAs 32

Pushdown Automton -- PDA Input String Stck Sttes 33

Initil Stck Symol Stck Stck $ z ottom specil symol 34

he Sttes Input symol Pop symol Push symol, c q 1 q 2 35

, c q 1 q 2 input stck top Replce c e h $ e h $ 36

, c q 1 q 2 input stck c top Push e h $ e h $ 37

, q 1 q 2 input stck top Pop e h $ e h $ 38

, q 1 q 2 input stck top No Chnge e h $ e h $ 39

Non-Determinism, c q 2 q 1, c q 1 q 2, c q 3 trnsition hese re llowed trnsitions in Non-deterministic PDA (NPDA) 40

NPDA: Non-Deterministic PDA Exmple:,,,,, $ $ q 0 q 1 q 2 q 3 41

Execution Exmple: Input ime 0 $ Stck current stte,,,,, $ $ q q 1 q 2 q 3 0 42

Input ime 1 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 43

Input ime 2 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 44

Input ime 3 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 45

Input ime 4 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 46

Input ime 5 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 47

Input ime 6 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 48

Input ime 7 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 49

Input ime 8 $ Stck,, ccept,,, $ $ q 0 q 1 q 2 q 3 50

A string is ccepted if there is computtion such tht: All the input is consumed he lst stte is finl stte At the end of the computtion, we do not cre out the stck contents 51

he input string is ccepted y the NPDA:,,,,, $ $ q 0 q 1 q 2 q 3 52

In generl, L n n { : n 0} is the lnguge ccepted y the NPDA:,,,,, $ $ q 0 q 1 q 2 q 3 53

Another NPDA exmple NPDA M L( M ) R { ww },,,, q 0,, $ $ q 1 q 2 54

Execution Exmple: Input ime 0 $,, Stck,, q 0,, $ $ q 1 q 2 55

Input ime 1 $,, Stck,, q 0,, $ $ q 1 q 2 56

ime 2 Input $,, Stck,, q 0,, $ $ q 1 q 2 57

ime 3 Input Guess the middle of string $,, Stck,, q 0,, $ $ q 1 q 2 58

ime 4 Input $,, Stck,, q 0,, $ $ q 1 q 2 59

Input ime 5 $,, Stck,, q 0,, $ $ q q 2 1 60

Input ime 6 $,, Stck,, ccept q 0, q 1, $ $ q 2 61

Rejection Exmple: Input ime 0 $,, Stck,, q 0,, $ $ q 1 q 2 62

Input ime 1 $,, Stck,, q 0,, $ $ q 1 q 2 63

Input ime 2 $,, Stck,, q 0,, $ $ q 1 q 2 64

Input ime 3 Guess the middle of string $,, Stck,, q 0,, $ $ q 1 q 2 65

Input ime 4 $,, Stck,, q 0,, $ $ q 1 q 2 66

Input ime 5 here is no possile trnsition. Input is not consumed $,, Stck,, q 0,, $ $ q q 2 1 67

Another computtion on sme string: Input ime 0 $,, Stck,, q 0,, $ $ q 1 q 2 68

Input ime 1 $,, Stck,, q 0,, $ $ q 1 q 2 69

Input ime 2 $,, Stck,, q 0,, $ $ q 1 q 2 70

Input ime 3 $,, Stck,, q 0,, $ $ q 1 q 2 71

Input ime 4 $,, Stck,, q 0,, $ $ q 1 q 2 72

Input ime 5 No finl stte is reched $,, Stck,, q 0,, $ $ q 1 q 2 73

here is no computtion tht ccepts string L(M ),,,, q 0,, $ $ q 1 q 2 74