Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing Lex and yacc LR Parsing Semantic Processing.

Similar presentations


Presentation on theme: "1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing Lex and yacc LR Parsing Semantic Processing."— Presentation transcript:

1 1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing Lex and yacc LR Parsing Semantic Processing Symbol Tables Run-time Storage Organization Code Generation and Local Code Optimization Global Optimization

2 2 Chapter 6 LR Parsing Techniques

3 3 Outline Shift-Reduce Parsers LR Parsers: LR(0) LR(1) Parsing SLR(1) Parsing LALR(1) Parsing

4 4 Shift-Reduce Parsers The fundamental concern of a bottom-up parser is deciding when what looks like the RHS of a production can be replaced by its LHS.

5 5 Shift-Reduce Parsers A shift-reduce parser works as follows:  A parser stack, initially empty, contains symbols already parsed.  The parse stack catenated with the remaining input always represents a right sentential form.  Tokens are shifted onto the stack until the top of the stack contains the handle of the sentential form.  Recall that a handle is a sequence of symbols that match some production’s RHS and which may be correctly replaced with that production’s LHS.  The handle is reduced by replacing it on the parse stack with the nonT that is its parent in the parse tree.

6 6 Shift-Reduce Parsers The grammar G 0 A shift-reduce parser

7 7 Shift-Reduce Parsers

8 8 The driver utilizes a parse stack that contains parse states, usually coded as integers. The driver uses two tables, action and go_to.  The action tells the parser whether to shift, reduce, terminate successfully, or signal a syntax error.  The go_to table defines successor states after a token or LHS is matched and shifted.

9 9 Shift-Reduce Parsers The action

10 10 Shift-Reduce Parsers The go_to

11 11 Shift-Reduce Parsers

12 12 R4 R2 R1=A

13 13 LR Parsers LR parsers are characterized by the number of lookahead symbols that are examined to determine parsing actions.  LR(K), where k is the lookahead size.

14 14 LR Parsers

15 15 LR Parsers: LR(0) LR(0) Parsing

16 16 LR Parsers: LR(0) We begin parsing with a configuration set Start Symbolthe end maker

17 17 LR Parsers: LR(0)

18 18 LR Parsers: LR(0)

19 19 LR Parsers: LR(0)

20 20 LR Parsers: LR(0)

21 21 LR Parsers: LR(0)

22 22 LR Parsers: LR(0)

23 23 LR Parsers: LR(0)

24 24 LR Parsers: LR(0)

25 25 LR Parsers: LR(0)

26 26 LR Parsers: LR(0)

27 27

28 28 LR Parsers: LR(0)

29 29 LR Parsers: LR(1)

30 30 LR Parsers: LR(1)

31 31 鑑于 historical information 尚需融入 state I i 之中, 以解決 shift/reduce conflict ! 今以一實例說明 canonical LR parsing table 之建構 : G’: S’ ::= S S ::= CC C ::= cC C ::= d 求 LR(1) item set S’ ::=.S,$ S ::=.CC,$ C ::=.cC,c/d C ::=.d,c/d I0I0 First(  $) First(C$) I1I2I3I4I1I2I3I4 S’ ::= S.,$ I1I1 S ::= C.C,$ C ::=.cC,$ C ::=.d,$ I2I2 C ::= c.C,c/d C ::=.cC,c/d C ::=.d,c/d I3I3 C ::= d.,c/d I4I4 S ::= CC.,$ C ::= c.C,$ C ::=.cC,$ C ::=.d,$ C ::= d.,$ C ::=cC.,c/d C ::= cC.,$ I5I5 I6I6 I7I7 I8I8 I9I9 需求取 First 集合而不必求 Follow 集合 !

32 32 State action goto c d $ S C 0 s3 s4 1 2 1 acc 2 s6 s7 5 3 s3 s4 8 4 r3 r3 5 r1 6 s6 s7 9 7 r3 8 r2 r2 9 r2 Canonical parsing table for grammar G’ Every SLR(1) grammar is an LR(1) grammar, but for an SLR(1) grammar the canonical LR parser may have more states than the SLR parser for the same grammar. The grammar of the previous example is SLR and has an SLR parser with seven states, compared with the ten shown above. 由於就 the number of states 而言,canonical LR parser 實在太 龐大, 因此時常難以落實, 而 SLR parser 卻又能力有所未逮, 於是 LALR parser 於焉誕生 ; 其狀態數與 SLR parser 完成相同, 然 shift/reduce conflict 較少發生.

33 33 LR Parsers: LR(1)

34 34

35 35 LR Parsers: LR(1)

36 36 LR Parsers: LR(1)

37 37 LR Parsers: LR(1)

38 38

39 39

40 40

41 41 LR Parsers: SLR(1)

42 42 LR Parsers: SLR(1)

43 43

44 44 LR Parsers: SLR(1) SLR parsing table 之建構流程 : ( 一 ) 增加一條語法規則 (0)S’ → S {S 表文法之 start symbol} 於文法 G 中, 令新語法名曰 G’  ( 二 ) 計算 items I 之 closure 集合 closure (I)  ( 三 ) 計算 grammar symbol X 之 goto 值 I’  goto (I, X) 並求其 closure (I’)  ( 四 ) 重覆上一步驟直到不再產生新的 goto 值  { 而得 item set} ( 五 ) 計算所有 nonterminals 之 FIRST 與 FOLLOW 集合  ( 六 ) 藉 Item 與 FOLLOW 兩集合建置 parsing table 

45 45

46 46

47 47 LR Parsers: SLR(1) LR(1) is very powerful. The goto and action tables of LR(1) are too big due to too many states. Two alternatives to LR(1)  SLR(1): LR(0) machine + lookahead  LALR(1): merge states of LR(1) machine The lookahead in LR(1) machine is computed from the context, whereas the lookahead in SLR(1) is the Follow sets. LR(1) lookaheads are more precise.

48 48 LR Parsers: SLR(1)

49 49 LR Parsers: SLR(1)

50 50 LR Parsers: LALR(1)

51 51 LR Parsers: LALR(1)

52 52 1. 根據 page 19 之方法求得 a collection of sets of LR(1) items. 2. 將所有 items 中之 first components 相同者 (the same cores ) 合併. 例示 : 以 page 19 之實例來說, 可資合併者包括 : 3 and 6, 4 and 7, 8 and 9 等三對, 合併而成 : C ::= c.C,c/d/$ C ::=.cC,c/d/$ C ::=.d,c/d/$ C ::= d.,c/d/$ C ::=cC.,c/d/$ I 36 I 47 I 89 State action goto c d $ S C 0 s36 s47 1 2 1 acc 2 s36 s47 5 36 s36 s47 89 47 r3 r3 r3 5 r1 89 r2 r2 r2 Comes up with

53 53 LR Parsers: LALR(1)

54 54 LR Parsers: LALR(1)

55 55

56 56

57 57

58 58 LR Parsers: LALR(1)

59 59

60 60

61 61 LR Parsers: LALR(1)

62 62

63 63

64 64

65 65

66 66

67 67

68 68

69 69


Download ppt "1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing Lex and yacc LR Parsing Semantic Processing."

Similar presentations


Ads by Google