9/27/2006Prof. Hilfinger, Lecture 141 Parsing Odds and Ends Lecture 14 (P. N. Hilfinger, plus slides adapted from R. Bodik)

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
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.
Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
Mooly Sagiv and Roman Manevich School of Computer Science
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Formal Aspects Term 2, Week4 LECTURE: LR “Shift-Reduce” Parsers: The JavaCup Parser-Generator CREATES LR “Shift-Reduce” Parsers, they are very commonly.
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
Parsing V Introduction to LR(1) Parsers. from Cooper & Torczon2 LR(1) Parsers LR(1) parsers are table-driven, shift-reduce parsers that use a limited.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
CS 330 Programming Languages 09 / 23 / 2008 Instructor: Michael Eckmann.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
Top-Down Parsing.
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.
2/20/2008Prof. Hilfinger CS164 Lecture 121 Earley’s Algorithm: General Context-Free Parsing Lecture 12 P. N. Hilfinger.
Bottom-up parsing Goal of parser : build a derivation
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
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,
Chapter 9 Syntax Analysis Winter 2007 SEG2101 Chapter 9.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
1 Compiler Construction Syntax Analysis Top-down parsing.
Lexical and Syntax Analysis
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
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.
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.
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
More Parsing CPSC 388 Ellen Walker Hiram College.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.
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. 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.
10/10/2002© 2002 Hal Perkins & UW CSED-1 CSE 582 – Compilers LR Parsing Hal Perkins Autumn 2002.
Top-Down Parsing.
Parsing and Code Generation Set 24. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program,
Top-Down Predictive Parsing We will look at two different ways to implement a non- backtracking top-down parser called a predictive parser. A predictive.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Joey Paquet, 2000, 2002, 2008, 2012, Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic Analysis.
Parsing III (Top-down parsing: recursive descent & LL(1) )
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
Parsing V LR(1) Parsers. LR(1) Parsers LR(1) parsers are table-driven, shift-reduce parsers that use a limited right context (1 token) for handle recognition.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
Lecture 5: LR Parsing CS 540 George Mason University.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.
Syntax and Semantics Structure of programming languages.
Programming Languages Translator
CS510 Compiler Lecture 4.
Table-driven parsing Parsing performed by a finite state machine.
Fall Compiler Principles Lecture 4: Parsing part 3
4 (c) parsing.
Top-Down Parsing.
Lexical and Syntax Analysis
Syntax-Directed Translation
Syntax Errors; Static Semantics
Ambiguity, Precedence, Associativity & Top-Down Parsing
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
LR Parsing. Parser Generators.
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:

9/27/2006Prof. Hilfinger, Lecture 141 Parsing Odds and Ends Lecture 14 (P. N. Hilfinger, plus slides adapted from R. Bodik)

9/27/2006Prof. Hilfinger, Lecture 142 Administrivia Trial run of project autograder on Wednesday night (sometime). No other runs until Friday.

9/27/2006Prof. Hilfinger, Lecture 143 Topics Syntax-directed translation and LR parsing Syntax-directed translation and recursive descent Dealing with errors in LR parsing: quick and dirty approach

9/27/2006Prof. Hilfinger, Lecture 144 Syntax-Directed Translation and LR Parsing Idea: Add semantic stack, parallel to the parsing stack: –each symbol (terminal or non-terminal) on the parsing stack stores its value on the semantic stack –each reduction uses the values at the top of the stack to compute the new value to be associated with the symbol that’s produced –when the parse is finished, the semantic stack will hold just one value: the translation of the root non-terminal, which is the translation of the whole input.

9/27/2006Prof. Hilfinger, Lecture 145 Semantic actions during parsing when shifting –push the value of the terminal on the semantic stack when reducing –pop k values from the semantic stack, where k is the number of symbols on production’s RHS –push the production’s value on the semantic stack

9/27/2006Prof. Hilfinger, Lecture 146 An LR example Grammar + translation rules: E  E + ( E )$$ = $1 + $4 E  int $$ = $1 Input: 2 + ( 3 ) + ( 4 )

9/27/2006Prof. Hilfinger, Lecture 147 Shift-Reduce Example with evaluations parsing stacksemantic stack I int + (int) + (int)$ shift I

9/27/2006Prof. Hilfinger, Lecture 148 Shift-Reduce Example with evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E  int2 I

9/27/2006Prof. Hilfinger, Lecture 149 Shift-Reduce Example with evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E  int2 I E I + (int) + (int)$ shift 3 times2 I

9/27/2006Prof. Hilfinger, Lecture 1410 Shift-Reduce Example with evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E  int2 I E I + (int) + (int)$ shift 3 times2 I E + (int I ) + (int)$ red. E  int2 ‘+’ ‘(‘ 3 I

9/27/2006Prof. Hilfinger, Lecture 1411 Shift-Reduce Example with evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E  int2 I E I + (int) + (int)$ shift 3 times2 I E + (int I ) + (int)$ red. E  int2 ‘+’ ‘(‘ 3 I E + (E I ) + (int)$ shift 2 ‘+’ ‘(‘ 3 I

9/27/2006Prof. Hilfinger, Lecture 1412 Shift-Reduce Example with evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E  int2 I E I + (int) + (int)$ shift 3 times2 I E + (int I ) + (int)$ red. E  int2 ‘+’ ‘(‘ 3 I E + (E I ) + (int)$ shift2 ‘+’ ‘(‘ 3 I E + (E) I + (int)$ red. E  E + (E) 2 ‘+’ ‘(‘ 3 ‘)’ I

9/27/2006Prof. Hilfinger, Lecture 1413 Shift-Reduce Example with evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E  int2 I E I + (int) + (int)$ shift 3 times2 I E + (int I ) + (int)$ red. E  int2 ‘+’ ‘(‘ 3 I E + (E I ) + (int)$ shift2 ‘+’ ‘(‘ 3 I E + (E) I + (int)$ red. E  E + (E) 2 ‘+’ ‘(‘ 3 ‘)’ I E I + (int)$ shift 3 times 5 I

9/27/2006Prof. Hilfinger, Lecture 1414 Shift-Reduce Example with evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E  int2 I E I + (int) + (int)$ shift 3 times2 I E + (int I ) + (int)$ red. E  int2 ‘+’ ‘(‘ 3 I E + (E I ) + (int)$ shift 2 ‘+’ ‘(‘ 3 I E + (E) I + (int)$ red. E  E + (E) 2 ‘+’ ‘(‘ 3 ‘)’ I E I + (int)$ shift 3 times 5 I E + (int I )$ red. E  int5 ‘+’ ‘(‘ 4 I

9/27/2006Prof. Hilfinger, Lecture 1415 Shift-Reduce Example with evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E  int2 I E I + (int) + (int)$ shift 3 times2 I E + (int I ) + (int)$ red. E  int2 ‘+’ ‘(‘ 3 I E + (E I ) + (int)$ shift 2 ‘+’ ‘(‘ 3 I E + (E) I + (int)$ red. E  E + (E) 2 ‘+’ ‘(‘ 3 ‘)’ I E I + (int)$ shift 3 times 5 I E + (int I )$ red. E  int5 ‘+’ ‘(‘ 4 I E + (E I )$ shift5 ‘+’ ‘(‘ 4 I

9/27/2006Prof. Hilfinger, Lecture 1416 Shift-Reduce Example with Evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E  int2 I E I + (int) + (int)$ shift 3 times2 I E + (int I ) + (int)$ red. E  int2 ‘+’ ‘(‘ 3 I E + (E I ) + (int)$ shift 2 ‘+’ ‘(‘ 3 I E + (E) I + (int)$ red. E  E + (E) 2 ‘+’ ‘(‘ 3 ‘)’ I E I + (int)$ shift 3 times 5 I E + (int I )$ red. E  int5 ‘+’ ‘(‘ 4 I E + (E I )$ shift5 ‘+’ ‘(‘ 4 I E + (E) I $ red. E  E + (E) 5 ‘+’ ‘(‘ 4 ‘)’ I

9/27/2006Prof. Hilfinger, Lecture 1417 Shift-Reduce Example with evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E  int2 I E I + (int) + (int)$ shift 3 times2 I E + (int I ) + (int)$ red. E  int2 ‘+’ ‘(‘ 3 I E + (E I ) + (int)$ shift 2 ‘+’ ‘(‘ 3 I E + (E) I + (int)$ red. E  E + (E) 2 ‘+’ ‘(‘ 3 ‘)’ I E I + (int)$ shift 3 times 5 I E + (int I )$ red. E  int5 ‘+’ ‘(‘ 4 I E + (E I )$ shift5 ‘+’ ‘(‘ 4 I E + (E) I $ red. E  E + (E) 5 ‘+’ ‘(‘ 4 ‘)’ I E I $ accept9 I

9/27/2006Prof. Hilfinger, Lecture 1418 Taking Advantage of Derivation Order So far, rules have been functional; no side effects except to define (once) value of LHS. LR parsing produces reverse rightmost derivation. Can use the ordering to do control semantic actions with side effects.

9/27/2006Prof. Hilfinger, Lecture 1419 Example of Actions with Side Effects E  E + T print “+”, E  T pass T  T * F print “*”, T  F pass F  int print $1, F  ( E ) pass We know that reduction taken after all the reductions that form the nonterminals on right-hand side. So what does this print for 3+4*(7+1)? * +

9/27/2006Prof. Hilfinger, Lecture 1420 Recursive-Descent Translation Translating with recursive descent is also easy. The semantic values (what Bison calls $$, $1, etc.), become return values of the parsing functions We’ll also assume that the lexer has a way to return lexical values (e.g., the scan function introduced in Lecture 9 might do so).

9/27/2006Prof. Hilfinger, Lecture 1421 E  T | E+T T  P | T*P P  int | ‘(‘ E ‘)’ def E(): T () while next() == “+”: scan(“+”); T() def T(): P() while next() == “*”: scan(“*”); P() Example of Recursive-Descent Translation def P(): if next()==int: scan (int) elif next()== “ ( “ : scan( “ ( “ ) E() scan( “ ) ” ) else: ERROR() (we ’ ve cheated and used loops; see Recursive Descent for Real slide from lecture 9)

9/27/2006Prof. Hilfinger, Lecture 1422 E  T | E+T T  P | T*P P  int | ‘(‘ E ‘)’ def E(): v = T () while next() == “+”: scan(“+”); v += T() return v def T(): v = P() while next() == “*”: scan(“*”); v *= P() return v Example contd.: Add Semantic Values def P(): if next()==int: v = scan (int) elif next()== “ ( “ : scan( “ ( “ ) v = E() scan( “ ) ” ) else: ERROR() return v

9/27/2006Prof. Hilfinger, Lecture 1423 Table-Driven LL(1) We can automate all this, and add to the LL(1) parser method from Lecture 9. However, this gets a little involved, and I’m not sure it’s worth it. (That is, let’s leave it to the LL(1) parser generators for now!)

9/27/2006Prof. Hilfinger, Lecture 1424 Dealing with Syntax Errors One purpose of the parser is to filter out errors that show up in parsing Later stages should not have to deal with possibility of malformed constructs Parser must identify error so programmer knows what to correct Parser should recover so that processing can continue (and other errors found) Parser might even correct error (e.g., PL/C compiler could “correct” some Fortran programs into equivalent PL/1 programs!)

9/27/2006Prof. Hilfinger, Lecture 1425 Identifying Errors All of the valid parsers we’ve seen identify syntax errors “as soon as possible.” Valid prefix property: all the input that is shifted or scanned is the beginning of some valid program … But the rest of the input might not be So in principle, deleting the lookahead (and subsequent symbols) and inserting others will give a valid program.

9/27/2006Prof. Hilfinger, Lecture 1426 Automating Recovery Unfortunately, best results require using semantic knowledge and hand tuning. –E.g., a(i].y = 5 might be turned to a[i].y = 5 if a is statically known to be a list, or a(i).y = 5 if a function. Some automatic methods can do an OK job that at least allows parser to catch more than one error.

9/27/2006Prof. Hilfinger, Lecture 1427 Bison’s Technique The special terminal symbol error is never actually returned by the lexer. Gets inserted by parser in place of erroneous tokens. Parsing then proceeds normally.

9/27/2006Prof. Hilfinger, Lecture 1428 Example of Bison’s Error Rules Suppose we want to throw away bad statements and carry on stmt : whileStmt | ifStmt | … | error NEWLINE ;

9/27/2006Prof. Hilfinger, Lecture 1429 Response to Error Consider erroneous text like if x y: … When parser gets to the y, will detect error. Then pops items off parsing stack until it finds a state that allows a shift or reduction on ‘error’ terminal Does reductions, then shifts ‘error’. Finally, throws away input until it finds a symbol it can shift after ‘error’

9/27/2006Prof. Hilfinger, Lecture 1430 Error Response, contd. So with our example: stmt : whileStmt | ifStmt | … | error NEWLINE ; We see ‘y’, throw away the ‘if x’, so as to be back to where a stmt can start. Shift ‘error’ and away more symbols to NEWLINE. Then carry on. Bad input: if x y: … x = 0

9/27/2006Prof. Hilfinger, Lecture 1431 Of Course, It’s Not Perfect “Throw away and punt” is sometimes called “panic-mode error recovery” Results are often annoying. For example, in our example, there’s an INDENT after the NEWLINE, which doesn’t fit the grammar and causes another error. Bison compensates in this case by not reporting errors that are too close together But in general, can get cascade of errors. Doing it right takes a lot of work.