Simple, efficient;limitated

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

Compiler Theory – 08/10(ppt) By Anindhya Sankhla 11CS30004 Group : G 29.
Compiler Designs and Constructions
Chapter 3 Syntax Analysis
CH4.1 CSE244 SLR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
Review: LR(k) parsers a1 … a2 … an $ LR parsing program Action goto Sm xm … s1 x1 s0 output input stack Parsing table.
LR Parsing Table Costruction
– 1 – CSCE 531 Spring 2006 Lecture 9 SLR Parse Table Construction Topics SLR Parse Table Construction Sets of Items Closure(I) Readings: 4.7 Homework:
1 Chapter 5: Bottom-Up Parsing (Shift-Reduce). 2 - attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working.
Pertemuan 12, 13, 14 Bottom-Up Parsing
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter
More SLR /LR(1) Professor Yihjia Tsai Tamkang University.
Bottom-Up Syntax Analysis Mooly Sagiv & Greta Yorsh Textbook:Modern Compiler Design Chapter (modified)
Bottom Up Parsing.
Chapter 4-2 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR Other.
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
Parser construction tools: YACC
LALR Parsing Canonical sets of LR(1) items
 an efficient Bottom-up parser for a large and useful class of context-free grammars.  the “ L ” stands for left-to-right scan of the input; the “ R.
LESSON 24.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019.
Using the LALR Parser Generator yacc By J. H. Wang May 10, 2011.
CS 321 Programming Languages and Compilers Bottom Up Parsing.
 an efficient Bottom-up parser for a large and useful class of context-free grammars.  the “ L ” stands for left-to-right scan of the input; the “ R.
Chapter 3-3 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR 
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
Compiler Principle and Technology Prof. Dongming LU Mar. 26th, 2014.
YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Bernd Fischer RW713: Compiler and Software Language Engineering.
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.
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.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
Bottom-up parsing. Bottom-up parsing builds a parse tree from the leaves (terminals) to the start symbol int E T * TE+ T (4) (2) (3) (5) (1) int*+ E 
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.
Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
1 Syntax Analysis Part III Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
YACC SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Yacc.
Lec04-bottomupparser 4/13/2018 LR Parsing.
Bottom-up Parsing.
Syntax Analysis Part III
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
UNIT - 3 SYNTAX ANALYSIS - II
Bottom-up Parsing.
Chapter 4 Syntax Analysis.
Compiler Construction
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Syntax Analysis Part III
Canonical LR Parsing Tables
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
Syntax Analysis Part III
Syntax Analysis — Parser
Lecture 9 SLR Parse Table Construction
Syntax Analysis Part III
Syntax Analysis Part III
Compiler Construction
Chapter 4. Syntax Analysis (2)
Compiler SLR Parser.
Bottom-up Parsing.
Syntax Analysis - 3 Chapter 4.
Compiler Lecture Note, Miscellaneous
Chapter 4. Syntax Analysis (2)
Compiler Design Yacc Example "Yet Another Compiler Compiler"
Chap. 3 BOTTOM-UP PARSING
Lecture 11 LR Parse Table Construction
Presentation transcript:

Simple, efficient;limitated Revision Simple, efficient;limitated LR Parser Input a1 … ai … an $ LR Parser 栈 sm Xm sm-1 Xm-1 … s0 Output Simple LR (SLR) Canonical LR (LR) LookAheadLR (LALR) action goto LR Parse Table 10/46

3.5 LR Parser 3.5.4 Construct Canonical LR Parsing table LR(0) Item [A·] Left of the dot indicates history information; right of the dot indicates what we expect from input 3.5.4 Construct Canonical LR Parsing table LR(1) Item set: It is possible to carry more information in the state that will allow us to rule out some of these in valid reductions The extra information is incorporated in to the state b y redefining items to include a terminal symbol as a second component. The general form of an item becomes [A·, a]

3.5 LR Parser 3.5.4 Construct Canonical LR Parsing table LR(1) item set: The general form of an item becomes [A·, a] LR(1) item [A·, a] is valid for a viable prefix : if there exists a derivation S *rm Aw rm w, where:  = ; a is the first symbol of w, or w is  and a is $。

3.5 LR Parser Ex S  BB B  bB | a Derivation S *rm bbBba rm bbbBba: [Bb·B, b] is valid for  = bbb LR(1) item [A·, a] is valid for viable prefix : if exists S *rm Aw rm w, where:  = ; a is the first symbol of w, or w is  anda is$。

3.5 LR Parser Ex S  BB B  bB | a From S *rm bbBba rm bbbBba we observe: [Bb·B, b] is valid for  = bbb For item [A·, a], If  is not empty, should shift (same as LR(0)); If  is empty,decide the reduction choice by a, rather than FOLLOW(A) (different from LR(0)). Usually a is subset of FOLLOW(A)

3.5 LR Parser Construct Canonical LR Parsing table Constructing LR(1) Sets of Items to recognize viable prefixes of G 1、Augmented grammar S S S BB B bB B a 15/46

3.5 LR Parser Construct Canonical LR Parsing table S S S BB B bB B a Construct Canonical LR Parsing table Constructing LR(1) Sets of Items to recognize viable prefixes of G 2、Construct LR(1) Item set I0: S .S, $ S .BB, $ B .bB, b/a B .a, b/a closure(I) 1、Place every item in I into closure(I) 2、If [Aα·Bβ, a] is in closure(I), and Bη is a production,then add [B·η,b ] into closure(I) if it does not belongs to closure(I) (b in FIRST(βa))

3.5 LR Parser Construct Canonical LR Parsing table S S S BB B bB B a Construct Canonical LR Parsing table Constructing LR(1) Sets of Items to recognize viable prefixes of G 2、Construct LR(1) Item set I0: S .S, $ (kernel item) S .BB, $ (nonkernel item, B .bB, b/a which could be B .a, b/a obtained from closure)

3.5 LR Parser S ·S, $ I0 S ·BB, $ B ·bB, b/a B ·a, b/a S S

3.5 LR Parser S S ·S, $ I0 S S·, $ I1 S ·BB, $ B ·bB, b/a B ·a, b/a S S S·, $ I1 S B·B, $ B ·bB, $ B ·a, $ I2 B b B b·B, b/a B ·bB, b/a B ·a, b/a I3 a B a·, b/a I4

3.5 LR Parser S ·S, $ I0 S ·BB, $ B ·bB, b/a B ·a, b/a B ·a, $ I2 S B B b·B, b/a B ·a, b/a I3 B a·, b/a I4 a b S BB·, $ I5 B b·B, $ B ·a, $ I6 B bB·, $ I9 B a·, $ I7 B bB·, b/a I8 20/46

3.5 LR Parser Construct Canonical LR Parsing table Constructing LR(1) Sets of Items to recognize viable prefixes of G For state i from Ii,its action function defined as: If[A ·a, b] in Ii,and goto(Ii, a) = Ij ,then set action[i, a] sj。 If [A · , a] in Ii,and A  S,then set action[i, a] rj . If [SS·, $] Ii,then set action[i, $] = acc。 If conflict occurs, the grammar is not LR(1)

3.5 LR Parser Construct Canonical LR Parsing table Constructing LR(1) Sets of Items to recognize viable prefixes of G For state i from Ii,its action function defined as: . . . Goto function for state i : if goto(Ii, A) = Ij, then goto[i, A] = j

3.5 LR Parser Construct Canonical LR Parsing table Constructing LR(1) Sets of Items to recognize viable prefixes of G For state i from Ii,its action function defined as: . . . Goto function for state i : Undefined entries are set as error。

3.5 LR Parser Construct Canonical LR Parsing table Constructing LR(1) Sets of Items to recognize viable prefixes of G For state i from Ii,its action function defined as: . . . Goto function for state i : Undefined entries are set as error。 The state that contains [S·S, $] is the start state

3.5 LR Parser 3.5.5 Constructing LALR Parse table Property: Parse tables of LALR and SLR have same number of states, meanwhile that of LR is much larger The power of LALR lies between SLR and LR 25/46

3.5 LR Parser Merged LR(1) item set: Omit the character a in [A · , a], and merge the item sets

3.5 LR Parser S ·S, $ I0 S ·BB, $ B ·bB, b/a B ·a, b/a B ·a, $ I2 S B B b·B, b/a B ·a, b/a I3 B a·, b/a I4 a b S BB·, $ I5 B b·B, $ B ·a, $ I6 B bB·, $ I9 B a·, $ I7 B bB·, b/a I8

3.5 LR Parser S ·S, $ I0 S ·BB, $ B ·bB, b/a B ·a, b/a S S BB·, $ I5 B S B·B, $ B ·bB, $ B ·a, $ I2 B b b B b·B, b/a/$ B ·bB, b/a/$ B ·a, b/a/$ I36 a b a B a B a·, b/a/$ I47 B bB·, b/a/$ I89

3.5 LR Parser stack input 0 bba$ S ·S, $ I0 S ·BB, $ B ·bB, b/a B ·a, b/a S S S·, $ I1 S BB·, $ I5 B stack input 0 bba$ S B·B, $ B ·bB, $ B ·a, $ I2 B b b B b·B, b/a/$ B ·bB, b/a/$ B ·a, b/a/$ I36 a b a B a B a·, b/a/$ I47 B bB·, b/a/$ I89 30/46

3.5 LR Parser stack input 0b36 ba$ S ·S, $ I0 S ·BB, $ B ·bB, b/a B ·a, b/a S S S·, $ I1 S BB·, $ I5 B stack input 0b36 ba$ S B·B, $ B ·bB, $ B ·a, $ I2 B b b B b·B, b/a/$ B ·bB, b/a/$ B ·a, b/a/$ I36 a b a B a B a·, b/a/$ I47 B bB·, b/a/$ I89

3.5 LR Parser stack input 0b36b36 a$ S ·S, $ I0 S ·BB, $ B ·bB, b/a B ·a, b/a S S S·, $ I1 S BB·, $ I5 B stack input 0b36b36 a$ S B·B, $ B ·bB, $ B ·a, $ I2 B b b B b·B, b/a/$ B ·bB, b/a/$ B ·a, b/a/$ I36 a b a B a B a·, b/a/$ I47 B bB·, b/a/$ I89

3.5 LR Parser stack input 0b36b36a47 $ S ·S, $ I0 S ·BB, $ B ·bB, b/a B ·a, b/a S S S·, $ I1 S BB·, $ I5 B stack input 0b36b36a47 $ S B·B, $ B ·bB, $ B ·a, $ I2 B b b B b·B, b/a/$ B ·bB, b/a/$ B ·a, b/a/$ I36 a b a B a B a·, b/a/$ I47 B bB·, b/a/$ I89

3.5 LR Parser stack input 0b36b36B89 $ S ·S, $ I0 S ·BB, $ B ·bB, b/a B ·a, b/a S S S·, $ I1 S BB·, $ I5 B stack input 0b36b36B89 $ S B·B, $ B ·bB, $ B ·a, $ I2 B b b B b·B, b/a/$ B ·bB, b/a/$ B ·a, b/a/$ I36 a b a B a B a·, b/a/$ I47 B bB·, b/a/$ I89

3.5 LR Parser stack input 0b36B89 $ S ·S, $ I0 S ·BB, $ B ·bB, b/a B ·a, b/a S S S·, $ I1 S BB·, $ I5 B stack input 0b36B89 $ S B·B, $ B ·bB, $ B ·a, $ I2 B b b B b·B, b/a/$ B ·bB, b/a/$ B ·a, b/a/$ I36 a b a B a B a·, b/a/$ I47 B bB·, b/a/$ I89 35/46

3.5 LR Parser stack input 0B2 $ S ·S, $ I0 S ·BB, $ B ·bB, b/a B ·a, b/a S S S·, $ I1 S BB·, $ I5 B stack input 0B2 $ S B·B, $ B ·bB, $ B ·a, $ I2 B b b B b·B, b/a/$ B ·bB, b/a/$ B ·a, b/a/$ I36 a b a B a B a·, b/a/$ I47 B bB·, b/a/$ I89

3.5 LR Parser Merged item set could lead to conflicts No new shift-reduce conflict [A·, a] [B·a, b] [B·a, c] [A·, d]

3.5 LR Parser Merged item set could lead to conflicts No new shift-reduce conflict New reduce-reduce conflict is possible

3.5 LR Parser Merged item set could lead to conflicts No new shift-reduce conflict New reduce-reduce conflict is possible S  S S  aAd | bBd | aBe | bAe A  c B  c

3.5 LR Parser Merged item set could lead to conflicts No new shift-reduce conflict New reduce-reduce conflict is possible S  S S  aAd | bBd | aBe | bAe A  c B  c valid for ac A  c ·, d B  c ·, e 40/46

3.5 LR Parser Merged item set could lead to conflicts No new shift-reduce conflict New reduce-reduce conflict is possible S  S S  aAd | bBd | aBe | bAe A  c B  c valid for ac valid for bc A  c ·, d B  c ·, e A  c ·, e B  c ·, d

3.5 LR Parser 合并同心项目集可能会引起冲突 同心集的合并不会引起新的移进归约冲突 同心集的合并有可能产生新的归约归约冲突 S  S S  aAd | bBd | aBe | bAe A  c B  c 对ac有效的项目集 对bc有效的项目集 A  c ·, d B  c ·, e A  c ·, e B  c ·, d 合并同心集后 A  c ·, d/e B  c ·, d/e

3.5 LR Parser Merged item set could lead to conflicts No new shift-reduce conflict New reduce-reduce conflict is possible S  S S  aAd | bBd | aBe | bAe A  c B  c valid ac valid for bc A  c ·, d B  c ·, e A  c ·, e B  c ·, d merged LR(1) but not LALR(1) A  c ·, d/e B  c ·, d/e

3.5 LR Parser Construct LALR(1) Parsing table Construct LR(1) item set C = {I0, I1, …, In}。 Search for LR(1) item sets that could be merged Construct parsing table as LR(1)

3.7 Parser Generator 3.7.1 Parser Generator Yacc Lexer generated by Lex Lex compiler Lex source lex.l lex.yy.c C a.out input Token stream Yacc compiler Yacc source translate.y y.tab.c C a.out Token stream output 1/36

3.7 Parser Generator Structure of Yacc source code Declaration %% Translation rules Auxiliary functions

3.7 Parser Generator Ex: Declaration %{ EE + T | T TT * F | F F(E) | digit Ex: Declaration %{ #include <ctype.h> /* constant, variable*/ %} %token DIGIT %%

3.7 Parser Generator Ex: Translation rules EE + T | T TT * F | F F(E) | digit Ex: Translation rules line : expr ‘\n’ {printf(“%d\n”, $1);} ; expr : expr ‘+’ term {$$ = $1+$3;} | term term : term ‘*’ factor {$$ = $1 * $3;} | factor factor : ’(’ expr ‘)’ {$$ = $2;} | DIGIT %%

3.7 Parser Generator Ex: Auxiliary function yylex(){ int c; EE + T | T TT * F | F F(E) | digit Ex: Auxiliary function yylex(){ int c; c = getchar(); if (isdigit(c)){ yylval = c – ‘0’; return DIGIT; } return c; 5/36

3.7 Parser Generator 3.7.2 Use Yacc to process Ambiguous Grammar To default rules to resolve conflicts: For reduce-reduce conflicts, choose the first reduce rule For shift-reduce conflicts, choose shift rules

3.7 Parser Generator 3.7.2 Use Yacc to process Ambiguous Grammar Ex: Calculator Calculate the expression from input。 lines lines expr ‘\n’ | lines ‘\n’ | e E E + E | E – E | E * E | E / E | (E) | -E | number

3.7 Parser Generator %{ # include <ctype .h> # include <stdio.h > # define YYSTYPE double /* 将栈定义为double类型 */ %}   %token NUMBER %left ‘+’ ‘’ %left ‘*’ ‘/ ’ %right UMINUS %% lines lines expr ‘\n’ | lines ‘\n’ | e E E + E | E – E | E * E | E / E | (E) | -E | number

3.7 Parser Generator lines : lines expr ‘\ n’ {printf ( “%g \ n”, $2 ) } | lines ‘\ n’ | /*  */ ; expr : expr ‘+’ expr {$$ = $1 + $3; } | expr ‘’ expr {$$ = $1  $3; } | expr ‘*’ expr {$$ = $1 * $3; } | expr ‘/ ’ expr {$$ = $1 / $3; } | ‘(’ expr ‘)’ {$$ = $2; } | ‘’ expr %prec UMINUS {$$ = $2; } | NUMBER %% lines lines expr ‘\n’ | lines ‘\n’ | e E E + E | E – E | E * E | E / E | (E) | -E | number

3.7 Parser Generator yylex ( ) { int c; while ( ( c = getchar ( ) ) = = ‘ ’ ); if ( ( c = = ‘.’ ) | | (isdigit (c) ) ) { ungetc (c, stdin); scanf ( “% lf ”, &yylval); return NUMBER; } return c; lines lines expr ‘\n’ | lines ‘\n’ | e E E + E | E – E | E * E | E / E | (E) | -E | number 10/36

课堂练习 S  V = E S  E V  * E V  id E  V I0 : S   · S, $ Construct the DFA based on LR(1) items S  V = E S  E V  * E V  id E  V I0 : S   · S, $ S  ·V = E, $ S  · E, $ V  · * E, =/$ V  · id, =/$ E  · V, $ V I2 : S  V · = E, $ E  V ·, $ 45/46

参考答案 S’. S, $ S . V=E, $ S. E, $ V.*E, =/$ V. id, =/$ E. V, $ S