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

Slides:



Advertisements
Similar presentations
Parsing 4 Dr William Harrison Fall 2008
Advertisements

Chapter 2-2 A Simple One-Pass Compiler
Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
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.
Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Top-Down Parsing.
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #7 Parsing.
Parsing III (Eliminating left recursion, recursive descent parsing)
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
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)
CS 330 Programming Languages 09 / 23 / 2008 Instructor: Michael Eckmann.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
CH2.1 CSE4100 Chapter 2: A Simple One Pass Compiler Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371.
Chapter 2 Chang Chi-Chung rev.1. A Simple Syntax-Directed Translator This chapter contains introductory material to Chapters 3 to 8  To create.
MIT Top-Down Parsing Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn 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.
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?
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.
CS 330 Programming Languages 09 / 26 / 2006 Instructor: Michael Eckmann.
1 Top Down Parsing. CS 412/413 Spring 2008Introduction to Compilers2 Outline Top-down parsing SLL(1) grammars Transforming a grammar into SLL(1) form.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax (cont.) Lecture 4 – Syntax (Cont.), Spring CSE3302 Programming Languages,
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lesson 7 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lexical and Syntax Analysis
Lesson 5 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Syntax and Semantics Structure of programming languages.
COP4020 Programming Languages Syntax Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Left Recursion Lecture 7 Fri, Feb 4, 2005.
Top Down Parsing - Part I Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Simple One-Pass Compiler
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Recursive Descent Parsers Lecture 6 Mon, Feb 2, 2004.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
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 lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
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 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 CIS 461 Compiler Design and Construction Fall 2012 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #8 Parsing Techniques.
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)
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.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Top-Down Parsing.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #6 Parsing.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Errors in Top-Down Parsing Teoría de Autómatas y Lenguajes Formales M. Luisa González Díaz Universidad de Valladolid, 2005.
Chapter 2: A Simple One Pass Compiler
CSE 3302 Programming Languages
Programming Languages Translator
Lexical and Syntax Analysis
Context free grammars Terminals Nonterminals Start symbol productions
Lecture #12 Parsing Types.
Parsing with Context Free Grammars
Chapter 4 Top-Down Parsing Part-1 September 8, 2018
4 (c) parsing.
Syntax-Directed Definition
Recursive decent parsing
COP4020 Programming Languages
Syntax Analysis - Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Presentation transcript:

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

2 Outline Recursive descent parsers Left recursion Left factoring

RECURSIVE DESCENT PARSING 3

Writing a recursive descent parser Straightforward once the grammar is written in an appropriate form: –For each nonterminal: create a function Represents the expectation of that nonterminal in the input Each such function should choose a grammar production, i.e., RHS, based on the lookahead token It should then process the chosen RHS –Terminals are “matched”: match(IF); match(LEFT_PARENTHESIS); … match(RIGHT_PARENTHESIS); … –For nonterminals their corresponding “expectation functions” are called 4

The function match() Helper function to consume terminals: void match(int expected_lookahead) { if (lookahead == expected_lookahead) lookahead = nextToken(); else error(); } (assumes tokens are represented as ints) 5

Recursive descent example Grammar for a subset of the language “types in Pascal”: type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num Examples of “programs”: ^ my_type array [ ] of Integer array [ Char ] of

Recursive descent example void type() { switch (lookahead) { case '^': match('^'); match(ID); break; case ARRAY: match(ARRAY); match('['); simple(); match(']'); match(OF); type(); break; default: simple(); } void simple() { switch (lookahead) { case INTEGER: match(INTEGER); break; case CHAR: match(CHAR); break; case NUM: match(NUM); match(DOTDOT); match(NUM); break; default: error(); } 7

Exercise (1) List the calls made by the previous recursive descent parser on the input string array [ num dotdot num ] of integer To get you started: type() match(ARRAY) match('[') simple()... 8

9 type array [ num dotdot num ] of integer type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num

10 type array [ num dotdot num ] of integer type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num simple type

11 type array [ num dotdot num ] of integer type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num simple type

12 type array [ num dotdot num ] of integer type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num simple type

13 type array [ num dotdot num ] of integer type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num simple type

14 type array [ num dotdot num ] of integer type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num simple type

15 type array [ num dotdot num ] of integer type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num simple type

16 type array [ num dotdot num ] of integer type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num simple type

17 type array [ num dotdot num ] of integer type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num simple type

18 type array [ num dotdot num ] of integer type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num simple type

19 type array [ num dotdot num ] of integer type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num simple type simple

20 type array [ num dotdot num ] of integer type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num type→ ^ id | array [ simple ] of type | simple simple→ integer | char | num dotdot num simple type simple

LEFT RECURSION 21

The problem with left recursion Left-recursive grammar: A → A α | β Problematic for recursive descent parsing –Infinite recursion 22

The problem with left recursion The left-recursive expression grammar: expr→ expr + num | expr – num | num Parser code: void expr() { if (lookahead != NUM) expr(); match('+'); … 23

Eliminating left recursion Left-recursive grammar: A → A α | β Rewritten grammar: A → β M M → α M | ε 24

Exercise (2) Remove the left recursion from the following grammar for formal parameter lists in C: list→ par | list, par par → int id int and id are tokens that represent the keyword int and identifiers, respectively. Hint: what is α and what is β in this case? 25

LEFT FACTORING 26

The problem Recall: how does a predictive parser choose production body? What if the lookahead token matches more than one such production body? 27

The problem Problematic grammar: list→ num | num, list If lookahead = num, what to expect? 28

Left factoring The previous grammar, list→ num | num, list becomes list → num list’ list’→ ε |, list 29

Exercise (3) Perform left factoring on the following grammar for declarations of variables and functions in C: decl→ int id ; | int id ( pars ) ; pars →... 30

Conclusion Recursive descent parsers Left recursion Left factoring 31

Next time The sets FIRST and FOLLOW Defining LL(1) grammars Non-recursive top-down parser Handling syntax errors 32