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.

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

Exercise: Balanced Parentheses
Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
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
LESSON 18.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Top-Down Parsing.
1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing LR Parsing Lex and yacc Semantic Processing.
Parsing III (Eliminating left recursion, recursive descent parsing)
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.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Discussion #51/18 Discussion #5 LL(1) Grammars &Table-Driven Parsing.
Chapter 2 A Simple Compiler
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
1 The Parser Its job: –Check and verify syntax based on specified syntax rules –Report errors –Build IR Good news –the process can be automated.
1 Chapter 4: Top-Down Parsing. 2 Objectives of Top-Down Parsing an attempt to find a leftmost derivation for an input string. an attempt to construct.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
Chapter 2 A Simple Compiler
Top-Down Parsing.
Compiler construction in4020 – lecture 3 Koen Langendoen Delft University of Technology The Netherlands.
Bottom-up parsing Goal of parser : build a derivation
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
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.
Review: –How do we define a grammar (what are the components in a grammar)? –What is a context free grammar? –What is the language defined by a grammar?
Top-Down Parsing - recursive descent - predictive parsing
Chapter 5 Top-Down Parsing.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
LANGUAGE TRANSLATORS: WEEK 3 LECTURE: Grammar Theory Introduction to Parsing Parser - Generators TUTORIAL: Questions on grammar theory WEEKLY WORK: Read.
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.
# 1 CMPS 450 Parsing CMPS 450 J. Moloney. # 2 CMPS 450 Check that input is well-formed Build a parse tree or similar representation of input Recursive.
Parsing III (Top-down parsing: recursive descent & LL(1) )
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
C Chuen-Liang Chen, NTUCS&IE / 77 TOP-DOWN PARSING Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CS 153 A little bit about LR Parsing. Background We’ve seen three ways to write parsers:  By hand, typically recursive descent  Using parsing combinators.
Syntax and Semantics Structure of programming languages.
1 Chapter 4 Grammars and Parsing. 2 Context-Free Grammars: Concepts and Notation A context-free grammar G = (Vt, Vn, S, P) –A finite terminal vocabulary.
COP4020 Programming Languages Syntax Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Parsing III (Top-down parsing: recursive descent & LL(1) ) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
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.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
8 January 2004 Department of Software & Media Technology 1 Top Down Parsing Recursive Descent Parsing Top-down parsing: –Build tree from root symbol –Each.
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
Parsing III (Top-down parsing: recursive descent & LL(1) )
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
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.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
CS 154 Formal Languages and Computability March 22 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
Programming Languages Translator
Lecture #12 Parsing Types.
Table-driven parsing Parsing performed by a finite state machine.
CS 540 George Mason University
COP4020 Programming Languages
Compiler Design 7. Top-Down Table-Driven Parsing
Chapter 5 Grammars and Parsers
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Presentation transcript:

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  LR

3 The LL(1) Predict Function Given the productions A  1 A  2 … A  n During a (leftmost) derivation … A …  …  1 …or …  2 …or …  n … Deciding which production to match –Using lookahead symbols

4 The LL(1) Predict Function The limitation of LL(1) –LL(1) contains exactly those grammars that have disjoint predict sets for productions that share a common left-hand side Single Symbol Lookahead

5 Not extended BNF form $: end of file token

6

7

8 The LL(1) Parse Table The parsing information contain in the Predict function can be conveniently represented in an LL(1) parse table T:V n x V t  P  {Error} where P is the set of all productions The definition of T T[A][t]= A  X 1  X m if t  Predict( A  X 1  X m ); T[A][t]=Error otherwise

9

10 The LL(1) Parse Table A grammar G is LL(1) if and only if all entries in T contain a unique prediction or an error flag

11 Building Recursive Descent Parsers from LL(1) Tables Similar the implementation of a scanner, there are two kinds of parsers –Build in The parsing decisions recorded in LL(1) tables can be hardwired into the parsing procedures used by recursive descent parsers –Table-driven

12 Building Recursive Descent Parsers from LL(1) Tables The form of parsing procedure: The name of the nonterminal the parsing procedure handles A sequence of parsing actions

13 Building Recursive Descent Parsers from LL(1) Tables E.g. of an parsing procedure for in Micro

14 Building Recursive Descent Parsers from LL(1) Tables An algorithm that automatically creates parsing procedures like the one in Figure 5.6 from LL(1) table

15 Building Recursive Descent Parsers from LL(1) Tables The data structure for describing grammars Name of the symbols in grammar

16 Building Recursive Descent Parsers from LL(1) Tables gen_actions() –Takes the grammar symbols and generates the actions necessary to match them in a recursive descent parse

17

18 An LL(1) Parser Driver Rather than using the LL(1) table to build parsing procedures, it is possible to use the table in conjunction with a driver program to form an LL(1) parser Smaller and faster than a corresponding recursive descent parser Changing a grammar and building a new parser is easy –New LL(1) table are computed and substituted for the old tables

19 A  aBcD A D c B a

20

21 LL(1) Action Symbols During parsing, the appearance of an action symbol in a production will serve to initiate the corresponding semantic action – a call to the corresponding semantic routine. gen_action(“ID:= #gen_assign;”) match(ID); match(ASSIGN); expression(); gen_assign(); match(semicolon); What are action symbols? (See next page.)

22

23 LL(1) Action Symbols The semantic routine calls pass no explicit parameters –Necessary parameters are transmitted through a semantic stack –semantic stack  parse stack Semantic stack is a stack of semantic records. Action symbols are pushed to the parse stack –See figure 5.11

24 Difference between Fig. 5.9 and Fig 5.11

25 Making Grammars LL(1) Not all grammars are LL(1). However, some non-LL(1) grammars can be made LL(1) by simple modifications. When a grammar is not LL(1) ? This is called a conflict, which means we do not know which production to use when is on stack top and ID is the next input token. ID 2,3

26 Making Grammars LL(1) Major LL(1) prediction conflicts –Common prefixes –Left recursion Common prefixes  if then end if;  if then else end if; Solution: factoring transform –See figure 5.12

27 Making Grammars LL(1)  if then  end if;  else end if;

28 Making Grammars LL(1) Grammars with left-recursive production can never be LL(1) A  A  –Why? A will be the top stack symbol, and hence the same production would be predicted forever

29 Making Grammars LL(1) Solution: Figure 5.13 AAA…AAAA…A A  NT N  … N  T  T 

30 Making Grammars LL(1) Other transformation may be needed –No common prefixes, no left recursion 1  2  ID : 3  4  ID := ; ID 2,3

31 Making Grammars LL(1) Lookahead two tokens seems be needed! LL(2) ? Example A:B := C ; B := C ;

32 Making Grammars LL(1)  ID  :  := ;  ID := ; Solution: An equivalent LL(1) grammar ! Example A:B := C ; B := C ;

33 Making Grammars LL(1) In Ada, we may declare arrays as A: array(I.. J, BOOLEAN) A straightforward grammar for array bound ..  ID Solution  .. 

34 Making Grammars LL(1) Greibach Normal Form –Every production is of the form A  a  “a” is a terminal and  is (possible empty) string of variables –Every context-free language L without can be generated by a grammar in Greibach Normal Form –Factoring of common prefixes is easy Given a grammar G, we can –G  GNF  No common prefixes, no left recursion (but may be still not LL(1))

35 The If-Then-Else Problem in LL(1) Parsing “Dangling else” problem in Algo60, Pascal, and C –else clause is optional BL={[ i ] j | i  j  0} [  if then ]  else BL is not LL(1) and in fact not LL(k) for any k

36 The If-Then-Else Problem in LL(1) First try –G 1 : S  [ S CL S  CL  ] CL  G 1 is ambiguous: E.g., [[] S [ S CL ] S [ S CL ] Why an ambiguous grammar is not LL(1)? Please try to answer this question according to this figure.

37 Second try –G 2 : S  [S S  S1 S1  [S1] S1  G 2 is not ambiguous: E.g., [[] The problem is [  First([S) and [  First(S1) [[  First 2 ([S) and [[  First 2 (S1) –G 2 is not LL(1), nor is it LL(k) for any k. The If-Then-Else Problem in LL(1) [ S1 [ S1 ] S

38 Solution: conflicts + special rules –G 3 : G  S; S  if S E S  Other E  else S E  G 3 is ambiguous We can enforce that T[E, else] = 4th rule. This essentially forces “else “ to be matched with the nearest unpaired “ if “. The If-Then-Else Problem in LL(1)

39 If all if statements are terminated with an end if, or some equivalent symbol, the problem disappears. S  if S E S  Other E  else S end if E  end if An alternative solution –Change the language The If-Then-Else Problem in LL(1)

40 Properties of LL(1) Parsers A correct, leftmost parse is guaranteed All grammars in the LL(1) class are unambiguous –Some string has two or more distinct leftmost parses –At some point more than one correct prediction is possible All LL(1) parsers operate in linear time and, at most, linear space