Compiler Construction

Slides:



Advertisements
Similar presentations
Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
Advertisements

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.
Parsing III (Eliminating left recursion, 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.
Professor Yihjia Tsai Tamkang University
Top-Down Parsing.
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
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
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
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.
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 III (Top-down parsing: recursive descent & LL(1) )
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.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 5 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
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.
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.
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 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.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Week 5 Questions / Concerns What’s due: Lab2 part a due on Sunday Lab1 check-off by appointment Test#1 HW#5 next Thursday Coming up: Lab3 Posted. Discuss.
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.
LECTURE 7 Lex and Intro to Parsing. LEX Last lecture, we learned a little bit about how we can take our regular expressions (which specify our valid tokens)
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Parsing III (Top-down parsing: recursive descent & LL(1) )
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.
Announcements/Reading
Parsing #1 Leonidas Fegaras.
Parsing III (Top-down parsing: recursive descent & LL(1) )
A Simple Syntax-Directed Translator
CS510 Compiler Lecture 4.
Lexical and Syntax Analysis
Lecture #12 Parsing Types.
Parsing — Part II (Top-down parsing, left-recursion removal)
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Chapter 4 Top-Down Parsing Part-1 September 8, 2018
Top-Down Parsing.
4 (c) parsing.
Parsing Techniques.
Top-Down Parsing CS 671 January 29, 2008.
Syntax-Directed Definition
CS 540 George Mason University
Parsing & Scanning Lecture 2
Lecture 8 Bottom Up Parsing
Lecture 8: Top-Down Parsing
Ambiguity, Precedence, Associativity & Top-Down Parsing
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
LL and Recursive-Descent Parsing
The Recursive Descent Algorithm
Recursive descent parsing
Nonrecursive Predictive Parsing
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Construction
Predictive Parsing Program
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Recursive descent parsing
Presentation transcript:

Compiler Construction Sohail Aslam Lecture 14 compiler: intro

Predictive Parsing The LL(1) Property If A → a and A → b both appear in the grammar, we would like FIRST(a)  FIRST(b) =  end of lect 13 compiler: intro

LL(k) Predictive Parsing Predictive parsers accept LL(k) grammars “left-to-right” scan of input LL(k) “k” tokens of lookahead left-most derivation

Predictive Parsing The LL(1) Property FIRST(a)  FIRST(b) =  allows the parser to make a correct choice with a lookahead of exactly one symbol!

Predictive Parsing What about e-productions? They complicate the definition of LL(1)

Predictive Parsing What about e-productions? They complicate the definition of LL(1)

Predictive Parsing If A → a and A → b and e  FIRST(a) , then we need to ensure that FIRST(b) is disjoint from FOLLOW(a), too

Predictive Parsing FOLLOW(a) is the set of all words in the grammar that can legally appear after an a.

Predictive Parsing For a non-terminal X, FOLLOW(X ) is the set of symbols that might follow the derivation of X.

Predictive Parsing FIRST and FOLLOW X FIRST FOLLOW

Predictive Parsing Define FIRST+(a) as FIRST(a)  FOLLOW(a), if e  FIRST(a) FIRST(a), otherwise

Predictive Parsing Then a grammar is LL(1) iff A → a and A → b implies FIRST+(a)  FIRST+(b) = 

Predictive Parsing Given a grammar that has the is LL(1) property we can write a simple routine to recognize each lhs code is simple and fast

Predictive Parsing Given a grammar that has the is LL(1) property we can write a simple routine to recognize each lhs code is simple and fast

Predictive Parsing Given a grammar that has the is LL(1) property we can write a simple routine to recognize each lhs code is simple and fast

Predictive Parsing Consider A → b1 | b2 | b3 , which satisfies the LL(1) property FIRST+(a)FIRST+(b) = 

/* find an A */ if(token  FIRST(b1)) find a b1 and return true else if(token  FIRST(b2)) find a b2 and return true if(token  FIRST(b3)) find a b3 and return true else error and return false

/* find an A */ if(token  FIRST(b1)) find a b1 and return true else if(token  FIRST(b2)) find a b2 and return true if(token  FIRST(b3)) find a b3 and return true else error and return false

/* find an A */ if(token  FIRST(b1)) find a b1 and return true else if(token  FIRST(b2)) find a b2 and return true if(token  FIRST(b3)) find a b3 and return true else error and return false

/* find an A */ if(token  FIRST(b1)) find a b1 and return true else if(token  FIRST(b2)) find a b2 and return true if(token  FIRST(b3)) find a b3 and return true else error and return false

/* find an A */ if(token  FIRST(b1)) find a b1 and return true else if(token  FIRST(b2)) find a b2 and return true if(token  FIRST(b3)) find a b3 and return true else error and return false

Predictive Parsing Grammar with the LL(1) property are called predictive grammars because the parser can “predict” the correct expansion at each point in the parse.

Predictive Parsing Parsers that capitalize on the LL(1) property are called predictive parsers One kind of predictive parser is the recursive descent parser

Predictive Parsing Parsers that capitalize on the LL(1) property are called predictive parsers One kind of predictive parser is the recursive descent parser

Recursive Descent Parsing 1 Goal → expr 2 term expr' 3 expr' + term expr' 4 | - term expr' 5 e 6 term factor term' 7 term' * factor term' 8 ∕ factor term' 9 10 factor number 11 id 12 ( expr )

Recursive Descent Parsing This leads to a parser with six mutually recursive routines Goal Term Expr TPrime EPrime Factor

Recursive Descent Parsing Each recognizes one non-terminal (NT) or terminal (T) Goal Term Expr TPrime EPrime Factor

Recursive Descent Parsing The term descent refers to the direction in which the parse tree is built. Here are some of these routines written as functions

Recursive Descent Parsing The term descent refers to the direction in which the parse tree is built. Here are some of these routines written as functions

Goal() { token = next_token(); if(Expr() == true && token == EOF) next compilation step else { report syntax error; return false; }

Expr() { if(Term() == false) return false; else return Eprime(); }

Eprime() { token_type op = next_token(); if( op == PLUS || op == MINUS ) { if(Term() == false) return false; else return Eprime(); }

Recursive Descent Parsing Functions for other non-terminals Term, Factor, Tprime follow the same pattern.

Recursive Descent in C++ Shortcomings Too procedural No convenient way to build parse tree

Recursive Descent in C++ Using an OO Language Associate a class with each non-terminal symbol Allocated object contains pointer to the parse tree

Recursive Descent in C++ Using an OO Language Associate a class with each non-terminal symbol Allocated object contains pointer to the parse tree

Recursive Descent in C++ Using an OO Language Associate a class with each non-terminal symbol Allocated object contains pointer to the parse tree

Non-terminal Classes class NonTerminal { public: NonTerminal(Scanner* sc){ s = sc; tree = NULL; } virtual ~NonTerminal(){} virtual bool isPresent()=0; TreeNode* AST(){ return tree; } compiler: intro

class NonTerminal { public: NonTerminal(Scanner* sc){ s = sc; tree = NULL; } virtual ~NonTerminal(){} virtual bool isPresent()=0; TreeNode* AST(){ return tree; } Constructor: stores the pointer to the scanner (lexer) in protected variable and initializes tree pointer to NULL.

class NonTerminal { public: NonTerminal(Scanner* sc){ s = sc; tree = NULL; } virtual ~NonTerminal(){} virtual bool isPresent()=0; TreeNode* AST(){ return tree; } Polymorphic default destructor. Called in case derived classes do not define their own

class NonTerminal { public: NonTerminal(Scanner* sc){ s = sc; tree = NULL; } virtual ~NonTerminal(){} virtual bool isPresent()=0; TreeNode* AST(){ return tree; } isPresent is pure virtual (=0). Base class will not provide implementation

class NonTerminal { public: NonTerminal(Scanner* sc){ s = sc; tree = NULL; } virtual ~NonTerminal(){} virtual bool isPresent()=0; TreeNode* AST(){ return tree; } Return pointer to Abstract Syntax tree. Available to all subclasses

class NonTerminal { public: NonTerminal(Scanner* sc){ s = sc; tree = NULL; } virtual ~NonTerminal(){} virtual bool isPresent()=0; TreeNode* AST(){ return tree; }

class NonTerminal { . . . . . protected: Scanner* s; TreeNode* tree; }

class Expr:public NonTerminal { public: Expr(Scanner* sc): NonTerminal(sc){ } virtual bool isPresent(); }

NonTerminal is the base class; Expr is the derived class. class Expr:public NonTerminal { public: Expr(Scanner* sc): NonTerminal(sc){ } virtual bool isPresent(); } NonTerminal is the base class; Expr is the derived class.

Expr’s constructor calls the base class constructor explicitly. class Expr:public NonTerminal { public: Expr(Scanner* sc): NonTerminal(sc){ } virtual bool isPresent(); } Expr’s constructor calls the base class constructor explicitly.

isPresent() is a polymorphic function. class Expr:public NonTerminal { public: Expr(Scanner* sc): NonTerminal(sc){ } virtual bool isPresent(); } isPresent() is a polymorphic function.

class Eprime:public NonTerminal { Eprime(Scanner* sc, TreeNode* t): NonTerminal(sc){ exprSofar = t; } virtual bool isPresent(); protected: TreeNode* exprSofar; }

class Term:public NonTerminal { public: Term(Scanner* sc): NonTerminal(sc){ } virtual bool isPresent(); }

class Tprime:public NonTerminal { Tprime(Scanner* sc, TreeNode* t): NonTerminal(sc){ exprSofar = t; } virtual bool isPresent(); protected: TreeNode* exprSofar; }

class Factor:public NonTerminal { public: Factor(Scanner* sc, TreeNode* t): NonTerminal(sc){ }; virtual bool isPresent(); } end of lec 14 compiler: intro