Recursive Descent Parsing Top-down parsing: build tree from root symbol Each production corresponds to one recursive procedure Each procedure recognizes.

Slides:



Advertisements
Similar presentations
Exercise: Balanced Parentheses
Advertisements

Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Exercise 1: Balanced Parentheses Show that the following balanced parentheses grammar is ambiguous (by finding two parse trees for some input sequence)
Lecture # 11 Grammar Problems.
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.
LESSON 18.
Honors Compilers An Introduction to Grammars Feb 12th 2002.
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Parsing III (Eliminating left recursion, recursive descent parsing)
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem 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.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
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.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
MIT Top-Down Parsing Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
Recursive Descent Parsing Top-down parsing: build tree from root symbol Each production corresponds to one recursive procedure Each procedure recognizes.
Lexical and syntax analysis
CPSC 388 – Compiler Design and Construction
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Syntax and Semantics Structure of programming languages.
4 4 (c) parsing. Parsing A grammar describes the strings of tokens that are syntactically legal in a PL A recogniser simply accepts or rejects strings.
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.
# 1 CMPS 450 Parsing CMPS 450 J. Moloney. # 2 CMPS 450 Check that input is well-formed Build a parse tree or similar representation of input Recursive.
Parsing III (Top-down parsing: recursive descent & LL(1) )
-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.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Syntax and Semantics Structure of programming languages.
Joey Paquet, 2000, Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic 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.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
Parsing III (Top-down parsing: recursive descent & LL(1) ) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
CPS 506 Comparative Programming Languages Syntax Specification.
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
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 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.
TOP-DOWN PARSING Recursive-Descent, Predictive Parsing.
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.
Top-Down Parsing.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
8 January 2004 Department of Software & Media Technology 1 Top Down Parsing Recursive Descent Parsing Top-down parsing: –Build tree from root symbol –Each.
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.
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #6 Parsing.
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
Parsing III (Top-down parsing: recursive descent & LL(1) )
Top-down parsing 1. Last Time CYK – Step 1: get a grammar in Chomsky Normal Form – Step 2: Build all possible parse trees bottom-up Start with runs of.
Lecture # 10 Grammar Problems. Problems with grammar Ambiguity Left Recursion Left Factoring Removal of Useless Symbols These can create problems for.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
Syntax and Semantics Structure of programming languages.
Parsing — Part II (Top-down parsing, left-recursion removal)
A Simple Syntax-Directed Translator
Programming Languages Translator
Table-driven parsing Parsing performed by a finite state machine.
Parsing — Part II (Top-down parsing, left-recursion removal)
Top-Down Parsing.
Programming Language Syntax 7
Parsing — Part II (Top-down parsing, left-recursion removal)
LL and Recursive-Descent Parsing
Predictive Parsing Program
Presentation transcript:

Recursive Descent Parsing Top-down parsing: build tree from root symbol Each production corresponds to one recursive procedure Each procedure recognizes an instance of a non-terminal, returns tree fragment for the non-terminal

General model Each right-hand side of a production provides body for a function Each non-terminal on the rhs is translated into a call to the function that recognizes that non-terminal Each terminal in the rhs is translated into a call to the lexical scanner. Error if the resulting token is not the expected terminal. Each recognizing function returns a tree fragment.

Example: parsing a declaration FULL_TYPE_DECLARATION ::= type DEFINING_IDENTIFIER is TYPE_DEFINITION ; Translates into: get token type Find a defining_identifier -- function call get token is Recognize a type_definition -- function call get token semicolon In practice, we already know that the first token is type, that’s why this routine was called in the first place! Predictive parsing is guided by the next token

Example: parsing a loop FOR_STATEMENT ::= ITERATION_SCHEME loop STATEMENTS end loop; Node1 := find_iteration_scheme; -- call function get token loop List1 := Sequence of statements -- call function get token end get token loop get token semicolon; Result := build loop_node with Node1 and List1 return Result

Complications If there are multiple productions for a non- terminal, we need a mechanism to determine which production to use IF_STAT ::= if COND then Stats end if; IF_STAT ::= if COND then Stats ELSIF_PART end if; When next token is if, can’t tell which production to use.

Solution: factorize grammar If several productions have the same prefix, rewrite as single production: IF_STAT ::= if COND then STATS [ELSIF_PART] end if; Problem now reduces to recognizing whether an optional Component (ELSIF_PART) is present

Complication: recursion Grammar cannot be left-recursive: E ::= E + T | T Problem: to find an E, start by finding an E… Original scheme leads to infinite loop: grammar is inappropriate for recursive- descent

Solution: remove left-recursion E ::= E + T | T means that eventually E expands into T + T + T …. Rewrite as: E ::= TE’ E’ ::= + TE’ | epsilon Informally: E’ is a possibly empty sequence of terms separated by an operator

Recursion can involve multiple productions A ::= B C | D B ::= A E | F Can be rewritten as: A ::= A E C | F C | D And then apply previous method General algorithm to detect and remove left- recursion from grammar (see ASU)

Further complication Transformation does not preserve associativity : E ::= E + T | T Parses a + b + c as (a + b) + c E ::= TE’, E’ ::= + TE’ | epsilon Parses a + b +c as a + (b + c) Incorrect for a - b – c : must rewrite tree

In practice: use loop to find sequence of terms Node1 := P_Term; -- call function that recognizes a term loop exit when Token not in Token_Class_Binary_Addop; Node2 := New_Node (P_Binary_Adding_Operator); Scan; -- past operator Set_Left_Opnd (Node2, Node1); Set_Right_Opnd (Node2, P_Term); -- find next term Set_Op_Name (Node2); Node1 := Node2; -- operand for next operation end loop;