Chapter 4 Top-Down Parsing Recursive-Descent Gang S. Liu College of Computer Science & Technology Harbin Engineering University.

Slides:



Advertisements
Similar presentations
Session 14 (DM62) / 15 (DM63) Recursive Descendent Parsing.
Advertisements

Top-Down Parsing.
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 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.
Professor Yihjia Tsai Tamkang University
Top-Down Parsing.
Chapter 3 Chang Chi-Chung Parse tree intermediate representation The Role of the Parser Lexical Analyzer Parser Source Program Token Symbol.
Lexical and syntax analysis
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. Goals of Parsing Check the input for syntactic accuracy Return appropriate error messages Recover if possible Produce, or at least traverse,
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.
Chapter 9 Syntax Analysis Winter 2007 SEG2101 Chapter 9.
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
Chapter 5 Top-Down Parsing.
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 Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
-Mandakinee Singh (11CS10026).  What is parsing? ◦ Discovering the derivation of a string: If one exists. ◦ Harder than generating strings.  Two major.
Context-Free Grammars and Parsing
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax (cont.) Lecture 4 – Syntax (Cont.), Spring CSE3302 Programming Languages,
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 3.
Lesson 5 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Syntax and Semantics Structure of programming languages.
Joey Paquet, 2000, Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic Analysis.
6/4/2016IT 3271 The most practical Parsers: Predictive parser: 1.input (token string) 2.Stacks, parsing table 3.output (syntax tree, intermediate codes)
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Compiler Principle and Technology Prof. Dongming LU Mar. 27th, 2015.
Recursive Descent Parsers Lecture 6 Mon, Feb 2, 2004.
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.
More Parsing CPSC 388 Ellen Walker Hiram College.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
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)
Compiler Principle and Technology Prof. Dongming LU Mar. 12th, 2014.
ISBN Chapter 4 Lexical and Syntax Analysis.
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.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Chapter 2 (part) + Chapter 4: Syntax Analysis S. M. Farhad 1.
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #6 Parsing.
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.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Syntax and Semantics Structure of programming languages.
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
COMPILER CONSTRUCTION
Programming Languages Translator
CS510 Compiler Lecture 4.
Context free grammars Terminals Nonterminals Start symbol productions
Lecture #12 Parsing Types.
Chapter 3 Context-Free Grammar and Parsing
Parsing.
Syntax Analysis Chapter 4.
Chapter 4 Top-Down Parsing Part-1 September 8, 2018
4 (c) parsing.
Syntax-Directed Definition
CSC 4181 Compiler Construction Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
COMPILER CONSTRUCTION
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Chapter 4 Top-Down Parsing Recursive-Descent Gang S. Liu College of Computer Science & Technology Harbin Engineering University

Top-down Parsing A top-down parsing algorithm parses an input string of tokens by tracing out the steps in a leftmost derivation. The traversal of the parse tree occurs from the root to the leaves. Two forms of top-down parsing: 1.Predictive parsers. Attempts to predict the next construction in the input string using one or more lookahead tokens. 2.Backtracking parsers. Tries different possibilities for a parse of the input, backing up an arbitrary amount in the input. May require exponential time

Examples (1) exp => exp op exp (2) => number op exp (3) => number + exp (4) => number + number exp exp op exp number + number exp exp op exp number + number (1) exp => exp op exp (2) => exp op number (3) => exp + number (4) => number + number Leftmost derivation Rightmost derivation Preorder numbering The reverse of a Postorder numbering

Two Kinds of Top-Down Parsing 1.Recursive-descent parsing Versatile Suitable for handwritten parser 2.LL(1) parsing No longer often used Simple scheme with explicit stack Prelude for more powerful and complex bottom-up algorithms First “L” – the input is processed from left to right Second “L” – leftmost derivation 1 – one lookahead symbol

match matches the current token with its parameter, advances the input if it succeeds. match(expToken) if token = expToken then getToken(); else error; endif; Recursive-Descent The grammar rule for a nonterminal A is viewed as a definition for a procedure that will recognize an A. exp → exp addop term | term addop → + | - term → term mulop factor | factor mulop → * factor → (exp) | number factor() switch token case(: match((); exp(); match()); break; case number: match(number); break; default: error;

Choice ifStmt() match(if); match ((); exp(); match()); statement(); if token = else then match (else); statement(); end if; statement → if-stmt | other if-stmt → if (exp) statement [ else statement ] exp → 0 | 1 EBNF is designed to mirror closely the actual code for recursive-descent parser.

Repetition exp → exp addop term | term exp → term { addop term } exp() term(); while token = + or token = - do match (token); term(); end while; Left recursive grammar: A ::= A α | β –Equivalent to β α*

Problems with Recursive-Descent 1.It may be difficult to convert a grammar into EBNF. 2.It may be difficult to distinguish two or more grammar rule options A → α | β, if both α and β begin with nonterminals. (First set) 3.A → ε. It may be necessary to know what token can come after the nonterminal A. (Follow set)

Reporting Errors At a minimum, any parser must indicate that some error exists, if a program contains a syntax error. Usually, a parser will attempt to give a meaningful error message and determine the location where that error has occurred. Some parsers may attempt some form of error correction.

General Principles 1.A parser should determine that an error has occurred as soon as possible. 2.The parser must pick a place to resume the parse. A parser must try to parse as much of the code as possible. 3.A parser should try to avoid the error cascade problem. 4.A parser must avoid infinite loops an errors.

Panic Mode A standard form of error recovery in recursive- descent parsers is called panic mode. The basic mechanism - a set of synchronizing tokens. –Tokens may be added to the set as parsing proceeds. –If error is encountered, the parser scans ahead until it sees one of the synchronizing tokens. Then parsing is resumed. –Error cascades are avoided. What tokens to add to the set? –Symbols like semicolons, commas, parentheses

Homework 4.2 Given the grammar A → ( A ) A | ε, write pseudocode to parse this grammar by recursive- descent. 4.3 Given the grammar Write pseudocode to parse this grammar by recursive-descent. statement → assign-stmt | call-stmt | other assign-stmt → identifier := exp call-stmt → identifier ( exp-list )