Recursive descent parsing

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

Grammar and Algorithm }
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.
Lexical and Syntactic Analysis Here, we look at two of the tasks involved in the compilation process –Given source code, we need to first break it into.
1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing LR Parsing Lex and yacc Semantic Processing.
Parsing III (Eliminating left recursion, recursive descent parsing)
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
Top-Down Parsing.
– 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.
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) )
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
Lexical and Syntax Analysis
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.
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 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.
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX - Recursive descent parsing Programming Language.
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX –Recursive descent parsing Programming Language.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Parsing III (Top-down parsing: recursive descent & LL(1) )
4.1 Introduction - Language implementation systems must analyze
Lexical and Syntax Analysis
Parsing III (Top-down parsing: recursive descent & LL(1) )
Chapter 4 - Parsing CSCE 343.
Chapter 4 Lexical and Syntax Analysis.
Lexical and Syntax Analysis
Lecture #12 Parsing Types.
Parsing IV Bottom-up Parsing
Table-driven parsing Parsing performed by a finite state machine.
Top-down parsing cannot be performed on left recursive grammars.
Lexical and Syntax Analysis
Syntax Analysis Sections :.
Lexical and Syntax Analysis
Programming Language Syntax 7
Lexical and Syntax Analysis
Top-Down Parsing CS 671 January 29, 2008.
Lexical and Syntactic Analysis
Lecture 7 Predictive Parsing
CS 540 George Mason University
Lecture 9 SLR Parse Table Construction
Compiler Design 7. Top-Down Table-Driven Parsing
Chapter 2: A Simple One Pass Compiler
Top-Down Parsing Identify a leftmost derivation for an input string
Lecture 8: Top-Down Parsing
Programming Language Syntax 5
Ambiguity in Grammar, Error Recovery
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
LL and Recursive-Descent Parsing
Computing Follow(A) : All Non-Terminals
Chapter 4 Action Routines.
Chapter 4: Lexical and Syntax Analysis Sangho Ha
Lexical and Syntax Analysis
Lecture 7 Predictive Parsing
Recursive descent parsing
Nonrecursive Predictive Parsing
Compiler Construction
Lexical and Syntax Analysis
Predictive Parsing Program
Lexical and Syntax Analysis
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 2, 09/04/2003 Prof. Roy Levow.
CH 4 - Language semantics
4.1 Introduction - Language implementation systems must analyze
PZ03BX - Recursive descent parsing
Compiler design Review COMP 442/6421 – Compiler Design
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Recursive descent parsing Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 3.4

Recursive descent parsing overview A simple parsing algorithm Shows the relationship between the formal description of a programming language and the ability to generate executable code for programs in the language. Use extended BNF for a grammar, e.g., expressions: <arithmetic expression>::=<term>{[+|-]<term>}* Consider the recursive procedure to recognize this: procedure Expression; begin Term; /* Call Term to find first term */ while ((nextchar=`+') or (nextchar=`-')) do nextchar:=getchar; /* Skip over operator */ Term end

Generating code Assume each procedure outputs its own postfix (Section 8.2, to be discussed later) To generate code, need to output symbols at appropriate places in procedure. procedure Expression; begin Term; /* Call Term to find first term */ while ((nextchar=`+') or (nextchar=`-')) do nextchar:=getchar; /* Skip over operator */ Term; output previous ‘+’ or ‘-’; end

Generating code (continued) Each non-terminal of grammar becomes a procedure. Each procedure outputs its own postfix. Examples: procedure Term; begin Primary; while ((nextchar=`*') or (nextchar=`/')) do nextchar:=getchar; /* Skip over operator */ Primary; output previous ‘*’ or ‘/’; end Procedure Identifier; if nextchar= letter output letter else error; nextchar=getchar; Figure 3.13 of text has complete parser for expressions.

Recursive Descent Parsing Recall the expression grammar, after transformation This produces a parser with six mutually recursive routines: • Goal • Expr • EPrime • Term • TPrime • Factor Each recognizes one NT or T The term descent refers to the direction in which the parse tree is built.

Recursive Descent Parsing A couple of routines from the expression parser

Transition diagrams for the grammar  E E' T T' F  TE' +TE' |  FT' *FT' |  (E) | id  Transition diagrams for the grammar

Simplified transition diagrams.   (a) (b) (c) (d) Simplified transition diagrams.

Simplified transition diagrams for arithmetic expressions.  Simplified transition diagrams for arithmetic expressions.

Example transition diagrams Corresponding transition diagrams: An expression grammar with left recursion and ambiguity removed: E’ -> + T E’ | ε T -> F T’ T’ -> * F T’ | ε F -> ( E ) | id E -> T E’

Predictive parsing without recursion To get rid of the recursive procedure calls, we maintain our own stack.

Example Use the table-driven predictive parser to parse id + id * id Assuming parsing table Initial stack is $E Initial input is id + id * id $

LR parsing

LR parsing example Grammar: 1. E -> E + T 2. E -> T 3. T -> T * F 4. T -> F 5. F -> ( E ) 6. F -> id