CS 44 – Jan. 29 Expression grammars –Associativity √ –Precedence CFG for entire language (handout) CYK algorithm –General technique for testing for acceptance.

Slides:



Advertisements
Similar presentations
lec02-parserCFG March 27, 2017 Syntax Analyzer
Advertisements

CSCI 3130: Formal Languages and Automata Theory Tutorial 5
Theorem 7.16: Every CFL is a member of P Proof: Let G be a Chomsky normal form grammar for language L. The following O(n 3 ) algorithm decides whether.
The Pumping Lemma for CFL’s
Closure Properties of CFL's
CYK Parser Von Carla und Cornelia Kempa. Overview Top-downBottom-up Non-directional methods Unger ParserCYK Parser.
101 The Cocke-Kasami-Younger Algorithm An example of bottom-up parsing, for CFG in Chomsky normal form G :S  AB | BB A  CC | AB | a B  BB | CA | b C.
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.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
The CYK Algorithm David Rodriguez-Velazquez CS – 6800 Summer I
Chapter 4 Normal Forms for CFGs Chomsky Normal Form n Defn A CFG G = (V, , P, S) is in chomsky normal form if each rule in G has one of.
CS5371 Theory of Computation
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
CS 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 11, 2006.
Lecture Note of 12/22 jinnjy. Outline Chomsky Normal Form and CYK Algorithm Pumping Lemma for Context-Free Languages Closure Properties of CFL.
Transparency No. P2C4-1 Formal Language and Automata Theory Part II Chapter 4 Parse Trees and Parsing.
1 CSC 3130: Automata theory and formal languages Tutorial 4 KN Hung Office: SHB 1026 Department of Computer Science & Engineering.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
1 Module 32 Chomsky Normal Form (CNF) –4 step process.
January 23, 2015CS21 Lecture 81 CS21 Decidability and Tractability Lecture 8 January 23, 2015.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
CONVERTING TO CHOMSKY NORMAL FORM
The Pumping Lemma for Context Free Grammars. Chomsky Normal Form Chomsky Normal Form (CNF) is a simple and useful form of a CFG Every rule of a CNF grammar.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
CS 461 – Oct. 7 Applications of CFLs: Compiling Scanning vs. parsing Expression grammars –Associativity –Precedence Programming language (handout)
CSCI 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Ambiguity.
Context-Free Grammars
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Context Free Grammar. Introduction Why do we want to learn about Context Free Grammars?  Used in many parsers in compilers  Yet another compiler-compiler,
The CYK Algorithm Presented by Aalapee Patel Tyler Ondracek CS6800 Spring 2014.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Membership problem CYK Algorithm Project presentation CS 5800 Spring 2013 Professor : Dr. Elise de Doncker Presented by : Savitha parur venkitachalam.
CS 461 – Sept. 19 Last word on finite automata… –Scanning tokens in a compiler –How do we implement a “state” ? Chapter 2 introduces the 2 nd model of.
Phrase-structure grammar A phrase-structure grammar is a quadruple G = (V, T, P, S) where V is a finite set of symbols called nonterminals, T is a set.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
CS 461 – Oct. 12 Parsing Running a parse machine –“Goto” (or shift) actions –Reduce actions: backtrack to earlier state –Maintain stack of visited states.
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.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
Top-Down Parsing.
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Normal forms.
Transparency No. 1 Formal Language and Automata Theory Homework 5.
Exercises on Chomsky Normal Form and CYK parsing
Chomsky Normal Form.
1 Context Free Grammars Xiaoyin Wang CS 5363 Spring 2016.
CS 44 – Jan. 28 Pumping lemma #2 Applications to compiling.
Introduction to Parsing
lec02-parserCFG May 8, 2018 Syntax Analyzer
David Rodriguez-Velazquez CS – 6800 Summer I
Chomsky Normal Form CYK Algorithm
Chapter 2 :: Programming Language Syntax
Ambiguity Parsing algorithms
Complexity and Computability Theory I
Theorem 29 Given any PDA, there is another PDA that accepts exactly the same language with the additional property that whenever a path leads to ACCEPT,
CS314 – Section 5 Recitation 3
Jaya Krishna, M.Tech, Assistant Professor
CS 3304 Comparative Languages
Context-Free Grammars
LR(1) grammars The Chinese University of Hong Kong Fall 2010
Introduction to Parsing
Introduction to Parsing
Midterm #2 — Review problems
Context-Free Grammars
LR(1) grammars The Chinese University of Hong Kong Fall 2011
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
The Cocke-Kasami-Younger Algorithm
Normal forms and parsing
lec02-parserCFG May 27, 2019 Syntax Analyzer
Context-Free Grammars
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

CS 44 – Jan. 29 Expression grammars –Associativity √ –Precedence CFG for entire language (handout) CYK algorithm –General technique for testing for acceptance (Note we don’t want PDA since it could be nondeterministic!)

Precedence (* /) bind stronger than (+ -) (+ -) separate better than (* /) Need to break up expression into terms –Ex. 9 – 8 * / 5 –We want to say that an expression consists of “terms” separated by + and – –And each term consists of numbers separated by * and / –But which should we define first, expr or term?

Precedence (2) Which grammar is right? expr  expr + term | expr – term | term term  term * num | term / num | num Or this one: expr  expr * term | expr / term | term term  term + num | term – num | num Let’s try examples * 3 and 1 * 2 + 3

Moral If a grammar is defining something hierarchical, like an expression, define large groupings first. Lower precedence operators appear first in grammar. (They separate better) –Ex. * appears lower in parse tree than + because it gets evaluated first. In a real programming language, there can be more than 10 levels of precedence. C has ~15!

C language Handout –How does the grammar begin? –Where are the mathematical expressions? –Do you agree with the precedence? –Do you see associativity? –What else is defined in grammar? –Where are the terminals?

Accepting input How can we tell if a given source file (input stream of tokens) is a valid program? Language defined by CFG, so … –Can see if there is some derivation from grammar? –Can convert CFG to PDA? Exponential performance not acceptable. (e.g. doubling every time we add token) Two improvements: –CYK algorithm, runs in O(n 3 ) –Bottom-up parsing, generally linear, but restrictions on grammar.

CYK algorithm In , discovered independently by Cocke, Younger, Kasami. Given any CFG and any string, can tell if grammar generates string. The grammar needs to be in CNF first. –This ensures that the rules are simple. Rules are of the form X  a or X  YZ Consider all substrings of len 1 first. See if these are in language. Next try all len 2, len 3, …. up to length n.

continued Maintain results in an NxN table. Top right portion not used. –Example on right is for testing word of length 3. Start at bottom; work your way up. For length 1, just look for “unit rules” in grammar, e.g. X  a XX X

continued For general case i..j –Think of all possible ways this string can be broken into 2 pieces. –Ex = or –We want to know if both pieces  L. This handles rules of form A  BC. Let’s try example from (in CNF) 1..3 XX X

337  ? S  AB A  3 | AC B  7 | BD C  3 D  7 For each len 1 string, which variables generate it? 1..1 is 3. Rules A and C is 3. Rules A and C is 7. Rules B and D XX X 1..1 A, C 2..2 A, C 3..3 B, D

337  ? S  AB A  3 | AC B  7 | BD C  3 D  7 Length 2: 1..2 = = (A or C)(A or C) = rule A 2..3 = = (A or C)(B or D) = rule S 1..3 XX 1..2 A 2..3 S X 1..1 A, C 2..2 A, C 3..3 B, D

337  ? S  AB A  3 | AC B  7 | BD C  3 D  7 Length 3: 2 cases for 1..3: : (A)(B or D) = S : (A or C)(S) no! We only need one case to work S XX 1..2 A 2..3 S X 1..1 A, C 2..2 A, C 3..3 B, D

Example #2 Let’s test the word baab S  AB | BC A  BA | a B  CC | b C  AB | a Length 1: ‘a’ generated by A, C ‘b’ generated by B 1..4 XXX XX X 1..1 B 2..2 A, C 3..3 A, C 4..4 B

baab S  AB | BC A  BA | a B  CC | b C  AB | a Length 2: 1..2 = = (B)(A, C) = S,A 2..3 = = (A,C)(A,C) = B 3..4 = = (A,C)(B) = S,C 1..4 XXX XX 1..2 S, A 2..3 B 3..4 S, C X 1..1 B 2..2 A, C 3..3 A, C 4..4 B

baab S  AB | BC A  BA | a B  CC | b C  AB | a Length 3: [ each has 2 chances! ] 1..3 = = (S,A)(A,C) = Ø 1..3 = = (B)(B) = Ø 2..4 = = (B)(B) = Ø 2..4 = = (A,C)(S,C) = B 1..4 XXX 1..3 Ø 2..4 B XX 1..2 S, A 2..3 B 3..4 S, C X 1..1 B 2..2 A, C 3..3 A, C 4..4 B

Finally… S  AB | BC A  BA | a B  CC | b C  AB | a Length 4 [has 3 chances!] 1..4 = = (Ø)(B) = Ø 1..4 = = (S,A)(S,C) = Ø 1..4 = = (B)(B) = Ø Ø means we lose! baab  L. However, in general don’t give up if you encounter Ø in the middle of the process Ø XXX 1..3 Ø 2..4 B XX 1..2 S, A 2..3 B 3..4 S, C X 1..1 B 2..2 A, C 3..3 A, C 4..4 B