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.

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

Chapter 2-2 A Simple One-Pass Compiler
A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
LESSON 18.
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,
Chapter 2 A Simple Compiler
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.
Professor Yihjia Tsai Tamkang University
Chapter 2 A Simple Compiler
MIT Top-Down Parsing Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
CS 3240: Languages and Computation Course Overview Sasha Boldyreva.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
1 Chapter 2 A Simple Compiler. 2 Outlines 2.1 The Structure of a Micro Compiler 2.2 A Micro Scanner 2.3 The Syntax of Micro 2.4 Recursive Descent Parsing.
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
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?
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.
-Mandakinee Singh (11CS10026).  What is parsing? ◦ Discovering the derivation of a string: If one exists. ◦ Harder than generating strings.  Two major.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lexical and Syntax Analysis
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
CPS 506 Comparative Programming Languages Syntax Specification.
Simple One-Pass Compiler
COP4020 Programming Languages Parsing Prof. Xin Yuan.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Chapter 1 Introduction Major Data Structures in Compiler
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
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 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 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Top-Down Parsing.
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.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
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 By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Chapter 2: A Simple One Pass Compiler
CS 2130 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing Warning: The precedence table given for the Wff grammar is in error.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
A Simple Syntax-Directed Translator
Programming Languages Translator
Context free grammars Terminals Nonterminals Start symbol productions
Lecture #12 Parsing Types.
Compiler Construction
Top-down parsing cannot be performed on left recursive grammars.
Parsing with Context Free Grammars
4 (c) parsing.
Lexical and Syntax Analysis
Top-Down Parsing CS 671 January 29, 2008.
Syntax-Directed Definition
Chapter 2: A Simple One Pass Compiler
Top-Down Parsing Identify a leftmost derivation for an input string
Lecture 7: Introduction to Parsing (Syntax Analysis)
R.Rajkumar Asst.Professor CSE
CMPE 152: Compiler Design August 21/23 Lab
Compiler design.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Presentation transcript:

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 string. Equivalently, it can be viewed as an attempt to construct a parse tree for the input starting form the root and creating nodes of the parse tree in preorder.

Muhammad Idrees, Lecturer University of Lahore 2 Top-Down Parsing(cont’d) We now consider a general form of top down parsing, called recursive descent, that may involve backtracking, that is making repeated scans of the input.However, backtracking parsers are not seen frequently

Muhammad Idrees, Lecturer University of Lahore 3 Top-Down Parsing(cont’d) One reason is that backtracking is rarely needed to parse programming language constructs. In situations like natural language parsing, backtracking is still not very efficient, and tabular methods such as the dynamic programming algorithm or method of Earley are preferred.

Muhammad Idrees, Lecturer University of Lahore 4 Example. Consider the grammar, S cAd A ab|a and the input string w = cad. To construct a parse tree for this string top down, we initially create a tree consisting of a single node labeled S.

Muhammad Idrees, Lecturer University of Lahore 5 Example (cont’d) An input pointer points to “c”, the first symbol of w. We then use the first production for S to expand the tree and obtain the tree of the Fig.(a) S Fig(a) c Ad

Muhammad Idrees, Lecturer University of Lahore 6 Example (cont’d) The leftmost leaf, labeled “c”, matches the first symbol of w,So we now advance the input pointer to “a”,the second symbol of w, and consider the next leaf, labeled A. We can then expand A using the first alternative for A to obtain the tree of the fig(b).

Muhammad Idrees, Lecturer University of Lahore 7 Example (cont’d) S c A d a b Fig(b)

Muhammad Idrees, Lecturer University of Lahore 8 Example (cont’d) We now have a match for the second input symbol so we advance the input pointer to “d”, the third input symbol and compare “d” against the next leaf, labeled “b”. Since “b” does not match “d”, we report failure and go back to A to see whether there is another alternative for A that we have not tried but that might produce a match.

Muhammad Idrees, Lecturer University of Lahore 9 Example (cont’d) S c Ad a Fig(c)

Muhammad Idrees, Lecturer University of Lahore 10 Example (cont’d) In going back to A, we must reset the input pointer to position 2, the position it had when we first came to A, which means that the procedure for A must store the input pointer in a local variable. We now try the second alternative for A to obtain the tree of the fig(c). The leaf “a” matches the second symbol w and the leaf “d” matches the third symbol. Since we have produced a parse tree for w, we halt and announce successful completion of parsing.

Muhammad Idrees, Lecturer University of Lahore 11 Top-down Parsing To find a leftmost derivation for an input string Construct a parse tree from the root Example S  cAd A  ab | a Input w = cad S cAd S cAd S cAd ab a

Muhammad Idrees, Lecturer University of Lahore 12 Example input: cad cad S cd A S cd A ab S cd A ab Problem: backtrack cad S cd A a S cd A a Example: S  c A d A  ab | a

Muhammad Idrees, Lecturer University of Lahore 13 Parsing – Top-Down & Predictive Top-Down Parsing  Parse tree / derivation of a token string occurs in a top down fashion. For Example, Consider : type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num Suppose input is : array [ num dotdot num ] of integer Parsing would begin with type  ??? Start symbol

Muhammad Idrees, Lecturer University of Lahore 14 Top-Down Parse type]simpleof [ array type ]simpleof[array type num dotdot Input : array [ num dotdot num ] of integer Lookahead symbol type ? Input : array [ num dotdot num ] of integer Lookahead symbol type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num Start symbol

Muhammad Idrees, Lecturer University of Lahore 15 Top-Down Parse Input : array [ num dotdot num ] of integer type]simpleof[array type num dotdotsimple type]simpleof[array type num dotdotsimple integer Lookahead symbol type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num Start symbol

Muhammad Idrees, Lecturer University of Lahore 16 Recursive Descent or Predictive Parsing Parser Operates by Attempting to Match Tokens in the Input Stream Utilize both Grammar and Input Below to Motivate Code for Algorithm array [ num dotdot num ] of integer type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num procedure match ( t : token ) ; begin if lookahead = t then lookahead : = nexttoken else error end ;

Muhammad Idrees, Lecturer University of Lahore 17 Top-down algorithm (continued) procedure simple ; begin if lookahead = integer then match ( integer ); else if lookahead = char then match ( char ); else if lookahead = num then begin match (num); match (dotdot); match (num) end else error end ; type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num

Muhammad Idrees, Lecturer University of Lahore 18 Top-Down Algorithm (Continued) procedure type ; begin if lookahead is in { integer, char, num } then simple else if lookahead = ‘  ’ then begin match (‘  ’ ) ; match( id ) end else if lookahead = array then begin match( array ); match(‘[‘); simple; match(‘]’); match(of); type end else error end ; type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num

Muhammad Idrees, Lecturer University of Lahore 19 Tracing Input: array [ num dotdot num ] of integer To initialize the parser: set global variable : lookahead = array call procedure: type Procedure call to type with lookahead = array results in the actions: match( array ); match(‘[‘); simple; match(‘]’); match(of); type Procedure call to simple with lookahead = num results in the actions: match (num); match (dotdot); match (num) Procedure call to type with lookahead = integer results in the actions: simple Procedure call to simple with lookahead = integer results in the actions: match ( integer ) type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num

Muhammad Idrees, Lecturer University of Lahore 20 Compiler Phases – Front End Semantic Action Checking Scanner Parser Intermediate Representation Intermediate Representation Semantic Error Request Token Get Token Start

Muhammad Idrees, Lecturer University of Lahore 21 Big Picture  Parsing: Matching code we are translating to rules of a grammar. Building a representation of the code.  Scanning: An abstraction that simplifies the parsing process by converting the raw text input into a stream of known objects called tokens.  Grammar dictates syntactic rules of a language i.e, how a legal sentence in a language could be formed  Lexical rules of a language dictate how a legal word in a language is formed by concatenating alphabet of the language.

Muhammad Idrees, Lecturer University of Lahore 22 Overall Operation Parser is in control of the overall operation Demands scanner to produce a token Scanner reads input file into token buffer & forms a token Token is returned to parser Parser attempts to match the token Failure: Syntax Error! Success: – Does nothing and returns to get next token or – Takes Semantic Action

Muhammad Idrees, Lecturer University of Lahore 23 Overall Operation Semantic Action: Lookup variable name – If found okay – If not: Put in symbol table If semantic checks succeed, do code- generation Return to get next token No more tokens? Done!

Muhammad Idrees, Lecturer University of Lahore 24 Tokenization Input File Token Buffer  What does the Token Buffer contain? – Token being identified  Why a two-way ( ) street? – Characters can be read – and unread – Termination of a token

Muhammad Idrees, Lecturer University of Lahore 25 Example main() m

Muhammad Idrees, Lecturer University of Lahore 26 Example main() am

Muhammad Idrees, Lecturer University of Lahore 27 Example main() iam

Muhammad Idrees, Lecturer University of Lahore 28 Example main() niam

Muhammad Idrees, Lecturer University of Lahore 29 Example main() (niam

Muhammad Idrees, Lecturer University of Lahore 30 Example main() niam Keyword : main

Muhammad Idrees, Lecturer University of Lahore 31 Overall Operation Parser is in control of the overall operation Demands scanner to produce a token Scanner reads input file into token buffer & forms a token Token is returned to parser Parser attempts to match the token Failure: Syntax Error! Success: – Does nothing and returns to get next token OR – Takes Semantic Action

Muhammad Idrees, Lecturer University of Lahore 32 Overall Operation Semantic Action: Lookup variable name – If found okay – If not: Put in symbol table If semantic checks succeed, do code- generation Return to get next token No more tokens? Done!

Muhammad Idrees, Lecturer University of Lahore 33 Grammar Rules  MAIN OPENPAR CLOSEPAR  NULL  VAR , VAR  NULL  CURLYOPEN CURLYCLOSE  VAR ;  VAR = ;  VAR  +  -  INT  FLOAT

Muhammad Idrees, Lecturer University of Lahore 34 Demo main() { int a,b; a = b; } Parser Scanner Token Buffer

Muhammad Idrees, Lecturer University of Lahore 35 Demo main() { int a,b; a = b; } Parser Scanner "Please, get me the next token" Token Buffer

Muhammad Idrees, Lecturer University of Lahore 36 Demo main() { int a,b; a = b; } Parser Scanner m

Muhammad Idrees, Lecturer University of Lahore 37 Demo main() { int a,b; a = b; } Parser Scanner am

Muhammad Idrees, Lecturer University of Lahore 38 Demo main() { int a,b; a = b; } Parser Scanner iam

Muhammad Idrees, Lecturer University of Lahore 39 Demo main() { int a,b; a = b; } Parser Scanner niam

Muhammad Idrees, Lecturer University of Lahore 40 Demo main() { int a,b; a = b; } Parser Scanner (niam

Muhammad Idrees, Lecturer University of Lahore 41 Demo main() { int a,b; a = b; } Parser Scanner niam

Muhammad Idrees, Lecturer University of Lahore 42 Demo main() { int a,b; a = b; } Parser Scanner Token: main Token Buffer

Muhammad Idrees, Lecturer University of Lahore 43 Demo main() { int a,b; a = b; } Parser Scanner "I recognize this" Token Buffer

Muhammad Idrees, Lecturer University of Lahore 44 Parsing (Matching) Start matching using a rule When match takes place at a certain position, move further (get next token & repeat the process) If expansion needs to be done, choose appropriate rule (How to decide which rule to choose?) If no rule found, declare error If several rules found the grammar (set of rules) is ambiguous Grammar ambiguous? Language ambiguous?

Muhammad Idrees, Lecturer University of Lahore 45 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner "Please, get me the next token"

Muhammad Idrees, Lecturer University of Lahore 46 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: MAIN  MAIN OPENPAR CLOSEPAR

Muhammad Idrees, Lecturer University of Lahore 47 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner "Please, get me the next token"  MAIN OPENPAR CLOSEPAR

Muhammad Idrees, Lecturer University of Lahore 48 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: OPENPAR  MAIN OPENPAR CLOSEPAR

Muhammad Idrees, Lecturer University of Lahore 49 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: CLOSEPAR  MAIN OPENPAR CLOSEPAR  NULL

Muhammad Idrees, Lecturer University of Lahore 50 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: CLOSEPAR  MAIN OPENPAR CLOSEPAR  NULL

Muhammad Idrees, Lecturer University of Lahore 51 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: CLOSEPAR  MAIN OPENPAR CLOSEPAR

Muhammad Idrees, Lecturer University of Lahore 52 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: CURLYOPEN  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE

Muhammad Idrees, Lecturer University of Lahore 53 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: INT  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR ;  INT

Muhammad Idrees, Lecturer University of Lahore 54 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: INT  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR ;  INT

Muhammad Idrees, Lecturer University of Lahore 55 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: INT  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR ;  INT

Muhammad Idrees, Lecturer University of Lahore 56 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: VAR  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR ; , VAR  NULL

Muhammad Idrees, Lecturer University of Lahore 57 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: ',' [COMMA]  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR ; , VAR  NULL

Muhammad Idrees, Lecturer University of Lahore 58 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: VAR  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR ; , VAR  NULL

Muhammad Idrees, Lecturer University of Lahore 59 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: ';'  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR ; , VAR  NULL

Muhammad Idrees, Lecturer University of Lahore 60 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: ';'  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR ; , VAR  NULL

Muhammad Idrees, Lecturer University of Lahore 61 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: ';'  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR ; , VAR  NULL

Muhammad Idrees, Lecturer University of Lahore 62 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: ';'  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR ; , VAR  NULL

Muhammad Idrees, Lecturer University of Lahore 63 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: ';'  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR ;

Muhammad Idrees, Lecturer University of Lahore 64 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: ';'  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR ;

Muhammad Idrees, Lecturer University of Lahore 65 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: VAR  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR = ;  VAR

Muhammad Idrees, Lecturer University of Lahore 66 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: '='  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR = ;  VAR

Muhammad Idrees, Lecturer University of Lahore 67 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: VAR  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR = ;  VAR

Muhammad Idrees, Lecturer University of Lahore 68 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: VAR  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR = ;  VAR

Muhammad Idrees, Lecturer University of Lahore 69 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: VAR  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR = ;  VAR

Muhammad Idrees, Lecturer University of Lahore 70 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: ';'  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR = ;

Muhammad Idrees, Lecturer University of Lahore 71 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: ';'  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE  VAR = ;

Muhammad Idrees, Lecturer University of Lahore 72 Scanning & Parsing Combined main() { int a,b; a = b; } Parser Scanner Token: CURLYCLOSE  MAIN OPENPAR CLOSEPAR  CURLYOPEN CURLYCLOSE