Chapter 4. Syntax Analysis (2)

Slides:



Advertisements
Similar presentations
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.
Advertisements

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 Theory – 08/10(ppt) By Anindhya Sankhla 11CS30004 Group : G 29.
Compiler Designs and Constructions
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.
Compiler Construction Sohail Aslam Lecture Finite Automaton of Items Then for every item A →  X  we must add an  -transition for every production.
LR Parsing Table Costruction
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
1 Example S'  S, S  CC, C  cC, C  d State 0. Closure({[S'  S, $]}) = { S'  S, $ S   CC, FIRST( $)  S   CC, $ C   cC, FIRST(C$)  C.
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)
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
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.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
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.
SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019.
 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.
1 LR Parsers  The most powerful shift-reduce parsing (yet efficient) is: LR(k) parsing. LR(k) parsing. left to right right-most k lookhead scanning derivation.
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)
–Exercise: construct the SLR parsing table for grammar: S->L=R, S->R L->*R L->id R->L –The grammar can have shift/reduce conflict or reduce/reduce conflict.
1 Compiler Construction Syntax Analysis Top-down parsing.
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.
Bottom-Up Parsing Algorithms LR(k) parsing L: scan input Left to right R: produce Rightmost derivation k tokens of lookahead LR(0) zero tokens of look-ahead.
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.
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.
COMPILER CONSTRUCTION
Chapter 4. Syntax Analysis (1). 2 Application of a production  A  in a derivation step  i   i+1.
Announcements/Reading
Lec04-bottomupparser 4/13/2018 LR Parsing.
Compiler Construction
Introduction to LR Parsing
Programming Languages Translator
lec04-bottomupparser June 6, 2018 Bottom-Up Parsing
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
CS 488 Spring 2012 Lecture 4 Bapa Rao Cal State L.A.
Table-driven parsing Parsing performed by a finite state machine.
Compiler Construction
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Simple, efficient;limitated
Canonical LR Parsing Tables
Syntax Analysis Part II
Shift Reduce Parsing Unit -3
Prepared By: NITIN GOYAL - 04CS1026 JITENDRA BINDAL - 04CS1027
Subject Name:COMPILER DESIGN Subject Code:10CS63
LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.
Syntax Analysis — Parser
Parsing #2 Leonidas Fegaras.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Bottom Up Parsing.
Compiler Construction
Chapter 4. Syntax Analysis (2)
Parsing #2 Leonidas Fegaras.
Compiler Construction
Compiler Construction
Parsing Bottom-Up Introduction.
Chap. 3 BOTTOM-UP PARSING
Chapter 4. Syntax Analysis (1)
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

Chapter 4. Syntax Analysis (2)

Fig. 4.23. Operator-precedence relations. id + * $ ·> <· Fig. 4.23. Operator-precedence relations.

Operator-Precedence Relations from Associativity and Precedence (1/2) 1. If operator θ1 has higher precedence than operator θ2, make θ1 ·> θ2 and θ2 <· θ1 . For example, if * has higher precedence than +, make * ·> + and + <· *. These relations ensure that, in an expression of the form E+E*E+E, the central E*E is the handle that will be reduced first. 2. If θ1 and θ2 are operators of equal precedence (they may in fact be the same operator), then make θ1 ·> θ2 and θ2 ·> θ1 if the operators are left-associative, or make θ1 <· θ2 and θ2 <· θ1 if they are right-associative. For example, if + and – are left-associative, then make + ·> +, + ·> -, - ·> - and - ·> +. If  is right associative, then make  <· . These relations ensure that E-E+E will have handle E-E selected and EEE will have the last EE selected.

Operator-Precedence Relations from Associativity and Precedence (2/2) 3. Make θ <· id, id ·> θ, θ <· (, ( <· θ , ) ·> θ, θ ·> ), θ ·> $, and $ <· θ for all operators θ. Also, let These rules ensure that both id and (E) will be reduced to E. Also, $ serves as both the left and right endmarker, causing handles to be found between $’s wherever possible. · ( = ) $ <· ( $ <· id ( <· ( id ·> $ ) ·> $ ( <· id id ·> ) ) ·> )

Fig. 4.25. Operator-precedence relations. + - * / ↑ id ( ) $ ·> <· = · Fig. 4.25. Operator-precedence relations.

Precedence Functions Example 4.29 The Precedence table of Fig. 4.25 has the following pair of precedence functions, For example, * <· id and, f(*) < g(id). Note that f(id) > g(id) suggests that id ·> id; but, in fact, no precedence relation holds between id and id. Other error entries in Fig. 4.25 are similarly replaced by one or another precedence relation. + - * / ↑ ( ) id $ f 2 4 6 g 1 3 5

Fig. 4.26. Graph representing precedence functions.

Fig. 4.28. Operator-precedence matrix with error entries id ( ) $ e3 ·> <· = e4 e2 e1 · Fig. 4.28. Operator-precedence matrix with error entries

Fig. 4.29. Model of an LR parser.

Fig. 4.31. Parsing table for expression grammar. STATE action goto id + * ( ) $ E T F s5 s4 1 2 3 s6 acc r2 s7 r4 4 8 5 r6 6 9 7 10 s11 r1 r3 11 r5 Fig. 4.31. Parsing table for expression grammar.

Fig. 4.32. Moves of LR parser on id * id + id. STACK INPUT ACTION (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13) (14) 0 id 5 0 F 5 0 T 2 0 T 2 * 7 0 T 2 * 7 id 5 0 T 2 * 7 F 10 0 E 1 0 E 1 + 6 0 E 1 + 6 id 5 0 E 1 + 6 F 3 0 E 1 + 6 T 9 id * id + id$ * id + id$ id + id$ + id$ id$ $ shift reduced by F  id reduced by T  F reduced by T  T*F reduced by E  T E  E + T accept Fig. 4.32. Moves of LR parser on id * id + id.

Fig. 4.35. Canonical LR(0) collection for grammar (4.19) E'  · E E  · E + T E  · T T  · T * F T  · F F  · (E) F  · id I5: F  id · I6: E  E + · T I7: T  T * · F I1: E'  E · E  E · + T I2: E  T · T  T · * F I8: F  ( E · ) I9: E  E + T · I3: T  F · I4: F  (· E ) I10: T  T * F · I11: F  ( E ) · Fig. 4.35. Canonical LR(0) collection for grammar (4.19)

Fig. 4.36. Transition diagram of DFA D form viable prefixes.

Fig. 4.37. Canonical LR(0) collection for grammar (4.20). S'  · S S  · L = R S  · R L  · * R L  · id R  · L I5: L  id · I6: S  L = · R I1: S'  S · I7: L  * R · I2: S  L · = R R  L · I8: I9: S  L = R · I3: S  R · I4: L  * · R Fig. 4.37. Canonical LR(0) collection for grammar (4.20).

Fig. 4.39. The goto graph for grammar (4.21).

Fig. 4.40. Canonical parsing table for grammar (4.21). STATE action goto c d $ S C s3 s4 1 2 acc s6 s7 5 3 8 4 r3 r1 6 9 7 r2 Fig. 4.40. Canonical parsing table for grammar (4.21).

Constructing LALR Parsing Table Example 4.44. Consider the grammar S'  S S  aAd | bBd | aBe | bAe A  c B  c which generates the four strings acd, ace, bcd, and bce. The reader can check that the grammar is LR(1) by constructing the sets of items. Upon doing so, we find the set of items { [ A  c · , e ], [ B  c · , e ]} valid for viable prefix ac and { [ A  c · , e ], [ B  c · , d ]} valid for bc. Neither of these sets generates a conflict, and their cores are the same. However, their union, which is A  c · , d / e B  c · , d / e generates a reduce/reduce conflict, since reductions by both A  c and B  c are called for on inputs d and e.

Fig. 4.41. LALR parsing table for grammar (4.21). STATE action goto c d $ S C s36 s47 1 2 acc 5 36 89 47 r3 r1 r2 Fig. 4.41. LALR parsing table for grammar (4.21).

Efficient Construction of LALR Parsing Tables Example 4.46. Let us again consider the augmented grammar S'  S S  L = R | R A  * R | id B  L The kernels of the sets of LR(0) items for this grammar are shown in Fig. 4.42. I0: S'  · S I1: S'  S · I2: S  L · = R R  L · I3: S  R · I4: L  * · R I5: L  id · I6: S  L = · R I7: L  * R · I8: R  L · I9: S  L = R · Fig. 4.42. Kernels of the sets of LR(0) items for grammar (4.20).

Efficient Construction of LALR Parsing Tables Example 4.47. Let us construct the kernels of the LALR(1) items for the grammar in the previous example. The kernels of the LR(0) items were shown in Fig. 4.42. When we apply Algorithm 4.12 to the kernel of set of items I0, we compute closure ({[S'  · S, #]}), which is S'  · S, # S  · L = R, # S  · R, # L  · * R, #/= L  · id, #/= R  · L, #

Fig.4.44. Propagation of lookaheads. FROM TO I0: S'  · S I1: I2: I3: I4: I5: S'  S · S  L · = R R  L · S  R · L  * · R L  id · I6: S  L = · R I7: I8: L  * R · I9: S  L = R · Fig.4.44. Propagation of lookaheads.

Fig. 4.45. Computation of lookaheads. SET ITEM LOOKAHEADS INIT PASS1 PASS2 PASS3 I0: S'  · S $ I1: S'  S · I2: S  L · = R R L · I3: S  R · I4: L  * · R = =/$ I5: L  id · I6: S  L = · R I7: L  * R · I8: R  L · I9: S  L = R · Fig. 4.45. Computation of lookaheads.

Fig. 4.46. Set of LR(0) items for augmented grammar (4.22). E'  · E E  · E + E E  · E * E E  · ( E ) E  · id I5: E  E * · E I1: E'  E · E'  E · + E E'  E · * E I6: E'  ( E · ) I2: E  ( ·E ) I7: E'  E + E · I8: E'  E * E · I3: E  id · I4: E  E + · E I9: E'  ( E ) · Fig. 4.46. Set of LR(0) items for augmented grammar (4.22).

Fig. 4.47. Parsing table for grammar (4.22). STATE action goto id + * ( ) $ E s3 s2 1 s4 s5 acc 2 6 3 r4 4 8 5 s9 7 r1 r2 9 r3 Fig. 4.47. Parsing table for grammar (4.22).