Discussion #51/18 Discussion #5 LL(1) Grammars &Table-Driven Parsing.

Slides:



Advertisements
Similar presentations
Grammar and Algorithm }
Advertisements

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.
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
Top-down Parsing By Georgi Boychev, Rafal Kala, Ildus Mukhametov.
YANGYANG 1 Chap 5 LL(1) Parsing LL(1) left-to-right scanning leftmost derivation 1-token lookahead parser generator: Parsing becomes the easiest! Modifying.
Mooly Sagiv and Roman Manevich School of Computer Science
Predictive Parsing l Find derivation for an input string, l Build a abstract syntax tree (AST) –a representation of the parsed program l Build a symbol.
Discussion #61/13 Discussion #6 Parsing Recursive Grammars.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Top-Down Parsing.
Discussion #71/13 Discussion #7 Recursive Descent Parsing.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
Top-Down parsing LL(1) parsing. Overview of Top-Down  There are only two actions 1.Replace 2.Match.
Professor Yihjia Tsai Tamkang University
LR(1) Languages An Introduction Professor Yihjia Tsai Tamkang University.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
Top-Down Parsing.
Lexical and syntax analysis
COP4020 Programming Languages Computing LL(1) parsing table Prof. Xin Yuan.
Parsing IV Bottom-up Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Syntax and Semantics Structure of programming languages.
Parsing Chapter 4 Parsing2 Outline Top-down v.s. Bottom-up Top-down parsing Recursive-descent parsing LL(1) parsing LL(1) parsing algorithm First.
Top-Down Parsing - recursive descent - predictive parsing
1 Chapter 5 LL (1) Grammars and Parsers. 2 Naming of parsing techniques The way to parse token sequence L: Leftmost R: Righmost Top-down  LL Bottom-up.
Chapter 5 Top-Down Parsing.
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
Ambiguity, LL1 Grammars and Table-driven Parsing
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Syntax and Semantics Structure of programming languages.
Parsing Lecture 5 Fri, Jan 28, Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
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.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
Bc. Jozef Lang (xlangj01) Bc. Zoltán Zemko (xzemko01) Increasing power of LL(k) parsers.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Top-Down Parsing.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
Top-Down Predictive Parsing We will look at two different ways to implement a non- backtracking top-down parser called a predictive parser. A predictive.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Grammars, Derivations and Parsing. Sample Grammar Simple arithmetic expressions (E) Basis Rules: –A Variable is an E –An Integer is an E Inductive Rules:
CS 154 Formal Languages and Computability March 22 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
Error recovery in predictive parsing An error is detected during the predictive parsing when the terminal on top of the stack does not match the next input.
Parsing #1 Leonidas Fegaras.
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Bottom-Up Parsing.
Parsing and Parser Parsing methods: top-down & bottom-up
Table-driven parsing Parsing performed by a finite state machine.
Top-down parsing cannot be performed on left recursive grammars.
CS 404 Introduction to Compiler Design
4 (c) parsing.
Compiler Design 7. Top-Down Table-Driven Parsing
Programming Language Syntax 5
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
Syntax Analysis - Parsing
Kanat Bolazar February 16, 2010
Nonrecursive Predictive Parsing
Parsing Bottom-Up Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Presentation transcript:

Discussion #51/18 Discussion #5 LL(1) Grammars &Table-Driven Parsing

Discussion #52/18 Topics Approaches to Parsing –Full backtracking –Deterministic Simple LL(1), table-driven parsing Improvements to simple LL(1) grammars

Discussion #53/18 Prefix Expression Grammar Consider the following grammar (which yields prefix expressions for binary operators): E  N | OEE O  + |  | * | / N  0 | 1 | 2 | 3 | 4 Here, prefix expressions associate an operator with the next two operands. * (* (+ 2 3) 4) (2 + 3) * 4 = 20 * (* 2 (+ 3 4)) 2 * (3 + 4) = 14

Discussion #54/18 E NOEE …+  *NOEEN …+NN E  N | OEE O  + |  | * | / N  0 | 1 | 2 | 3 | 4 *+342 Top-Down Parsing with Backtracking

Discussion #55/18 What are the obvious problems? We never know what production to try. It appears to be terribly inefficient—and it is. Are there grammars for which we can always know what rule to choose? Yes! Characteristics: –Only single symbol look ahead –Given a non-terminal and a current symbol, we always know which production rule to apply

Discussion #56/18 LL(1) Parsers An LL parser parses the input from Left to right, and constructs a Leftmost derivation of the sentence. An LL(k) parser uses k tokens of look-ahead. LL(1) parsers, although fairly restrictive, are attractive because they only need to look at the current non-terminal and the next token to make their parsing decisions. LL(1) parsers require LL(1) grammars.

Discussion #57/18 Simple LL(1) Grammars For simple LL(1) grammars all rules have the form A  a 1  1 | a 2  2 | … | a n  n where a i is a terminal, 1 <= i <= n a i  a j for i  j and  i is a sequence of terminals and non-terminal or is empty, 1 <= i <= n

Discussion #58/18 Creating Simple LL(1) Grammars By making all production rules of the form: A  a 1  1 | a 2  2 | … | a n  n Thus, E  0 | 1 | 2 | 3 | 4 | +EE |  EE | *EE | /EE Why is this not a simple LL(1) grammar? E  N | OEE O  + |  | * | / N  0 | 1 | 2 | 3 | 4 How can we change it to simple LL(1)?

Discussion #59/18 E  (1) 0 | (2) 1 | (3) 2 | (4) 3 | (5) 4 | (6) +EE | (7)  EE | (8) *EE | (9) /EE * E  2 * 3 E ?*EE 8 EE  EE EE* Success! Fail! Output = Example: LL(1) Parsing

Discussion #510/18 Simple LL(1) Parse Table A parse table is defined as follows: (V  {#})  (V T  {#})  {( , i), pop, accept, error } where –  is the right side of production number i –# marks the end of the input string (#  V) If A  (V  {#}) is the symbol on top of the stack and a  (V T  {#}) is the current input symbol, then: ACTION (A, a) = pop if A = a for a  V T accept if A = # and a = # (a , i) which means “pop, then push a  and output i” (A  a  is the ith production) error otherwise

Discussion #511/18 Parse Table E  (1) 0 | (2) 1 | (3) 2 | (4) 3 | (5) +EE | (6) *EE 0123+*# E(0,1)(1,2)(2,3)(3,4)(+EE,5)(*EE,6) 0pop * #accept V  {#} V T  {#} All blank entries are error

Discussion #512/ *# E(0,1)(1,2)(2,3)(3,4)(+EE,5)(*EE,6) 0,1,2,3,+,*pop #accept ActionStackInputOutput InitializeE#E#  *+123# ACTION(E,*) = Replace [E,*EE], Out 6*EE#  *+123#6 ACTION(*,*) = pop(*,*)EE#*  +123#6 ACTION(E,+) = Replace [E,+EE], Out 5+EEE#*  +123#65 ACTION(+,+) = pop(+,+)EEE#*+  123#65 ACTION(E,1) = Replace [E,1], Out 21EE#*+  123#652 ACTION(1,1) = pop(1,1)EE#*+1  23#652 ACTION(E,2) = Replace [E,2], Out 32E#*+1  23#6523 ACTION(2,2) = pop(2,2)E#E#*+12  3#6523 ACTION(E,3) = Replace [E,3], Out 43#3#*+12  3#65234 ACTION(3,3) = pop(3,3)#*+123  #65234 ACTION(#,#) = acceptDone!

Discussion #513/18 Simple LL(1): More Restrictive than Necessary Simple LL(1) grammars are very easy and efficient to parse but also very restrictive. The good news: we can achieve the same desirable results without being so restrictive. How? We only need to retain the restriction that single-symbol look ahead uniquely determines which rule to use.

Discussion #514/18 Consider the following grammar, which is not simple LL(1): E  (1) N | (2) OEE O  (3) + | (4) * N  (5) 0 | (6) 1 | (7) 2 | (8) 3 What are the problem rules? (1) & (2) Observe that it is possible distinguish between rules 1 and 2. –N leads to {0, 1, 2, 3} –O leads to {+, *} –{0, 1, 2, 3}  {+, *} =  –Thus, if we see 0, 1, 2, or 3 we choose (1), and if we see + or *, we choose (2). Relaxing Simple LL(1) Restrictions

Discussion #515/18 LL(1) Grammars FIRST(  ) = {  |   *  and   V T } A grammar is LL(1) if for all rules of the form A   1 |  2 | … |  n the sets FIRST(  1 ), FIRST(  2 ), …, and FIRST(  n ) are pair-wise disjoint; that is, FIRST(  i )  FIRST(  j ) =  for i  j

Discussion #516/18 E  (1) N | (2) OEE O  (3) + | (4) * N  (5) 0 | (6) 1 | (7) 2 | (8) 3 +*0123# E(OEE,2) (N,1) O(+,3)(*,4) N(0,5)(1,6)(2,7)(3,8) +pop * #accept V  {#} V T  {#} For (A, a), we select ( , i) if a  FIRST(  ) and  is the right hand side of rule i.

Discussion #517/18 +*0123# E(OEE,2) (N,1) O(+,3)(*,4) N(0,5)(1,6)(2,7)(3,8) +,*,0,1,2,3pop #accept ActionStackInputOutput InitializeE#E#  *+123# ACTION(E,*) = Replace [E,OEE], Out 2OEE#  *+123#2 ACTION(*,*) = pop(*,*)EE#*  +123#24 ACTION(E,+) = Replace [E,OEE], Out 2OEEE#*  +123#242 ACTION(+,+) = pop(+,+)EEE#*+  123#2423 ACTION(N,1) = Replace [N,1], Out 61EE#*+  123# ACTION(1,1) = pop(1,1)EE#*+1  23# ACTION(E,2) = Replace [E,N], Out 1NE#*+1  23# ACTION(2,2) = pop(2,2)E#E#*+12  3# ACTION(E,3) = Replace [E,N], Out 1N#N#*+12  3# ACTION(3,3) = pop(3,3)#*+123  # ACTION(#,#) = acceptDone! ACTION(O,*) = Replace [O,*], Out 4*EE#  *+123#24 ACTION(O,+) = Replace [O,+], Out 3+EEE#*  +123#2423 ACTION(E,1) = Replace [E,N], Out 1NEE#*+  123#24231 ACTION(N,2) = Replace [N,2], Out 72E#*+1  23# ACTION(N,3) = Replace [N,3], Out 83#3#*+12  3#

Discussion #518/18 What does mean? E  (1) N | (2) OEE O  (3) + | (4) * N  (5) 0 | (6) 1 | (7) 2 | (8) 3 E (2) OEE (1) N (6) 1 (7) 2 (8) 3 (4) * (2) OEE (1) N (3) + (1) N defines a parse tree via a preorder traversal.