Lesson 5 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.

Slides:



Advertisements
Similar presentations
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Advertisements

Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
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.
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.
Professor Yihjia Tsai Tamkang University
Top-Down 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.
COP4020 Programming Languages Computing LL(1) parsing table Prof. Xin Yuan.
ERROR HANDLING Lecture on 27/08/2013 PPT: 11CS10037 SAHIL ARORA.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
1 Syntax Analysis Part I Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Top-Down Parsing - recursive descent - predictive parsing
Chapter 5 Top-Down Parsing.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 Compiler Construction Syntax Analysis Top-down parsing.
Lesson 9 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.
Joey Paquet, 2000, Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic Analysis.
Exercise 1 A ::= B EOF B ::=  | B B | (B) Tokens: EOF, (, ) Generate constraints and compute nullable and first for this grammar. Check whether first.
1 Problems with Top Down Parsing  Left Recursion in CFG May Cause Parser to Loop Forever.  Indeed:  In the production A  A  we write the program procedure.
Pembangunan Kompilator.  The parse tree is created top to bottom.  Top-down parser  Recursive-Descent Parsing ▪ Backtracking is needed (If a choice.
UNIT - 2 -Compiled by: Namratha Nayak | Website for Students | VTU - Notes - Question Papers.
Compiler Principle and Technology Prof. Dongming LU Mar. 27th, 2015.
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.
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.
Lecture 3: Parsing CS 540 George Mason University.
1 Context free grammars  Terminals  Nonterminals  Start symbol  productions E --> E + T E --> E – T E --> T T --> T * F T --> T / F T --> F F --> (F)
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Top-Down Parsing.
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.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Parser: CFG, BNF Backus-Naur Form is notational variant of Context Free Grammar. Invented to specify syntax of ALGOL in late 1950’s Uses ::= to indicate.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman Summer 2004 (1425)
Chapter 2 (part) + Chapter 4: Syntax Analysis S. M. Farhad 1.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Joey Paquet, 2000, 2002, 2008, 2012, Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic Analysis.
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.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Error recovery in top-down.
Parsing COMP 3002 School of Computer Science. 2 The Structure of a Compiler syntactic analyzer code generator program text interm. rep. machine code tokenizer.
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.
Syntax Analysis Part I Chapter 4
Introduction to Top Down Parser
Top-down parsing cannot be performed on left recursive grammars.
Syntax Analysis Chapter 4.
SYNTAX ANALYSIS (PARSING).
Top-Down Parsing.
3.2 Language and Grammar Left Factoring Unclear productions
Syntax Analysis Sections :.
Top-Down Parsing CS 671 January 29, 2008.
Lecture 7 Predictive Parsing
CS 540 George Mason University
Top-Down Parsing The parse tree is created top to bottom.
Chapter 4 Top Down Parser.
Computing Follow(A) : All Non-Terminals
Lecture 7 Predictive Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Lesson 5 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

2 Outline The sets FIRST and FOLLOW Non-recursive predictive parsing Handling syntax errors

THE SETS FIRST AND FOLLOW 3

Motivation Grammar problematic for predictive parsing: stmt→ func_call | loop func_call → id ( args ) ; loop→ while ( expr ) block | for ( expr ; expr ; expr ) block 4

Motivation stmt→ func_call | loop func_call → id ( args ) ; loop→ while ( expr ) block | for ( expr ; expr ; expr ) block FIRST(func_call) = { id } FIRST(loop) = { while, for } 5

FIRST(α) Simple case: α starts with a terminal a: FIRST(α) = { a } Harder case: α starts with a nonterminal A –Must examine what A can produce If α ⇒ * ε then ε ∊ FIRST(α) 6

Computing FIRST(X) Start with Ø If X is a terminal then add X and return If X ⇒ * ε then add ε For all rules X → Y 1 Y 2...Y k do –For all Y i, where i = 1..k, do Add FIRST(Y i ) except for ε If ε is not in FIRST(Y i ) then break 7

FIRST example (4.30 in the book) Grammar: E → T E' E' → + T E' | ε T → F T' T' → * F T' | ε F → ( E ) | id FIRST sets: FIRST(E) = { (, id } FIRST(T) = { (, id } FIRST(F) = { (, id } FIRST(E') = { +, ε } FIRST(T') = { *, ε } 8

FIRST example (4.30 in the book) Grammar: E → T E' E' → + T E' | ε T → F T' T' → * F T' | ε F → ( E ) | id E ⇒ T E' ⇒ F T' E' ⇒ ( E ) T' E' ⇒ … E ⇒ T E' ⇒ F T' E' ⇒ id T' E' ⇒ … FIRST(E) = { (, id } seems correct! 9

FIRST example (4.30 in the book) Grammar: E → T E' E' → + T E' | ε T → F T' T' → * F T' | ε F → ( E ) | id E' ⇒ + T E' ⇒ … + ∈ FIRST(E') seems correct! T' ⇒ * F T' ⇒ … * ∈ FIRST(T') seems correct! 10

Exercise (1) a)Compute FIRST(K) and FIRST(M): K → K, i : M K → i : M M → M, i M → i b)Compute FIRST(S), FIRST(A), and FIRST(B): S → 1 A : A S → 0 : A A → A B A → ε B → 0 B → 1 11

FOLLOW(A) “What can follow A?” Example grammar: S → a A b A c A → d | e FOLLOW(A) = { b, c } 12

Computing FOLLOW(A) Start with Ø If A is the start symbol then add $ For all rules B → α A β do –Add everything except ε from FIRST(β) For all rules B → α A, or B → α A β where ε ∊ FIRST(β), do –Add everything from FOLLOW(B) 13

FOLLOW example (4.30 in the book) Grammar: E → T E' E' → + T E' | ε T → F T' T' → * F T' | ε F → ( E ) | id FOLLOW sets: FOLLOW(E) = { $, ) } FOLLOW(E') = { $, ) } FOLLOW(T) = { +, $, ) } FOLLOW(T‘) = { +, $, ) } FOLLOW(F) = { *, +, $, ) } 14

FOLLOW example (4.30 in the book) Grammar: E → T E' E' → + T E' | ε T → F T' T' → * F T' | ε F → ( E ) | id 15 E $ ⇒ T E' $ ⇒ F T' E' $ ⇒ ( E ) T' E' $ ⇒ ( T E' ) T' E' $ ⇒ … FOLLOW(E) = { $, ) } seems correct! FOLLOW(E') = { $, ) } seems correct!

FOLLOW example (4.30 in the book) Grammar: E → T E' E' → + T E' | ε T → F T' T' → * F T' | ε F → ( E ) | id 16 E $ ⇒ T E' $ ⇒ T + T E' $ ⇒ T + T $ ⇒ T + F T' $ ⇒ T + ( E ) T' $ ⇒ T + ( T E' ) T' $ ⇒ T + ( T ) T' $ ⇒ … FOLLOW(T) = { +, $, ) } seems correct!

Exercise (2) a)Compute FOLLOW(K) and FOLLOW(M): K → K, i : M K → i : M M → M, i M → i b)Compute FOLLOW(S), FOLLOW(A), and FOLLOW(B): S → 1 A : A S → 0 : A A → A B A → ε B → 0 B → 1 17

LL(1) grammars Not left-recursive Not ambiguous For all A → α | β: –FIRST(α) ∩ FIRST(β) = Ø –If ε ∊ FIRST(α) then FOLLOW(A) ∩ FIRST(β) = Ø –If ε ∊ FIRST(β) then FOLLOW(A) ∩ FIRST(α) = Ø 18

NON-RECURSIVE PREDICTIVE PARSING 19

Types of top-down parsers Predictive recursive descent parsers –Lab 1 General recursive descent parsers Non-recursive predictive parsers 20

Non-recursive predictive parsers Keeps a stack of expected symbols Loops: –Pop a symbol X –If X is a terminal, match with lookahead –If X is a nonterminal, predict and push 21

Parse table Encodes predictions: 22 Nonterminal Input symbol id+*()$ EE → T E' E'E' → + T E'E' → ε TT → F T' T'T' → εT' → * F T'T' → ε FF → idF → ( E )

Demonstration Parse the string id * id using the previous parse table 23

Constructing the parse table For each rule A → α do –For each terminal a in FIRST(α) do Write A → α in position M[A, a] –If ε is in FIRST(α) then For each element b in FOLLOW(A) do –Add A → α in position M[A, b] 24

HANDLING SYNTAX ERRORS 25

Types of errors Lexical Syntactic Semantic Logical 26

Handling errors Point out the spot Tell the reason Try to recover and proceed compiling Do not generate code 27

Recovery strategies Panic mode Phrase-level Error productions Global correction 28

Panic mode Discard until synchronizing token What are good synchronizing tokens? Properties: –Simple and fast –Might miss errors in discarded input 29

Phrase-level Try to “fix” the input –Replace a comma by a semicolon –Delete or insert a semicolon –… 30

Error productions Anticipate common errors Add productions for these One variant supported in Bison 31

Global correction Try to find alternative parse tree Minimize corrections Too costly 32

Conclusion The sets FIRST and FOLLOW Definition of LL(1) grammars Non-recursive predictive parsing Handling syntax errors 33

Next time Code generation using syntax-directed translation Lexical analysis 34