Chapter 3 Chang Chi-Chung 2015.6.8.

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

Grammar and Algorithm }
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Lecture # 11 Grammar Problems.
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.
Top-Down Parsing.
Pertemuan 9, 10, 11 Top-Down Parsing
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
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.
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
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.
Top-Down parsing LL(1) parsing. Overview of Top-Down  There are only two actions 1.Replace 2.Match.
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.
1 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice Grammars and Parsing.
Chapter 4 Chang Chi-Chung
Chapter 3 Chang Chi-Chung Parse tree intermediate representation The Role of the Parser Lexical Analyzer Parser Source Program Token Symbol.
– 1 – CSCE 531 Spring 2006 Lecture 7 Predictive Parsing Topics Review Top Down Parsing First Follow LL (1) Table construction Readings: 4.4 Homework: Program.
Compiler Construction Sohail Aslam Lecture LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.
Compiler Principles Winter Compiler Principles Exercises on scanning & top-down parsing Roman Manevich Ben-Gurion University.
COP4020 Programming Languages Computing LL(1) parsing table Prof. Xin Yuan.
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.
Chapter 9 Syntax Analysis Winter 2007 SEG2101 Chapter 9.
1 Syntax Analysis Part I Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
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.
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) )
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
Chapter 3-3 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR 
Lesson 5 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
6/4/2016IT 3271 The most practical Parsers: Predictive parser: 1.input (token string) 2.Stacks, parsing table 3.output (syntax tree, intermediate codes)
Pembangunan Kompilator.  The parse tree is created top to bottom.  Top-down parser  Recursive-Descent Parsing ▪ Backtracking is needed (If a choice.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Top-Down Parsing The parse tree is created top to bottom. Top-down parser –Recursive-Descent Parsing.
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.
1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
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.
TOP-DOWN PARSING Recursive-Descent, Predictive Parsing.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Top-Down Parsing.
Syntax Analyzer (Parser)
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Top-Down Parsing CS308 Compiler Theory.
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.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Chapter 2 (part) + Chapter 4: Syntax Analysis S. M. Farhad 1.
Bernd Fischer RW713: Compiler and Software Language Engineering.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Parsing COMP 3002 School of Computer Science. 2 The Structure of a Compiler syntactic analyzer code generator program text interm. rep. machine code tokenizer.
Revision ? E  TE  E   + TE  |  T  FT  T   * FT  | 
Compiler Construction
Syntax Analysis Part I Chapter 4
Top-down parsing cannot be performed on left recursive grammars.
Syntax Analysis source program lexical analyzer tokens syntax analyzer
Compiler Design 7. Top-Down Table-Driven Parsing
Nonrecursive Predictive Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Presentation transcript:

Chapter 3 Chang Chi-Chung 2015.6.8

LL(1) Grammar Predictive parsers, that is, recursive-descent parsers needing no backtracking, can be constructed for a class of grammars called LL(1) First “L” means the input from left to right. Second “L” means leftmost derivation. “1” for using one input symbol of lookahead at each step tp make parsing action decisions. No left-recursive. No ambiguous.

LL(1) 文法 明確性文法 (No Ambiguity) 不可以有左遞迴 (No Left Recursion) 不可以有左因子 (No Left Factor)

Top-Down Parsing LL methods and recursive-descent parsing Left-to-right, Leftmost derivation Creating the nodes of the parse tree in preorder ( depth-first ) Grammar E  T + T T  ( E ) T  - E T  id Leftmost derivation E lm T + T lm id + T lm id + id E T + id E T + id E E T +

Top-down Parsing Give a Grammar G E  E + T | T T  T * F | F E → T E’ E’ → + T E’ | ε T → F T’ T’ → * F T’ | ε F → ( E ) | id E  E + T | T T  T * F | F F  ( E ) | id

Elimination of Left Recursion Productions of the form A  A  |  are left recursive Non-left-recursions A   A’ A’   A’ | ε When one of the productions in a grammar is left recursive then a predictive parser loops forever on certain inputs

Immediate Left-Recursion Elimination Group the Productions as A  A1 | A2 | … | Am | 1 | 2 | … | n Where no i begins with an A Replace the A-Productions by A   1 A’ | 2 A’ | … | n A’ A’  1 A’ | 2 A’ | … | m A’ | ε

Example Left-recursive grammar Into a right-recursive production A  A  |  |  | A  Into a right-recursive production A   AR |  AR AR   AR |  AR | 

Non-Immediate Left-Recursion The Grammar S  A a | b A  A c | S d | ε The nonterminal S is left recursive, because S  A a  Sda But S is not immediately left recursive.

Elimination of Left Recursion Eliminating left recursion algorithm Arrange the nonterminals in some order A1, A2, …, An for (each i from 1 to n) { for (each j from 1 to i-1){ replace each production Ai  Aj with Ai  1 | 2 | … | k where Aj  1 | 2 | … | k } eliminate the immediate left recursion in Ai }

Example A  B C | a B  C A | A b C  A B | C C | a i = 1 nothing to do i = 2, j = 1 B  C A | A b  B  C A | B C b | a b (imm) B  C A BR | a b BR BR  C b BR |  i = 3, j = 1 C  A B | C C | a  C  B C B | a B | C C | a i = 3, j = 2 C  B C B | a B | C C | a C  C A BR C B | a b BR C B | a B | C C | a (imm)C  a b BR C B CR | a B CR | a CR CR  A BR C B CR | C CR | 

Exercise Answer The grammar A  A c | A a d | b d |  S  A a | b A  A c | S d | ε Answer A  A c | A a d | b d | 

Left Factoring Left Factoring is a grammar transformation. Predictive Parsing Top-down Parsing Replace productions A   1 |  2 | … |  n |  with A   AR |  AR  1 | 2 | … | n

Example The Grammar stmt  if expr then stmt | if expr then stmt else stmt Replace with stmt  if expr then stmt stmts stmts  else stmt | ε

Exercise The following grammar Answer S  i E t S | i E t S e S | a E  b Answer S  i E t S S’ | a S’  e S | ε

LL(1)

LL(1) A grammar G is LL(1) if it is not left recursive and for each collection of productions A  1 | 2 | … | n for nonterminal A the following holds: FIRST(i)  FIRST(j) =  for all i  j 如果交集不是空集合,會如何? if i *  then j *  for all i  j FIRST(j)  FOLLOW(A) =  for all i  j

Example Grammar Not LL(1) because: S  S a | a Left recursive S  a S | a FIRST(a S)  FIRST(a) ={a}  S  a R |  R  S |  For R: S *  and  *  S  a R a R  S |  For R: FIRST(S)  FOLLOW(R)  

Top-down Parsing Give a Grammar G E  E + T | T T  T * F | F E → T E’ E’ → + T E’ | ε T → F T’ T’ → * F T’ | ε F → ( E ) | id E  E + T | T T  T * F | F F  ( E ) | id

Example FIRST E ( id E’ +  T T’ *  F Give a Grammar G FOLLOW E $ ) +  T T’ *  F Give a Grammar G E → T E’ E’ → + T E’ | ε T → F T’ T’ → * F T’ | ε F → ( E ) | id FOLLOW E $ ) E’ $ ) T + $ ) T’ + $ ) F * + $ )

Non-Recursive Predictive Parsing Table-Driven Parsing Given an LL(1) grammar G = <N, T, P, S> construct a table M[A,a] for A  N, a  T and use a driver program with a stack input a + b $ stack Predictive parsing program (driver) X Y Z $ output Parsing table M

Predictive Parsing Table Algorithm

Example A   FIRST() FOLLOW(A) E  T E’ ( id $ ) E’  + T E’ + E’    T  F T’ + $ ) T’  * F T’ * T’   F  ( E ) ( * + $ ) F  id id E  T E’ E’  + T E’ |  T  F T ’ T ’  * F T ’ |  F  ( E ) | id id + * ( ) $ E E  T E’ E’ E’  + T E’ E’   T T  F T’ T’ T’   T’  * F T’ F F  id F  ( E )

Example MATCHED STACK INPUT ACTION E$ id + id * id$ TE’$ E  T E’ FT’E’$ T  F T ’ id T’E’$ F  id id T’E’$ + id * id$ match id E’$ T ’   +TE’$ E’  + T E ’ id + id * id$ match + T  F T’ id + id * id$ * FT’E’$ T’  * F T’ id + id * id$ match * id + id * id $ Match id T’   E’   Table-Driven Parsing E  T E’ E’  + T E’ |  T  F T ’ T ’  * F T ’ |  F  ( E ) | id

Exercise Give a Grammar G as below Calculate the FIRST and FOLLOW S  i E t S S’ S’  e S | ε E  b Calculate the FIRST and FOLLOW Create a predictive parsing table

Answer Ambiguous grammar S  i E t S S’ | a S’ e S |  E  b FIRST() FOLLOW(A) S  i E t S S’ i e $ S  a a S’  e S e S’    E  b b t Ambiguous grammar S  i E t S S’ | a S’ e S |  E  b Error: duplicate table entry a b e i t $ S S  a S  i E t S S’ S’ S’   S’  e S S’   E E  b