Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.

Slides:



Advertisements
Similar presentations
CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155.
Advertisements

Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
Mooly Sagiv and Roman Manevich School of Computer Science
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #10, Feb. 14, 2007 Modified sets of item construction Rules for building LR parse tables.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
CS Summer 2005 Top-down and Bottom-up Parsing - a whirlwind tour June 20, 2005 Slide acknowledgment: Radu Rugina, CS 412.
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #10 Parsing.
Lecture #8, Feb. 7, 2007 Shift-reduce 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.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
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
Lexical and syntax analysis
LALR Parsing Canonical sets of LR(1) items
Parsing. Goals of Parsing Check the input for syntactic accuracy Return appropriate error messages Recover if possible Produce, or at least traverse,
Chap. 6, Bottom-Up Parsing J. H. Wang May 17, 2011.
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.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
1 Compiler Construction Syntax Analysis Top-down parsing.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
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.
CSI 3120, Syntactic analysis, page 1 Syntactic Analysis and Parsing Based on A. V. Aho, R. Sethi and J. D. Ullman Compilers: Principles, Techniques and.
Syntax and Semantics Structure of programming languages.
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.
Ambiguity in Grammar By Dipendra Pratap Singh 04CS1032.
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
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.
Syntax Analyzer (Parser)
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
Compiler Syntactic Analysis r Two general classes of parsing techniques m Bottom-up (Operator-Precedence parsing) Begin with the terminal nodes.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
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.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
Conflicts in Simple LR parsers A SLR Parser does not use any lookahead The SLR parsing method fails if knowing the stack’s top state and next input token.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
COMPILER CONSTRUCTION
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.
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
Bottom-Up Parsing.
Unit-3 Bottom-Up-Parsing.
Table-driven parsing Parsing performed by a finite state machine.
Chapter 4 Syntax Analysis.
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
Subject Name:COMPILER DESIGN Subject Code:10CS63
Compiler Design 7. Top-Down Table-Driven Parsing
Bottom Up Parsing.
Ambiguity in Grammar, Error Recovery
Bottom-Up Parsing “Shift-Reduce” Parsing
Kanat Bolazar February 16, 2010
USING AMBIGUOUS GRAMMARS
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler design Bottom-up parsing: Canonical LR and LALR
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can be easily eliminated eg from E ::= E + T | E * T | T produce E ::= T E2 E2 ::= + T E2 | * T E2 |  generally, make new productions for all the non-left-recursive old productions, using a new nonterminal make right-recursive productions for that new nonterminal, plus one  -production Non-immediate left-recursion (among several nonterminals) can be eliminated by ordering the nonterminals, cleaning them up one by one, and inlining previously cleaned productions for any leftmost nonterminals … see book p213 for detail Compiler Construction1

Perils of eliminating left recursion Any “Semantic actions” associated with productions need to be moved around so as to ensure they are still executed in the correct order Constructions which ought naturally to be parsed into a left-associative tree (such as “-”) now generate a right-associative tree, needing more care in code generation Empty productions are introduced into the grammar. –they too can if necessary be eliminated by separate technique Compiler Construction2

Ambiguous grammars If a grammar is ambiguous, it means that some sentences in the language it generates can be derived in more than one way Advantages of such grammars  the grammars are shorter and therefore simpler  the descriptions may be more natural, fewer nonterminals like Term, Factor  can allow special-case optimisations  parsing can be faster with time not wasted on “single productions|” –eg Term ::= Factor Disadvantages  Not LR, so parsing tables contain conflicts Fixes: Operator precedence; Parsing preferences – delete some parsing table entries Compiler Construction3

LR(0) automaton for ambiguous ‘Expression’ grammar E ::= E + E | E * E | ( E ) | id Compiler Construction4 I 0 E’ ::= E & E I 1 E’ ::= E E ::= E + E E ::= E * E I 4 E ::= E + E & E I 7 E ::= E + E E ::= E * E I 2 E ::= ( E ) & E I 3 E ::= id I 5 E ::= E * E & E I 6 E ::= ( E ) E ::= E + E E ::= E * E I 8 E ::= E * E E ::= E + E E ::= E * E I 9 E ::= ( E ) &E means E ::= E + E E ::= E * E E ::= ( E ::= id E ( id + * E E + + ( ( * ) Shift/Reduce conflicts arise at I 7 and I 8 + * *

Shift/Reduce conflicts, resolutions At I 7, reached after “E + E” if next is +, both shift and reduce are possible (as + is in FOLLOW(E))  shift, reduce correspond to + being right-, left-associative respectively if next is *, again both shift and reduce are possible  shift corresponds to giving * higher precedence than +  reduce corresponds to giving + higher precedence than * Similarly at I 8, reached after “E*E” if next is *,  shift, reduce correspond to * being right-, left-associative respectively if next is +,  shift corresponds to giving + higher precedence than *  reduce corresponds to giving * higher precedence than + Select just one of the possible actions to achieve the effect that is desired Compiler Construction5

Error detection and reporting in LR parsers Errors are detected when the parser reaches a state where the parsing table has no action for a combination of [next input token, top state on stack] None of SLR, LALR, or LR parsers will shift any tokens beyond the point where input has an error LR will not perform any reductions either; LALR may perform some; SLR may perform even more. It is possible to perform a panic-mode recovery, or to have recovery routines. Compiler Construction6

Panic-mode recovery in LR (& LALR & SLR) Pick one or more nonterminal corresponding to a significant program fragment, such as Statement or Block Arrange for errors to be handled by  scanning the stack for the first state s having a GOTO on such a nonterminal – say X  then skipping input tokens until finding one w that can follow X  remove all stack above s  add GOTO(X, w) to the stack  announce that an X has been inserted  continue parsing Compiler Construction7

Error routines in LR etc Routines can be written that are tailored to handling particular errors  It will be common for one routine to be appropriate for several of the blanks in the parsing table  Pointers to such routines can be inserted many times in the parsing table These routines can produce messages reflecting the diagnosis of errors programmers frequently make  eg “missing operand”, “unbalanced closing parenthesis found” Recovery often involves skipping input tokens, popping states off stack  care is needed with what may happen afterward if a particular rule does not do this  must guarantee eventual termination – input exhausted, or stack empty Compiler Construction8

Example error routines e1: push state 3 …id… onto stack, report “missing operand” e2: remove right parenthesis from input, report “unbalanced right parenthesis” e3: push state 4 …+… onto stack, report “missing operator” e4: push state 9 …)… onto stack, report “missing right parenthesis” Compiler Construction9