Discussion #61/13 Discussion #6 Parsing Recursive Grammars.

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

Grammar and Algorithm }
AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9.
A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Context-Free Grammars Lecture 7
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
1 Predictive parsing Recall the main idea of top-down parsing: Start at the root, grow towards leaves Pick a production and try to match input May need.
Discussion #51/18 Discussion #5 LL(1) Grammars &Table-Driven Parsing.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
Top-Down Parsing.
CPSC 388 – Compiler Design and Construction
Syntax and Semantics Structure of programming languages.
LR Parsing Compiler Baojian Hua
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
Ambiguity, LL1 Grammars and Table-driven Parsing
C Chuen-Liang Chen, NTUCS&IE / 77 TOP-DOWN PARSING Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Lesson 5 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CSI 3120, Syntactic analysis, page 1 Syntactic Analysis and Parsing Based on A. V. Aho, R. Sethi and J. D. Ullman Compilers: Principles, Techniques and.
Syntax and Semantics Structure of programming languages.
Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter 2.2 (Partial) 1.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
Compilation With an emphasis on getting the job done quickly Copyright © – Curt Hill.
Parsing Top-Down.
LL(1) Parser. What does LL signify ? The first L means that the scanning takes place from Left to right. The first L means that the scanning takes place.
Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.
ISBN Chapter 4 Lexical and Syntax Analysis.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Top-Down Parsing.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
BY: JAKE TENBERG & CHELSEA SHIPP PROJECT REVIEW: JGIBBERISH.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Spring 16 CSCI 4430, A Milanova 1 Announcements HW1 due on Monday February 8 th Name and date your submission Submit electronically in Homework Server.
lec02-parserCFG May 8, 2018 Syntax Analyzer
A Simple Syntax-Directed Translator
Chapter 4 - Parsing CSCE 343.
Programming Languages Translator
Chapter 4 Lexical and Syntax Analysis.
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 5, 09/25/2003 Prof. Roy Levow.
Textbook:Modern Compiler Design
Compilers Welcome to a journey to CS419 Lecture15: Syntax Analysis:
Chapter 4 Syntax Analysis.
Syntax Analysis Chapter 4.
Parsing with Context Free Grammars
Top-Down Parsing.
4 (c) parsing.
Syntax Analysis Sections :.
Lexical and Syntax Analysis
CS 540 George Mason University
Bottom-up derivation tree, original grammar
Compiler Design 7. Top-Down Table-Driven Parsing
Top-Down Parsing Identify a leftmost derivation for an input string
Bottom Up Parsing.
R.Rajkumar Asst.Professor CSE
Programming Language Syntax 5
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
CSCE 531 Compiler Construction
LR Parsing. Parser Generators.
Computing Follow(A) : All Non-Terminals
Chapter 4: Lexical and Syntax Analysis Sangho Ha
Predictive Parsing Program
lec02-parserCFG May 27, 2019 Syntax Analyzer
Ben-Gurion University
Presentation transcript:

Discussion #61/13 Discussion #6 Parsing Recursive Grammars

Discussion #62/13 Topics Tail recursion LL(1) with  Table driven LL(1) with  Lexical Analyzers

Discussion #63/13 Motivating Example Let’s use integers instead of digits in our prefix language. What’s the problem with * ? –Syntax error? Or is it simply ambiguous? –* = 2 * ( ) = 4214? –* = 2 * (72+100) = 344? Solution? –Let n mark the beginning of a number e.g. * n2 + n72 n100 = 2 * ( ) = 344 –Strange: but you’ll soon see where we are headed and why.

Discussion #64/13 E  (1) N | (2) OEE O  (3) + | (4) * N  (5) nI I  (6) D | (7) ID D  (8) 0 | (9) 1 | … | (17) 9 E I D n 2 OEENN+In DI DI D In DI D 7 2 NOEE* Consider: * n2 + n72 n100

Discussion #65/13 E  (1) N | (2) OEE O  (3) + | (4) * N  (5) nI I  (6) D | (7) ID D  (8) 0 | (9) 1 | … | (17) 9 E I D n 2 OEENN+ In ?? NOEE* * n2 + n72 n100 Question… Which rule do we choose? I  (6) D orI  (7) ID We don’t know without looking further ahead. Should we look further ahead, or find another way?

Discussion #66/13 LL(1) with  There is another way. Consider the following replacement: – (6) I  D by (6) I  D T – (7) I  ID T  (7) I | (8)  Now, if I is on the top of the stack and we see a digit, we choose I  DT. If T is on top, if we see a digit, we choose T  I, otherwise we choose T  . DT I DT I1 DT 00I  Note: The  does not “consume” the “+” which is still on top. Example: the 100 in …n100+n21n…

Discussion #67/13 Tails We use tails for things that go on forever. –Numbers, eg. 12, , … –Parameter lists, eg. (parm 1, parm 2,…,parm n ) –Variable names, eg. dog, doggone, … Note that T(ail) rules have special constructions: –FIRST(I) = FIRST(T) = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} –FIRST(  for T ) = { {V T  {#}} – FIRST(T) } = {+, *, n, #} –Note: FIRST(T)  FIRST(  for T ) =  –Note also: FIRST(I)  FIRST(  for T ) = V T  {#} –Thus, our tail construction simply iterates until it reaches the end. Further, it leaves the character that is one beyond the end on top of the stack.

Discussion #68/13 E  (1) N | (2) OEE O  (3) + | (4) * N  (5) nI I  (6) DT T  (7) I | (8)  D  (9) 0 | (10) 1 | … | (18) 9 +*012…n# E(OEE,2) (N,1) O(+,3)(*,4) N(nI,5) I(DT,6) T ( ,8) (I,7) ( ,8) D(0,9)(1,10)(2,11…) +pop * 0 1 2…pop n #accept

Discussion #69/13 +*012…n# E(OEE,2) (N,1) O(+,3)(*,4) N(nI,5) I(DT,6) T ( ,8) (I,7) ( ,8) D(0,9)(1,10)(2,11) +,*,0-9,npop #accept ActionStackInputOutput InitializeE#E#  +n10n1# ACTION(E,+) = Replace [E,OEE], Out 2OEE#  +n10n1#2 ACTION(+,+) = pop(+,+)EE#+  n10n1#23 ACTION(E,n) = Replace [E,N], Out 1NE#+  n10n1#231 ACTION(O,+) = Replace [O,+], Out 3+EE#  +n10n1#23 ACTION(N,n) = Replace [N,nI], Out 5nIE#+  n10n1#2315 ACTION(I,1) = Replace [I,DT], Out 6DTE#+n  10n1#23156 ACTION(n,n) = pop(n,n)IE#+n  10n1#2315 ACTION(D,1) = Replace [D,1], Out 101TE#+n  10n1# ACTION(1,1) = pop(1,1)TE#+n1  0n1# ACTION(T,0) = Replace [T,I], Out 7IE#+n1  0n1# ACTION(I,0) = Replace [I,DT], Out 6DTE#+n1  0n1# ACTION(D,0) = Replace [D,0], Out 90TE#+n1  0n1#

Discussion #610/13 +*012…n# E(OEE,2) (N,1) O(+,3)(*,4) N(nI,5) I(DT,6) T ( ,8) (I,7) ( ,8) D(0,9)(1,10)(2,11) +,*,0-9,npop #accept ActionStackInputOutput Continued…0TE#+n1  0n1# ACTION(0,0) = pop(0,0)TE#+n10  n1# ACTION(T,n) = Replace [T,  ], Out 8 E#E# +n10  n1# ACTION(E,n) = Replace [E,N], Out 1N#N#+n10  n1# ACTION(N,n) = Replace [N,nI], Out 5nI#+n10  n1# ACTION(n,n) = pop(n,n)I#I#+n10n  1# ACTION(I,1) = Replace [I,DT], Out 6DT#+n10n  1# ACTION(D,1) = Replace [D,1], Out 101T#+n10n  1# ACTION(1,1) = pop(1,1)T#T#+n10n1  # ACTION(T,#) = Replace [T,  ], Out 8 ## +n10n1  # ACTION(#,#) = Accept! # +n10n1  # ACTION( ,n) = pop  E# +n10  n1# ACTION( ,#) = pop  # +n10n1  #

Discussion #611/13 E  (1) N | (2) OEE O  (3) + | (4) * N  (5) nI I  (6) DT T  (7) I | (8)  D  (9) 0 | (10) 1 | … | (18) E is the parse for + n 1 0 n 1 OEE N 1 N 1 In 5 TD I 7 TD  8 In 5 TD 6 1  8

Discussion #612/13 E  (1) N | (2) OEE O  (3) + | (4) * N  (5) nI I  (6) DT T  (7) I | (8)  D  (9) 0 | (10) 1 | … | (18) E OEE N 1 N 1 In 5 TD I 7 TD  8 In 5 TD 6 1  8 Lexical Analyzer Motivation

Discussion #613/13 E  (1) N | (2) OEE O  (3) + | (4) * N  (5) E becomes the parse for +n10n1 where the tokens are +, n10, and n1 OEE N 1 N 1 n10 5 n1 5 Tokenization Simplifies Grammars