COMPILER (CSE 4120) (Lecture 6: Parsing 4 Bottom-up Parsing )

Similar documents
Compilation 2013 Parser Generators, Conflict Management, and ML-Yacc

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

Conflicts in LR Parsing and More LR Parsing Types

Using an LALR(1) Parser Generator

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

Bottom-Up Parsing. Lecture 11-12

Lecture 8: Deterministic Bottom-Up Parsing

Lecture 7: Deterministic Bottom-Up Parsing

Syntax Analysis Part IV

Topic 5: Syntax Analysis III

A Bison Manual. You build a text file of the production (format in the next section); traditionally this file ends in.y, although bison doesn t care.

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

LR(0) Parsing Summary. LR(0) Parsing Table. LR(0) Limitations. A Non-LR(0) Grammar. LR(0) Parsing Table CS412/CS413

Bottom-Up Parsing. Lecture 11-12

Parsing How parser works?

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

Part III : Parsing. From Regular to Context-Free Grammars. Deriving a Parser from a Context-Free Grammar. Scanners and Parsers.

Yacc Yet Another Compiler Compiler

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

CSCI Compiler Design

Parser. Larissa von Witte. 11. Januar Institut für Softwaretechnik und Programmiersprachen. L. v. Witte 11. Januar /23

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

CS415 Compilers. LR Parsing & Error Recovery

LECTURE 11. Semantic Analysis and Yacc

CSE302: Compiler Design

Ambiguity. Lecture 8. CS 536 Spring

G53CMP: Lecture 4. Syntactic Analysis: Parser Generators. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 4 p.1/32

LR Parsing LALR Parser Generators

Monday, September 13, Parsers

LR Parsing LALR Parser Generators

Context-free grammars

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Compiler Construction: Parsing

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

In One Slide. Outline. LR Parsing. Table Construction

Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals.

Syn S t yn a t x a Ana x lysi y s si 1

Wednesday, August 31, Parsers

LALR Parsing. What Yacc and most compilers employ.

Simple LR (SLR) LR(0) Drawbacks LR(1) SLR Parse. LR(1) Start State and Reduce. LR(1) Items 10/3/2012

S Y N T A X A N A L Y S I S LR

UNIT III & IV. Bottom up parsing

Compilers. Bottom-up Parsing. (original slides by Sam

Chapter 3: Lexing and Parsing

Parsing. COMP 520: Compiler Design (4 credits) Professor Laurie Hendren.

Hyacc comes under the GNU General Public License (Except the hyaccpar file, which comes under BSD License)

Introduction to Yacc. General Description Input file Output files Parsing conflicts Pseudovariables Examples. Principles of Compilers - 16/03/2006

Lecture Bottom-Up Parsing

Bottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form

Introduction to Parsing. Lecture 5

Principles of Programming Languages

Principles of Programming Languages

Downloaded from Page 1. LR Parsing

Example CFG. Lectures 16 & 17 Bottom-Up Parsing. LL(1) Predictor Table Review. Stacks in LR Parsing 1. Sʹ " S. 2. S " AyB. 3. A " ab. 4.

shift-reduce parsing

Introduction to Parsing. Lecture 5

Compiler Construction


Lexical and Syntax Analysis. Bottom-Up Parsing

Lecture Notes on Bottom-Up LR Parsing

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

Bottom-Up Parsing II (Different types of Shift-Reduce Conflicts) Lecture 10. Prof. Aiken (Modified by Professor Vijay Ganesh.

Syntax-Directed Translation

Concepts Introduced in Chapter 4

Bottom up parsing. The sentential forms happen to be a right most derivation in the reverse order. S a A B e a A d e. a A d e a A B e S.

More Bottom-Up Parsing

Compiler Construction

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

3. Parsing. Oscar Nierstrasz

LR Parsing. Table Construction

Grammars and ambiguity. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 8 1

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

Fall Compiler Principles Lecture 5: Parsing part 4. Roman Manevich Ben-Gurion University

Lecture Notes on Bottom-Up LR Parsing

Compiler Construction

LR Parsing Techniques

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Compiler Lab. Introduction to tools Lex and Yacc

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Building a Parser Part III

CS143 Handout 12 Summer 2011 July 1 st, 2011 Introduction to bison

CS606- compiler instruction Solved MCQS From Midterm Papers

Chapter 3 Parsing. time, arrow, banana, flies, like, a, an, the, fruit

Syntax Analysis. Tokens ---> Parse Tree. Main Problems. Grammars. Convert the list of tokens into a parse tree ( hierarchical analysis)

Compiler construction in4020 lecture 5

Top down vs. bottom up parsing

LR Parsing. Leftmost and Rightmost Derivations. Compiler Design CSE 504. Derivations for id + id: T id = id+id. 1 Shift-Reduce Parsing.

Bottom-Up Parsing LR Parsing

Transcription:

COMPILR (CS 4120) (Lecture 6: Parsing 4 Bottom-up Parsing ) Sungwon Jung Mobile Computing & Data ngineering Lab Dept. of Computer Science and ngineering Sogang University Seoul, Korea Tel: +82-2-705-8930 mail : jungsung@sogang.ac.kr Resolving Parsing Conflicts Parse conflicts possible when certain LR items are found in the same state. Depending on parser, may choose between LR items using lookahead Legal lookahead for LR items must be disjoint, else conflict exists. Shift-Reduce Reduce-Reduce [A :=., ] [A :=., ] [B :=., ] [B :=., ] LR(0) conflict conflict SLR(1) Follow(A) First( ) Follow(A) Follow(B) LR(1) First( ) CS 4120 Fundamentals of Compiler Construction -- Sungwon Jung 6-2 1

SLR(1) and LR(1) The Grammar S := G G := = id := +T T T := T*id id LR(0) states S 0 : [S :=.G] [G :=.=] [G :=.id] [ :=.+T] [ :=.T] [T :=.T*id] [T :=.id] S 1 : [G := id.] FOLLOW(G) = {$} [T := id.] FOLLOW(T) = {$, *, +, =} Reduce-reduce conflict in S 1 for lookahead $!!! LR(1) states S 0 : [S :=.G, $] [G :=.=, $] [G :=.id, $] [ :=.+T, =/+] [ :=.T, =/+] [T :=.T*id, =/+/*] [T :=.id, =/+/*] S 1 : [G := id., $] [T := id., =/+/*] Reduce-reduce conflict in S 1 resolved by lookahead!!! CS 4120 Fundamentals of Compiler Construction -- Sungwon Jung 6-3 LALR(1) Parsing LR(1) parser have many more states than SLR(1) parser approximately factor of ten for Pascal LALR(1) parser have same number of states as SLR(1) parsers, but with more power due to lookahead in states Define the core of a set of LR(1) items to be the set of LR(0) items derived by ignoring the lookahead symbols Thus, the two sets {[A :=., a], [A :=., b]} and {[A :=., c], [A :=., d]} have the same core. Key Idea: If two sets of LR(1) items, I i and I j,have the same core, we can merge the states that represent them in the ACTION and GOTO tables CS 4120 Fundamentals of Compiler Construction -- Sungwon Jung 6-4 2

LALR(1) Table Construction Build LR(1) sets of items, then merge 1. For each core present among the set of LR(1) items, find all sets having that core and replace these sets by their union 2. Update the goto function to reflect the replacement sets CS 4120 Fundamentals of Compiler Construction -- Sungwon Jung 6-5 LALR(1) Properties LALR(1) parsers have the same number of states as SLR(1) parsers core LR(0) items are the same May perform reduce than error But will catch error before more input is processed LALR derived from LR with no shift-reduce conflict will also have no shift-reduce conflict LALR may create reduce-reduce conflict not in LR from which LALR is derived CS 4120 Fundamentals of Compiler Construction -- Sungwon Jung 6-6 3

Classification of Grammars LL(k) LL(1) LR(k) LR(1) LALR(1) SLR LR(0) CS 4120 Fundamentals of Compiler Construction -- Sungwon Jung 6-7 LR Parsing of Ambiguous Grammars S := if then S else S S := if then S S := other The above grammar rules allows programs such as if a then if b then s1 else s2 (1) if a then { if b then s1 else s2 } --- Correct Interpretation! (2) if a then { if b then s1 } else s2 --- Wrong Interpretation! In the LR parsing table there will be a shift-reduce conflict: S := if then S. S := if then S. else S else (any) Shifting : Gives the interpretation (1) Reducing: Gives the interpretation (2) Reducing Shifting CS 4120 Fundamentals of Compiler Construction -- Sungwon Jung 6-8 4

LR Parsing of Ambiguous Grammars The ambiguity can be eliminated by introducing auxiliary nonterminals M (for matched statement) and U (for unmatched statement) S := M U M := if then M else M other U := if then S if then M else U Another method: Leave the grammar unchanged and tolerate the shift-reduce conflict Often possible to use ambiguous grammars by resolving shift-reduce conflicts in favor of shifting or reducing, as appropriate In constructing the parsing table, the above shift-reducing conflict can be resolved by shifting over reducing Prefer the interpretation (1) to (2) CS 4120 Fundamentals of Compiler Construction -- Sungwon Jung 6-9 Using Parser Generators The task of constructing LR(1) or LALR(1) parsing tables is simple enough to automated and so tedious to do by hand for realistic grammars!!! Automatic parser generators: Yacc (Yet another compilercompiler) Bison and occs : More recent implementations A Yacc Specification divided into 3 sections, separated by %% marks parser declarations %% grammar rules %% programs CS 4120 Fundamentals of Compiler Construction -- Sungwon Jung 6-10 5

An xample of Yacc program without Semantic actions attached 1. P L 2. S id := id 3. S while id do S 4. S begin L end 5. S if id then S 6. S if id then S else S 7. L S 8. L L ; S %{ int yylex(void) ; void yyerror (char *s) { M_error (M_tokPos, %s, s); } %} %token ID WHIL BGIN ND DO IF THN LS SMI ASSIGN %start prog %% prog: stmlist stm : ID ASSIGN ID WHIL ID DO stm BGIN stmlist ND IF ID THN stm IF ID THN stm LS stm stmlist : stm stmlist SMI stm CS 4120 Fundamentals of Compiler Construction -- Sungwon Jung 6-11 Conflicts Yacc reports shift-reduce and reduce-reduce conflicts A shift-reduce conflict: a choice between shifting and reducing A reduce-reduce conflict: a choice of reducing by two different rules By default, Yacc resolves Shift-reduce conflict by shifting Reduce-reduce conflicts by using the rule appearing earlier in the grammar See the Figure in the next slide : y.output yacc -dv filename.y produces 3 output files: y.tab.c, y.tab.h and y.output Check Yacc s default resolution of the shift-reduce conflict This shift-reduce conflict is not harmful!!! <--- Why? CS 4120 Fundamentals of Compiler Construction -- Sungwon Jung 6-12 6

xample: LR Parsing Table (Table 3.33) id num * / + - ( ) CS 4120 Fundamentals of Compiler Construction -- Sungwon Jung 6-13 Precedence Directives 1 The LR(k) parsing table of an ambiguous grammar will always have conflicts Ambiguous grammars can still be useful if we can find ways to resolve the conflicts xample Grammar: id num * / + - ( ) Should be parsed so that * and / bind more tightly than + and -, and each operator associates to the left See state 13 with lookahead + in Table 3.33 Conflict between shift into state 8 and reduce by rule 3 *. +. + (any) In this state the top of the stack is * CS 4120 Fundamentals of Compiler Construction -- Sungwon Jung 6-14 7

Precedence Directives 3 xample Grammar (Cont d): See state 9 with lookahead * Conflict between shift into state 12 and reduce by rule5: (s12, r5) In this state the top of the stack is + +. *. * (any) state 9 + * * + Shift Reduce If we wish * to bind tighter than +, then shift instead of reduce!!! CS 4120 Fundamentals of Compiler Construction -- Sungwon Jung 6-15 Precedence Directives 2 xample Grammar (Cont d): See state 13 with lookahead + Conflict between shift into state 8 and reduce by rule 3: (s8, r3) In this state the top of the stack is * *. +. + (any) state 13 * + + * Shift Reduce If we wish * to bind tighter than +, reduce instead of shift!!! CS 4120 Fundamentals of Compiler Construction -- Sungwon Jung 6-16 8

Associativity 1 xample Grammar (Cont d): See state 9 with lookahead + Conflict between shift into state 8 and reduce by rule5: (s8, r5) In this state the top of the stack is + +. +. + (any) state 9 + + + + Shift Reduce Shifting(Reducing) will make the operator right(left)-associative!!! CS 4120 Fundamentals of Compiler Construction -- Sungwon Jung 6-17 Associativity 2 Left-associativity means Conflict should be broken in favor of reduce!!! Right-associativity means Conflict should be broken in favor of shift!!! Nonassociative - is usually left-associative a - b - c usually means (a - b) - c Can make - operator nonassociative Instead of expressing a - b - c, express either (a-b)-c or a-(b-c) CS 4120 Fundamentals of Compiler Construction -- Sungwon Jung 6-18 9

Declaration of Precedence Directives in Yacc %nonassoc Q NQ %left PLUS MINUS %left TIMS DIV %right XP The above declaration indicates + and - are left-associative and bind equally tightly * and / are left-associative and bind more tightly than + and - ^ is right-associative and binds most tightly = and are non-associative, and bind most weakly than + and - *. +. + (any) The priority of a rule is given by the last token occurring on the right hand side of that rule CS 4120 Fundamentals of Compiler Construction -- Sungwon Jung 6-19 10