1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.

Slides:



Advertisements
Similar presentations
Parsing 4 Dr William Harrison Fall 2008
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.
Session 14 (DM62) / 15 (DM63) Recursive Descendent Parsing.
LESSON 18.
Top-Down Parsing.
Context-Free Grammars Lecture 7
Parsing III (Eliminating left recursion, recursive descent parsing)
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Discussion #51/18 Discussion #5 LL(1) Grammars &Table-Driven Parsing.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
Professor Yihjia Tsai Tamkang University
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.
Lexical and syntax analysis
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
Parsing IV Bottom-up Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice 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.
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.
Chapter 5 Top-Down Parsing.
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.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Syntax and Semantics Structure of programming languages.
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.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Chapter 4 Top-Down Parsing Recursive-Descent Gang S. Liu College of Computer Science & Technology Harbin Engineering University.
Compiler Principle and Technology Prof. Dongming LU Mar. 27th, 2015.
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.
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 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.
Compiler Principle and Technology Prof. Dongming LU Mar. 12th, 2014.
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.
Top-Down Parsing.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
8 January 2004 Department of Software & Media Technology 1 Top Down Parsing Recursive Descent Parsing Top-down parsing: –Build tree from root symbol –Each.
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 ( )
Parsing III (Top-down parsing: recursive descent & LL(1) )
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
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.
Syntax Analysis By Noor Dhia Left Recursion: Example1: S → S0s1s | 01 The grammar after eliminate left recursion is: S → 01 S’ S' → 0s1sS’
Syntax and Semantics Structure of programming languages.
COMPILER CONSTRUCTION
Parsing — Part II (Top-down parsing, left-recursion removal)
Programming Languages Translator
Parsing — Part II (Top-down parsing, left-recursion removal)
Parsing.
Chapter 4 Top-Down Parsing Part-1 September 8, 2018
Compiler Construction (CS-636)
4 (c) parsing.
Parsing Techniques.
CSC 4181 Compiler Construction Parsing
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
Parsing — Part II (Top-down parsing, left-recursion removal)
LL and Recursive-Descent Parsing
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi

Outline 1. Top-Down Parsing 2. Recursive-Decent Parsing 3. LL(1) Parsing 4. Error-Recovery in Top-Down Parsers 5. Summary 2

Top-Down Parsing Lecture: 7-8 3

Practical Work Write a regular expression that generates same language as the following grammar A  aA | B |  B  bB | A Write derivations for the string “abba” and construct numbered Parse Tree for it 4

Top-Down Parsing A top-down parsing algorithm parses an input string of tokens by tracing out the steps in left-most derivation  Parse trees implies preorder tree traversals Top-down parsers come in two forms; backtracking parsers and predictive parsers  A predictive parser attempts to predict the next construction in the input string using one or more lookahead tokens  A backtracking parser will try different possibilities for a parse of the input, backing up an arbitrary amount in the input if one possibility fails 5

Top-Down Parsing (Continue…) Backtracking parsers are more powerful than predictive parsers Backtracking parsers are much slower, requiring exponential time in general and, therefore, are unusable for practical compilers Two popular top-down parsing algorithms 1. Recursive-Decent Parsing 2. LL(1) Parsing 6

Recursive Decent Parsing The idea of Recursive Decent Algorithm is simple;  We view the grammar rule for a nonterminal A as a definition for a procedure that will recognize an A  The right hand side of the grammar rule for A specifies the structure of the code for this procedure  The sequence of terminals on RHS correspond to matches of input  The sequence of non terminals on RHS correspond to call to other procedures  The choices on RHS correspond to alternatives (case or if- statements) within the code 7

Recursive Decent Example Grammar rule: factor  ( exp ) | number Code: void factor(void) { if(token == number) match(number); else { match(‘(‘); exp(); match(‘)’); } 8

Recursive Decent Example (Discussion) How lookahead is not a problem in this example?  if the token is number, go one way, if the token is ‘(‘ go the other, and if the token is neither, declare error void match(Token expect) { if (token == expect) getToken(); else error(token,expect); } 9

Error Handling in RD is Tricky! If an error occurs, we must somehow gracefully exit possibly many recursive calls Best solution: use exception handling to manage stack unwinding (which C doesn’t have!) But there are worse problems: left recursion doesn’t work! 10

Left Recursion is Impossible Grammar exp  exp addop term | term Code void exp(void) { if (token == ??) { exp(); // uh, oh!! addop(); term(); } else term(); } 11

Repetition & Choice: Using EBNF Consider the following grammar and try to write its pseudo code for recursive descent; if-stmt  if ( exp ) statement | if ( exp ) statement else statement Consider the following grammar and try to convert it in recursive descent compatible grammar: exp  exp addop term | term 12

LL(1) Parsing LL(1) is top-down parsing algorithm  First ‘L’ means it processes input from left to right  Second ‘L’ means it traces out a leftmost derivation for the input string  The ‘(1)’ means it uses only one symbol of input to predict the direction of the parse LL(1) parsing uses an explicit stack rather than recursive calls to perform a parse LL(1) performs two actions 1. Replace a nonterminal A from top of stack using grammar 2. Match a token on top of stack with the next input token 13

LL(1) Parsing (Continue…) 14 LL(1) parser uses following table information during parsing Grammar: S  ( S ) S |  Input: () NoParsing StackInputAction 1$ S( ) $ S  ( S ) S 2$ S ) S (( ) $Match 3$ S ) S ) $ S   4$ S ) ) $Match 5$ S $ S  S   6$ $Accept

Removing Left Recursion LL(1) suffers the same problem due to left recursion as RD does EBNF is not a solution for LL(1) hence we need to rewrite grammar and remove left recursion from it Consider the following case A  Aα | β  Here α and β are combination of terminals and nonterminals where β does not begin with A  This type of grammar will generate string of type β[αα….]  The resultant will be:A  βA’ A’  αA’ |  15

Left Factoring Left factoring is required when two or more grammar rule choices share a common prefix string, as in the rule A  αβ | αγ Obviously an LL(1) parser cannot distinguish between the production choices in such a situation The solution in this case is to ‘factor’ the α out in the left and rewrite the rule as two rules; A  αA’ A’  β | γ 16

Error Recovery in Top-Down Parsers A parser should try to determine that an error has occurred as soon as possible. Waiting too long before declaring error means the location of the actual error may have been lost After an error has occurred, the parser must pick a likely place to resume the parse. A parser should always try to parse as much of the code as possible, in order to find as many real errors as possible during a single translation 17

Error Recovery in Top-Down Parsers (Continue…) A parser should try to avoid the error cascade problem, in which one error generates a lengthy sequence of spurious error messages A parser must avoid infinite loops on errors, in which an unending cascade of error messages is generated without consuming any input 18

19 Summary Any Questions?