680 likes | 1.61k Views
編譯器實作 FRANCIS Compiler. Lexical Analysis(1). Lexical Analysis(2). A. No. Another character. available?. End. Yes. Read a character. Yes. Blank?. Identify the string in. the queue, if it exists,. as a token. No. A. Delimiter?. Identify the string in. the queue, if it exists,. No.
E N D
A No Another character available? End Yes Read a character. Yes Blank? Identify the string in the queue, if it exists, as a token. No A Delimiter? Identify the string in the queue, if it exists, No as a token. Put this character Identify the delimiter into the queue. as a token. A A 如何抓Token
Syntax Analysis • 將Tokens分辨為Statement • 檢查文法 • 正確 • 不正確 : Report error message • 將一些資訊匯入表格內
Example 4.1 PROGRAM A1; VARIABLE INTEGER:X,Y,I; DIMENSION INTEGER:A(12); LABEL L91, L92; I=1; X=5; Y=11; L91 IF X GT Y THEN GTO L92 ELSE X=X+2; A(I)=X; I=I+1; GTO L91; L92 ENP;
Quadruple form 1 (( 5, 8), , , ) X 2 (( 5,11), , , ) Y 3 (( 5, 2), , , ) I 4 (( 5, 7), , , ) A 5 (( 5,14), , , ) L91 6 (( 5,15), , , ) L92 7 (( 1, 4),(3,2), ,(5,2)) I=1 8 (( 1, 4),(3,3), ,(5,8)) X=5 9 (( 1, 4),(3,4), ,(5,11)) Y=11 10 (( 2,10),(5,8),(5,11),(0,1)) T1=X GT Y 11 (( 2,12),(0,1),(6,12),(6,13)) IF T1 GO TO 12, ELSE GO TO 13 12 (( 2,11), , ,(6,18)) GTO L92 13 (( 1, 5),(5,8),(3,5),(0,2)) T2=X+2 14 (( 1, 4),(0,2), ,(5,8)) X=T2 15 (( 1, 4),(5,8),(5,7),(5,2)) A(I)=X 16 (( 1, 5),(5,2),(3,2),(5,2)) I=I+1 17 (( 2,11), , ,(6,10)) GTO L91 18 ((2,6), , , ) L92 ENP
分辨Statement • 以 ; 當作Statement之結束 • Statement 計有 PROGRAM GTO SUBROUTINE assignment VARIABLE CALL LABEL INPUT DIMENSION OUTPUT IF
處理PROGRAM(1) • 文法 PROGRAM identifier; e.g. PROGRAM main; • 代表程式的開頭 • 此程式的中間碼在哪裡
處理 PROGRAM(2) • Pointer指向中間碼所在位置 Identifier Subroutine Type Pointer 1 2 3 4 5 6 7 8 9 10 Table 5
處理變數宣告(1) • VARIABLE INTEGER : U, V, M • Data Type : ARRAY 1 BOOLEAN 2 CHARACTER 3 INTEGER 4 LABEL 5 REAL 6
處理變數宣告(2) • Subroutine 是哪個 routine 宣告的 • Type 是哪一種 Data Type Identifier Subroutine Type Pointer 1 2 3 4 5 6 7 8 9 10
處理變數宣告(3) • 產生中間碼Table 6 1 ((5,1), , , )U
處理變數宣告範例(1) VARIABLE INTEGER: U,V,M; 1 ((5,1), , , )U 2 ((5,5), , , )V 3 ((5,6), , , )M
處理變數宣告範例(2) Identifier Subroutine Type Pointer 1 2 3 4 5 6 7 8 9 10
處理陣列宣告(1) DEMENSION REAL A(16,5); • 必須紀錄此陣列的大小 • 必須產生中間碼
處理陣列宣告(2) • 記錄陣列大小 使用Information Table 7 ←Real Array ←Dimensionality ←The size of the first dimension ←The size of the second dimension
處理陣列宣告(2) • TYPE 1 為 ARRAY • Pointer 21 指向information Table
處理陣列宣告(3) • 產生中間碼 ((5,7), , , ) A
處理LABEL定義(1) • 文法 LABEL identifier ; e.g LABEL L91,L92; • Type 5 表示為 LABEL
處理LABEL定義(2) • 產生中間碼 ((5,14), , , ) L91 ((5,14), , , ) L92
處理LABEL(1) • Pointer 指向要跳到的中間碼位置
處理LABEL(3) 5 ((5,14), , , ) L91 6 ((5,15), , , ) L92 . . 10 ((1,5),(5,3),(5,10),(5,13)) L91 X=Y+Z . . . 18 ((2,6), , , ) L92 ENP
處理GTO GTO L91 • 至Table 5內找到L91指向之中間碼位置 • 產生中間碼 ((2,11), , ,(6,18)) GTO L92 中間碼TABLE 第18個位置
處理SUBROUTINE(1) SUBROUTINE S1(INTEGER:X,Y,M); • 可看做是PROGRAM及VARIABLE兩個部分 • 要在TABLE 5之POINTER指向中間碼位置(TABLE 6) • 要在TABLE 5之SUBROUTINE加上X,Y,M的SCOPE
處理SUBROUTINE(2) 4 Y 10 8 x 10 M 10 9 s1 20 10
處理CALL(1) • CALL (W, 136, A, 57.9); • 將傳遞的參數建立在Information table • 產生中間碼
參數個數 4 變數W 5 15 整數136 3 3 5 變數A 27 Real 57.9 4 2 Information table 7 處理CALL(2) 59
處理CALL(3) ((2 , 3) , (5 , 36), ,(7 , 59)) CALL S1
W 15 136 2 57.9 3 A 27 Table 3 Table 4 Table 5 處理CALL(4)
處理Assignment(1) • X=Y+Z ; • (+ , Y , Z , X) • ((1,5) , (5,3) (5,10) , (5,13)) • X=Y+U*V ; • (* , U , V , T1) • (+ , Y , T1 , X)
處理Assignment(2) • 使用Reverse Polish Notation • 可以透過stack來進行處理
Reverse Polish Notation(1) Input X=Y+U*V Operand stack Operator stack
X = Reverse Polish Notation(2) Input : Y+U*V
Y X = Reverse Polish Notation(3) Input : +U*V
+ Y = X Reverse Polish Notation(4) Input : U*V
U * Y + X = Reverse Polish Notation(5) Input : V
V U * Y + X = Reverse Polish Notation(6)
V U Y X * + = Reverse Polish Notation(7) • 得到Reverse Polish Notation XYUV*+=
V U Y X (U*V) Y X * + = + = Reverse Polish Notation(8) X=(Y+(U*V))
U Y X + ( = Example(1) input X=(Y+U)*V (+,Y,U, T1) (*,T1,V,T2) (=,T2, , X)
V (Y+U) X * = Example(2) input X=(Y+U)*V output (Y+U)
= X Example(3) output (Y+U)*V
處理IF(1) IF X GT Y AND Q THEN X=X+1 ELSE X=X+2; 14 (GT,X,Y,T4) 15 (AND,T4,Q,T1) 16 (IF,T1,(6,17),(6,20)) 17 (+,X,1,T2) X=X+1 18 (=,T2, ,X) 19 (GTO, , ,(6,22)) 20 (+,X,2,T3) X=X+2 21 (=,T3, ,X)