Formal Languages Simplifications of CFGs

Similar documents
FABER Formal Languages, Automata and Models of Computation

Normal Forms for Context-free Grammars

Closure Properties of Regular Languages

1.4 Nonregular Languages

CS 275 Automata and Formal Language Theory

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

CSC 311 Theory of Computation

CS 314 Principles of Programming Languages

CSC 473 Automata, Grammars & Languages 11/9/10

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

CS 275 Automata and Formal Language Theory

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

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

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

Automata and Languages

First Midterm Examination

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

Recursively Enumerable and Recursive. Languages

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

Lecture 6 Regular Grammars

1.3 Regular Expressions

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

Parse trees, ambiguity, and Chomsky normal form

First Midterm Examination

Lexical Analysis Finite Automate

CS 275 Automata and Formal Language Theory

Context-Free Grammars and Languages

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

Linearly Similar Polynomials

Exercises Chapter 1. Exercise 1.1. Let Σ be an alphabet. Prove wv = w + v for all strings w and v.

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

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

a b b a pop push read unread

Formal languages, automata, and theory of computation

Non-Deterministic Finite Automata

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

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER LANGUAGES AND COMPUTATION ANSWERS

Parsing and Pattern Recognition

Thoery of Automata CS402

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

Review of CFGs and Parsing I Context-free Languages and Grammars. Winter 2014 Costas Busch - RPI 1

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

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

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

1 From NFA to regular expression

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

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

CS 330 Formal Methods and Models

Convert the NFA into DFA

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

CSE : Exam 3-ANSWERS, Spring 2011 Time: 50 minutes

Informatics 2A: Lecture 20. Shay Cohen. 31 October 2017

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

NFAs and Regular Expressions. NFA-ε, continued. Recall. Last class: Today: Fun:

Refined interfaces for compositional verification

SEMANTIC ANALYSIS PRINCIPLES OF PROGRAMMING LANGUAGES. Norbert Zeh Winter Dalhousie University 1/28

State Minimization for DFAs

CS375: Logic and Theory of Computing

Designing finite automata II

THEOTY OF COMPUTATION

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

Harvard University Computer Science 121 Midterm October 23, 2012

CS 330 Formal Methods and Models

CS 330 Formal Methods and Models Dana Richards, George Mason University, Spring 2016 Quiz Solutions

f(x)dx . Show that there 1, 0 < x 1 does not exist a differentiable function g : [ 1, 1] R such that g (x) = f(x) for all

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

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

CMPSCI 250: Introduction to Computation. Lecture #31: What DFA s Can and Can t Do David Mix Barrington 9 April 2014

CMSC 330: Organization of Programming Languages

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

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

Let's start with an example:

PESIT SOUTHCAMPUS QUESTION BANK. Chapter 1 & 2 : Introduction to theory of computation and finite automata

Theory of Computation Regular Languages

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

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

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

CSCI FOUNDATIONS OF COMPUTER SCIENCE

Lecture 08: Feb. 08, 2019

CISC 4090 Theory of Computation

5.1 Definitions and Examples 5.2 Deterministic Pushdown Automata

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

Minimal DFA. minimal DFA for L starting from any other

Learning Moore Machines from Input-Output Traces

Line Integrals. Partitioning the Curve. Estimating the Mass

2. Lexical Analysis. Oscar Nierstrasz

HW3, Math 307. CSUF. Spring 2007.

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

Chart Parsing: the CYK Algorithm

Formal Languages and Automata

Nondeterminism and Nodeterministic Automata

Jim Lambers MAT 280 Spring Semester Lecture 26 and 27 Notes

Chapter 3 Solving Nonlinear Equations

Nondeterminism. Nondeterministic Finite Automata. Example: Moves on a Chessboard. Nondeterminism (2) Example: Chessboard (2) Formal NFA

Finite Automata-cont d

DFA Minimization and Applications

SWEN 224 Formal Foundations of Programming WITH ANSWERS

E 1 (n) = E 0 (n-1) E 0 (n) = E 0 (n-1)+e 0 (n-2) T(n -1)=2E 0 (n-2) + E 0 (n-3)

q 2 in δ 2. [4: 1 mark q 1 in δ 1 and q 2 and (q 1, q 2 ) (q 1, q 2) whenever q 1 for each component] (b) The resulting DFA:

Non-deterministic Finite Automata

Transcription:

Forml Lnguges implifictions of CFGs

ubstitution Rule Equivlent grmmr b bc ubstitute b bc bbc b 2

ubstitution Rule b bc bbc ubstitute b bc bbc bc Equivlent grmmr 3

In generl: xz y 1 ubstitute y 1 xz xy1z equivlent grmmr 4

Lnguge? 5

Nullble Vribles production: Nullble Vrible: 6

Removing Nullble Vribles Exmple Grmmr: Mb M Mb M Nullble vrible 7

Finl Grmmr Mb M Mb M ubstitute M Mb b M Mb M b 8

Unit-Productions Unit Production: (single vribles on both sides) 9

Removing Unit Productions Observtion: Is removed immeditely 10

Exmple Grmmr: bb 11

ubstitute bb bb 12

Remove bb bb 13

ubstitute bb bb 14

Remove repeted productions Finl grmmr bb bb 15

Lnguge? 16

Useless Productions b Useless Production ome derivtions never terminte... 17

nother grmmr: b Useless Production Not rechble from 18

In generl: contins only terminls if xy w wl(g) then vrible is useful otherwise, vrible is useless 19

production x is useless if ny of its vribles is useless b Productions Vribles useless useless useless useless C useless useless C D useless 20

Removing Useless Productions Exmple Grmmr: C C Cb 21

Remove useless productions 22

First: find ll vribles tht cn produce strings with only terminls Round 1: C {, } C Cb Round 2: {,, } 23

Keep only the vribles tht produce terminl symbols: {,, } (other vribles re useless) C C Cb Remove useless productions 24

econd: Find ll vribles rechble from Use Dependency Grph not rechble 25

Keep only the vribles rechble from (the other vribles re useless) Finl Grmmr Remove useless productions 26

Removing ll tep 1: Remove Nullble Vribles tep 2: Remove Unit-Productions tep 3: Remove Useless Vribles 27

Norml Forms for Context-free Grmmrs 28

Chomsky Norml Form Ech production hs form: C or vrible vrible terminl 29

Exmples: b Chomsky Norml Form Not Chomsky Norml Form 30

Conversion to Chomsky Norml Form Exmple: b c Not Chomsky Norml Form 31

32 c b Introduce vribles for terminls: c T b T T T T T T T c b c b c b T T T,,

33 Introduce intermedite vrible: c T b T T T T T T T c b c b c T b T T T T T T T V V c b c b 1 1 V 1

34 Introduce intermedite vrible: c T b T T T T T V V T T V V c b c b 2 2 1 1 V 2 c T b T T T T T T T V V c b c b 1 1

35 Finl grmmr in Chomsky Norml Form: c T b T T T T T V V T T V V c b c b 2 2 1 1 c b Initil grmmr

In generl: From ny context-free grmmr (which doesn t produce ) not in Chomsky Norml Form we cn obtin: n equivlent grmmr in Chomsky Norml Form 36

The Procedure First remove: Nullble vribles Unit productions 37

Then, for every symbol : dd production T In productions: replce with T New vrible: T 38

Replce ny production C C 1 2 C n with C V 1 1 V 1 C V 2 2 V n2 C n1 C n New intermedite vribles: V1, V2,, V n 2 39

Theorem: For ny context-free grmmr (which doesn t produce ) there is n equivlent grmmr in Chomsky Norml Form 40

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

exercise Find CNF for this grmmr: -> 00 11 -> C -> C -> epsilon (exercise 7.1.3, Hopcroft, Motwni, Ullmn) 42

Greibch Norml Form ll productions hve form: V V 1 2 V k k 0 terminl vribles 43

Exmples: c bb b b b Greibch Norml Form Not Greibch Norml Form 44

Conversion to Greibch Norml Form: T b T b bb T T T b b Greibch Norml Form 45

Theorem: For ny context-free grmmr (which doesn t produce ) there is n equivlent grmmr in Greibch Norml Form 46

Observtions Greibch norml forms re very good for prsing It is hrd to find the Greibch norml form of ny context-free grmmr 47

Try to compute Greibch Norml Form for grmmr in CNF exmple 48

Compilers 49

Progrm v = 5; if (v>5) x = 12 + v; while (x!=3) { x = x - 3; v = 10; }... Compiler Mchine Code dd v,v,0 cmp v,5 jmplt ELE THEN: dd x, 12,v ELE: WHILE: cmp x,3... 50

Compiler Lexicl nlyzer prser input progrm mchine code output 51

prser knows the grmmr of the progrmming lnguge 52

PROGRM TMT_LIT TMT Prser TMT_LIT TMT; TMT_LIT TMT; EXPR IF_TMT WHILE_TMT { TMT_LIT } EXPR IF_TMT WHILE_TMT EXPR + EXPR EXPR - EXPR INT if (EXPR) then TMT if (EXPR) then TMT else TMT while (EXPR) do TMT 53

The prser finds the derivtion of prticulr input input 10 + 2 * 5 Prser E -> E + E E * E INT derivtion E => E + E => E + E * E => 10 + E*E => 10 + 2 * E => 10 + 2 * 5 54

derivtion derivtion tree E E => E + E => E + E * E => 10 + E*E => 10 + 2 * E => 10 + 2 * 5 E 10 + E E * E 2 5 55

derivtion tree E mchine code E + E mult, 2, 5 10 E * E dd b, 10, 2 5 56

Prsing 57

input string Prser grmmr derivtion 58

Exmple: input bb Prser b b derivtion? 59

Prsing lgorithm? 60

Exhustive erch b b Phse 1: Find derivtion of b bb b ll possible derivtions of length 1 61

bb b b 62

Phse 2 b b b bb Phse 1 b + 2 more b b b b bb b bb b b 63

Phse 2 b b b bb b b b bb Phse 3 b bb bb 64

Finl result of exhustive serch (top-down prsing) Prser input b bb b derivtion b bb bb 65

Is exhustive serch good prsing lgorithm? 66

Time complexity of exhustive serch uppose there re no productions of the form Number of phses for string : w 2 w 67

For grmmr with k rules Time for phse 1: k k possible derivtions 68

Time for phse 2: k 2 k 2 possible derivtions 69

Time for phse : 2 w k 2 w k 2 w possible derivtions 70

Totl time needed for string : w k k 2 2 w k phse 1 phse 2 phse 2 w Extremely bd!!! 71

There exist fster lgorithms for specilized grmmrs -grmmr: x symbol string of vribles Pir (, ) ppers once 72

-grmmr exmple: b c Ech string hs unique derivtion b bc bcc 73

For -grmmrs: In the exhustive serch prsing there is only one choice in ech phse Time for phse: 1 Totl time for prsing string w : w 74

For generl context-free grmmrs: There exists prsing lgorithm tht prses string in time 3 w w (we will show this in the next clss) 75

The CYK Prser 76

Input: The CYK Membership lgorithm Grmmr G in Chomsky Norml Form tring w Output: find if wl(g) 77

Input exmple: The lgorithm Grmmr G: b tring : w bbb 78

bbb b b b b bb bb b bb bbb bb bbb bbb 79

b b b b bb bb b b bb bbb bb bbb bbb 80

b b b b b bb, b bb bbb bb bb bbb bbb 81

b b, b, bb b bb bbb, b bb b bb bbb, bbb, 82

Therefore: bbb L(G) Time Complexity: 3 w Observtion: The CYK lgorithm cn be esily converted to prser (bottom up prser) 83

The following slides re courtesy of Professor Ppp, University of Debrecen. 84

1,7 1,1 7,7 b b b

Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b

Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b

Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b

Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b

Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b

Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b

Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b

Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b

Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b

Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b

Grmmr CD C C,C,D C b C DD C b D D, C, D D,C,C,C b b b

,C Word is in lnguge if ppers here,d D, C, D D,C,C,C b b b

exercise Prse bb for this grmmr -> C -> -> CC b C -> (Hopcroft, Motwni, Ullmn, p301) 98