Presentation is loading. Please wait.

Presentation is loading. Please wait.

Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation.

Similar presentations


Presentation on theme: "Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation."— Presentation transcript:

1 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation Techniques Dr. Zheng Xiaojuan Associate Professor Software College of Northeast Normal University April. 2008

2 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -2- Knowledge Relation Graph Develop a Parser Syntax definition basing on Context Free Grammar using implement Top-down Bottom-up

3 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -3- §4 Top-down Parsing 4.1 Overview of Top-down Parsing 4.2 Three Important Sets 4.3 Left Recursion Removal & Left Factoring 4.4 Recursive-Descent Parsing 4.5 LL(1) Parsing    

4 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -4- 4.5 LL(1) Parsing Main idea of LL(1) Parsing Method LL(1) Grammar (LL(1) 文法 ) LL(1) Parsing Table (LL(1) 分析表 ) LL(1) Parsing Engine (LL(1) 分析驱动程序 ) LL(1) Parsing Process (LL(1) 分析过程 ) LL(1) Parser Generator – LLGen (LL(1) 分析程序的自动生成器 )

5 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -5- Main idea of LL(1) Parsing Method LL(1) Parsing –Left-to-right parsing, Left-most derivation, 1-symbol look ahead ; –Requires the same precondition( 和递归下降法要求相同 的前提条件 ) G = (V T, V N, S, P) For any A  V N, For any two productions of A, Predict(A   1)  Predict(A   2) = 

6 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -6- Main idea of LL(1) Parsing Method LL(1) Parsing –LL(1) parsing table to record predict sets for each production; (LL(1) 分析表 ) –A general engine( 一个通用的驱动程序 )

7 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -7- Example P: (1) Z  aBd (2) B  d (3) B  c (4) B  bB a b c d 句型输入动作 ZabcdDerive (1) aBdabcdMatch BdbcdDerive(4) bBdbcdMatch BdcdDerive(3) cd Match dd Succeed

8 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -8- LL(1) Parsing Mechanism Stack Input #dcba 驱动程序 Z LL[1] 分析表

9 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -9- LL(1) Parsing Mechanism Stack Input #………a 驱动程序 :  栈为空情形的处理  X  V T 情形的处理  X  V N 情形的处理 X … … LL[1] 分析表

10 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -10- 4.5 LL(1) Parsing Main idea of LL(1) Parsing Method LL(1) Grammar (LL(1) 文法 ) LL(1) Parsing Table (LL(1) 分析表 ) LL(1) Parsing Engine (LL(1) 分析驱动程序 ) LL(1) Parsing Process (LL(1) 分析过程 ) LL(1) Parser Generator – LLGen (LL(1) 分析程序的自动生成器 )

11 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -11- LL(1) Grammar If a CFG G meets the precondition below, we will call G is a LL(1) Grammar; G = (V T, V N, S, P) For any A  V N, For any two productions of A, Predict(A   1 )  Predict(A   2 ) = 

12 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -12- LL(1) Grammar Some features of a LL(1) Grammar –No ambiguity( 无二义性 ) –No left recursion( 无左递归 ) –For a non-terminal symbol, there is at most one empty production( 对于一个非终极符来讲, 最多只有一个空产生 式 )

13 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -13- 4.5 LL(1) Parsing Main idea of LL(1) Parsing Method LL(1) Grammar (LL(1) 文法 ) LL(1) Parsing Table (LL(1) 分析表 ) LL(1) Parsing Engine (LL(1) 分析驱动程序 ) LL(1) Parsing Process (LL(1) 分析过程 ) LL(1) Parser Generator – LLGen (LL(1) 分析程序的自动生成器 )

14 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -14- LL(1) Parsing Table (LL(1) 分析表 ) The purpose of LL(1) Parsing Table –According to, decide that which production of current non-terminal symbol can be used to derive; – 根据当前的非终极符和当前的输入符决定用哪一个产生 式来进行推导 ; If current non-terminal symbol is X, current input symbol is a, we can use X  if and only if a  predict(X  );

15 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -15- LL(1) Parsing Table (LL(1) 分析表 ) How to build LL(1) Parsing Table for a LL(1) Grammar? –For a LL(1) Grammar G = (V T, V N, S, P) –V T = {a 1, …, a n } –V N = {A 1, …, A n } –LL(A i, a i ) = [A i   ], if a i  predict(A i   ) –LL(A i, a i ) = error, if a i not belong to the predict set of any production of A i a1a1 …anan # A1A1 ……. … AnAn

16 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -16- Example 产生式 Predict 集 (1){a} (2){d} (3){c} (4){b} P: (1) Z  aBd (2) B  d (3) B  c (4) B  bB abcd# Z(1) B(4)(3)(2)

17 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -17- Example (1) E  TE’ { i, n, ( } (2) E’  + TE’ {+} (3) E’   {#, )} (4) T  FT’ {i,n,(} (5) T’  * F T’ {*} (6) T’   {),+, # } (7) F  (E) { ( } (8) F  i {i} (9) F  n {n} +*()in# E(1) E’(2)(3) T(4) T’(6)(5)(6) F(7)(8)(9)

18 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -18- 4.5 LL(1) Parsing Main idea of LL(1) Parsing Method LL(1) Grammar (LL(1) 文法 ) LL(1) Parsing Table (LL(1) 分析表 ) LL(1) Parsing Engine (LL(1) 分析驱动程序 ) LL(1) Parsing Process (LL(1) 分析过程 ) LL(1) Parser Generator – LLGen (LL(1) 分析程序的自动生成器 )

19 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -19- LL(1) Parsing Engine 分析驱动程序 Stack Input #………a 驱动程序 :  栈为空情形的处理  X  V T 情形的处理  X  V N 情形的处理 X … … LL[1] 分析表

20 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -20- LL(1) Parsing Engine Configuration( 格局 ): 可能的情形 – 栈为空情形的处理 :, succeed, if a=#, else error; –X  V T 情形的处理 :, match; else error; –X  V N 情形的处理 :, derive if there is a production X  such that a  predict(X  ), else error;

21 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -21- LL(1) Parsing Engine [1] 初始化: Stack := [ ] ; Push(S) ; [2] 读下一个输入符: Read(a) ; [3] 若当前格局是 (, # ) ,则成功结束;否则转下; [4] 设当前格局为(..... X, a.....) ,则  若 X  V T & X= a ,则 { Pop(1) ; Read(a) ; goto [3] }  若 X  V T & X  a ,则 Error ;  若 X  V N ,则: if LL(X, a)=X→Y 1 Y 2...... Y n then { Pop(1) ; Push(Y n,.....,Y 1 ) ; goto[3] } else Error

22 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -22- 4.5 LL(1) Parsing Main idea of LL(1) Parsing Method LL(1) Grammar (LL(1) 文法 ) LL(1) Parsing Table (LL(1) 分析表 ) LL(1) Parsing Engine (LL(1) 分析驱动程序 ) LL(1) Parsing Process (LL(1) 分析过程 ) LL(1) Parser Generator – LLGen (LL(1) 分析程序的自动生成器 )

23 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -23- LL(1) Parsing Process P: (1) Z  aBd (2) B  d (3) B  c (4) B  bB abcd# Z(1) B(4)(3)(2) a b c d a c c d

24 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -24- 4.5 LL(1) Parsing Main idea of LL(1) Parsing Method LL(1) Grammar (LL(1) 文法 ) LL(1) Parsing Table (LL(1) 分析表 ) LL(1) Parsing Engine (LL(1) 分析驱动程序 ) LL(1) Parsing Process (LL(1) 分析过程 ) LL(1) Parser Generator – LLGen (LL(1) 分析程序的自动生成器 )

25 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -25- Dealing with If-Then-Else CFG for If-Then-Else V T = {if, then, else, other, ;, exp} V N = {S, Stm, ElsePart} P: (1) S  Stm ; (2) Stm  if exp then Stm ElsePart (3) Stm  other (4) ElsePart  else Stm (5) ElsePart   (1){if, other} (2){if} (3){other} (4){else} (5){;, else} Predict set:

26 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -26- Dealing with If-Then-Else LL(1) parsing table ifthenelseotherexp;# S(1) Stm(2)(3) ElsePart(4) (5)(5) (4) 每个 else 与其前面未匹配的最近的 then 相匹配 !

27 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -27- Dealing with If-Then-Else stackinputaction Sif exp then if exp then other else other ;(1) Stm ;if exp then if exp then other else other;(2) if exp then Stm ElsePart;if exp then if exp then other else other; Stm ElsePart ElsePart; if exp then other else other; if exp then Stm ElsePart ElsePart; if exp then other else other; Match(3 次 ) (2) Match(3 次 ) other else other;(3) other ElsePart ElsePart;other else other;Match ElsePart ElsePart;else other;(4) Stm ElsePart;

28 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -28- Dealing with If-Then-Else stackinputaction succeed ElsePart ElsePart;else other;(4) else Stm ElsePart;else other;match Stm ElsePart;other; other ElsePart;other;match ElsePart;; (3) (5) ;;match

29 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -29- S Stm; if expthenStm ElsePart ifexp then StmElsePart other else Stm other

30 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -30- Building Parse Tree During LL(1) [1] 初始化: Stack := [ ] ; root=BuildOneNode(S); Push(S, root) ; [2] 读下一个输入符: Read(a) ; [3] 若当前格局是 (, # ) ,则成功结束;否则转下; [4] 设当前格局为(..... X, a.....) ,则  若 X  V T & X= a ,则 { Pop(1) ; Read(a) ; goto [3] }  若 X  V T & X  a ,则 Error ;  若 X  V N ,则: if LL(X, a)=X→Y 1 Y 2...... Y n then { (X, ptr) = Pop(1); for i=n to 1 { p[i] = BuildOneNode(Yi), Push(Y i, p[i]) ; } AddSons(ptr, p, n); goto[3] } else Error

31 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -31- LL(1) Parser Generator – LLGen LLGen 终极符 / 产生式个数 产生式右部长度 LL( 分析表 ) 可导空串的符号表 文法中所有符号 非 LL(1) 表的冲突

32 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -32- §4 Top-down Parsing 4.1 Overview of Top-down Parsing 4.2 Three Important Sets 4.3 Left Recursion Removal & Left Factoring 4.4 Recursive-Descent Parsing 4.5 LL(1) Parsing

33 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -33- Knowledge Relation Graph Top-down parsing Syntax definition basing on CFG using check precondition Predict(A  ) first(  ) follow(A) Yes Recursive-descent LL(1) Implement 文法等价 变换

34 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -34- Summary What is the main idea of Top-down parsing? What is the precondition of recursive-descent and LL(1) parsing? The definitions of three sets ? For a given CFG, calculate three sets? The main idea of recursive-descent parsing? ※

35 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -35- Summary For a given CFG, develop recursive-descent parser? –Calculate predict set for each production; –Check whether meets the precondition; –If yes, develop function for each non-terminal symbol; –Build parse tree during parsing;

36 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -36- Summary The main idea of LL(1) parsing? LL(1) Grammar? The mechanism of LL(1) parsing? For a given CFG, develop LL(1) parser? –Calculate predict set for each production; –Check whether meets the precondition; –If yes, generate LL(1) parsing table; –LL(1) parser = LL(1) parsing table + LL(1) parsing engine –Build parse tree; Give the LL(1) parsing process?

37 Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -37- Assignment G = {V T, V N, S, P} V T = {-, (, ), id} V N = {E, ET, V, VT} S = E P = { E  -E ET   E  (E) V  id VT E  V ET VT  (E) ET  -E VT   } (1)Generate LL(1) Parsing table; (2) “ -(id--id) ” 的 分析过程 ;


Download ppt "Software College of Northeast Normal University Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation."

Similar presentations


Ads by Google