Ambiguity in Grammar, Error Recovery

Slides:



Advertisements
Similar presentations
Error Handling A compiler should: detect errors locate errors recover from errors Errors may occur in any of the three main analysis phases of compiling:
Advertisements

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.
CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155.
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Compiler Designs and Constructions
 CS /11/12 Matthew Rodgers.  What are LL and LR parsers?  What grammars do they parse?  What is the difference between LL and LR?  Why do.
Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
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.
1 LR parsing techniques SLR (not in the book) –Simple LR parsing –Easy to implement, not strong enough –Uses LR(0) items Canonical LR –Larger parser but.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
1 Bottom-up parsing Goal of parser : build a derivation –top-down parser : build a derivation by working from the start symbol towards the input. builds.
Bottom-up parsing Goal of parser : build a derivation
– 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.
LALR Parsing Canonical sets of LR(1) items
1 Compiler Construction Syntax Analysis Top-down parsing.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
11 Outline  6.0 Introduction  6.1 Shift-Reduce Parsers  6.2 LR Parsers  6.3 LR(1) Parsing  6.4 SLR(1)Parsing  6.5 LALR(1)  6.6 Calling Semantic.
Chapter 3-3 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR 
1 Bottom-Up Parsing  “Shift-Reduce” Parsing  Reduce a string to the start symbol of the grammar.  At every step a particular substring is matched (in.
Recursive Descent Parsers Lecture 6 Mon, Feb 2, 2004.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Three kinds of bottom-up LR parser SLR “Simple LR” –most restrictions on eligible grammars –built quite directly from items as just shown LR “Canonical.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Lecture 5: LR Parsing CS 540 George Mason University.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.
Overview of Previous Lesson(s) Over View  Structure of the LR Parsing Table  Consists of two parts: a parsing-action function ACTION and a goto function.
Error recovery in predictive parsing An error is detected during the predictive parsing when the terminal on top of the stack does not match the next input.
Syntax error handling –Errors can occur at many levels lexical: unknown operator syntactic: unbalanced parentheses semantic: variable never declared runtime:
Lec04-bottomupparser 4/13/2018 LR Parsing.
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Bottom-up parsing Goal of parser : build a derivation
Compiler design Bottom-up parsing Concepts
Bottom-Up Parsing.
lec04-bottomupparser June 6, 2018 Bottom-Up Parsing
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
Table-driven parsing Parsing performed by a finite state machine.
Chapter 4 Syntax Analysis.
Compiler Construction
Compiler design Bottom-up parsing: Canonical LR and LALR
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Syntax Analysis Part II
Shift Reduce Parsing Unit -3
Subject Name:COMPILER DESIGN Subject Code:10CS63
Lecture 7 Predictive Parsing
4d Bottom Up Parsing.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Design 7. Top-Down Table-Driven Parsing
Bottom Up Parsing.
Compiler SLR Parser.
Bottom-Up Parsing “Shift-Reduce” Parsing
Computing Follow(A) : All Non-Terminals
4d Bottom Up Parsing.
Lecture 7 Predictive Parsing
Kanat Bolazar February 16, 2010
Parsing Bottom-Up.
Parsing Bottom-Up LR Table Construction.
4d Bottom Up Parsing.
Parsing Bottom-Up LR Table Construction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
Chap. 3 BOTTOM-UP PARSING
Compiler design Bottom-up parsing: Canonical LR and LALR
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

10.10.06 - Ambiguity in Grammar, Error Recovery By, J.Siddhartha Reddy 04CS1028

ERROR RECOVERY IN LR PARSING An LR Parser will detect an error when it consults parsing table and finds an error entry. A canonical parser will never make even a single reduction before announcing an error. The SLR and LALR parsers may take several reductions before announcing an error, but they will never shift an erroneous input into the stack.. We can implement two modes of recovery :

Panic Mode ·        We scan down the stack until a state s with a goto on a particular non-terminal A is found. Zero or more input symbols are then discarded until a symbol a is found that can legitimately follow A. The parser then stack the state goto[ s, A] and resume normal parsing. Normally there may be many choices for the non terminal A. Normally these would be non-terminals representing major program pieces, such as an expression, statement, or block.

Phrase Level Recovery It is implemented by examining each error entry in the LR parsing table and deciding on the basis of language the most likely program error that give rise to that error entry in the LR parsing table. An appropriate error procedure than can be implemented; presumably the top of the stack and/or first input symbols would be modified in a way deemed appropriate for each error.

As an example consider the grammar (1). E  E + E | E * E | ( E ) | id ---- (1) The parsing table contains error routines that have effect of detecting error before any shift move takes place.

The LR parsing table with error routines   Id + * ( ) $ E s3 e1 s2 e2 1 e3 s4 s5 acc 2 6 3 r4 4 7 5 8 s9 e4 r1 r2 9 r3

Error routines : e1 This routine is called from states 0,2,4 and 5, all of which the beginning of the operand, either an id of left parenthesis. Instead an operator, + or *, or the end of input was found. Action: Push an imaginary id on to the stack and cover it with a state 3. (the goto of the states 0, 2, 4 and 5) Print: Issue diagnostic “missing operand”

Error routines : e2 This routine is called from states 0, 1, 2, 4 and 5 on the finding a right parenthesis. Action: Remove the right parenthesis from the input Print: Issue diagnostic “Unbalanced right parenthesis”

Error routines : e3 This routine is called from states 1 or 6 when expecting an operator, and an id or right parenthesis is found. Action: Push + onto the stack and cover it with state 4. Print: Issue diagnostic “Missing operator”

Error routines : e4 This routine is called from state 6 when the end of input is found while expecting operator or a right parenthesis. Action: Push a right parenthesis onto the stack and cover it with a state 9. Print: Issue diagnostic “Missing right parenthesis”