제 7 장 ll 구문 분석

30
제 7 제 LL 제제 제제 제제제제 제제 제제제제 제제

Upload: saima

Post on 12-Jan-2016

53 views

Category:

Documents


0 download

DESCRIPTION

컴파일러 입문. 제 7 장 LL 구문 분석. 7.1 결정적 구문 분석. Deterministic Top-Down Parsing ::= deterministic selection of production rules to be applied in top-down syntax analysis. One pass nobackup 1. Input string is scanned once from left to right. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 제  7  장  LL  구문 분석

제 7 장 LL 구문 분석

컴파일러 입문컴파일러 입문

Page 2: 제  7  장  LL  구문 분석

Deterministic Top-Down Parsing ::= deterministic selection of production rules to be applied in top-down syntax analysis.

One pass nobackup

1. Input string is scanned once from left to right.

2. Parsing process is deterministic.

Top-down parsing with nobackup

::= deterministic top-down parsing.

called LL parsing. “Left to right scanning and Left parse”

LL Parsing[2/45]

Page 3: 제  7  장  LL  구문 분석

How to decide which production is to be applied: sentential form : 1 2 … i-1Xα

input string : 1 2 … i-1 i i+1 … n

X 1 | 2 ... | k ∈ P 일 때 ,

i 를 보고 X-production 중에 uniqueunique 하게 결정 .

the condition for no backtracking : FIRST 와 FOLLOW 가 필요 . (=> LL condition)

Page 4: 제  7  장  LL  구문 분석

FIRST() ::= the set of terminalsterminals that begin the strings derived from .

if , then is also in FIRST(). FIRST(A) ::= { a V∈ T {∪ } | A a, V* }.∈

Computation of FIRST(X), where X ∈ V. 1) if X V∈ T, then FIRST(X) = {X}

2) if X V∈ N and X a P, then FIRST(X) = FIRST(X) ∈ ∪ {a}

if X P, then FIRST(X) = FIRST(X) ∈ ∪ {}

3) if X Y1Y2 …Yk P and Y∈ 1Y2 …Yi-1 , then FIRST(X) = FIRST(X) ∪ (∪ FIRST(Yj) - {}).

if Y1Y2 …Yk , then FIRST(X) = FIRST(X) ∪{}.

*

*

*j=1

i

*

Page 5: 제  7  장  LL  구문 분석

Text p.275ex1) E TE E +TE |

T FT T FT | F (E) | id

FIRST(E) = FIRST(T) = FIRST(F) = {(, id} FIRST(E) = {+, } FIRST(T) = {, }

ex2) PROGRAM begin d semi X end X d semi X X s Y Y semi s Y | FIRST(PROGRAM) = {begin} FIRST(X) = {d,s} FIRST(Y) = {semi, }

Page 6: 제  7  장  LL  구문 분석

FOLLOW(A)

::= the set of terminals that can appear immediately to the right of A in some sentential form. If A can be the rightmost symbol in some sentential form, then $ is in FOLLOW(A).

::= {a V∈ T {$} | S ∪ Aa, , V∈ *}. ※ $ is the input right marker.

Computation of FOLLOW(A) 1) FOLLOW(S) = {$}

2) if A B P and ∈ , then FOLLOW(B) = FOLLOW(B) (FIRST(∪ ) - ) 3) if A B P or A ∈ B and , then FOLLOW(B) = FOLLOW(B) FOLLOW(∪ A).

*

*

Page 7: 제  7  장  LL  구문 분석

Text p.277

E TEE’ +TE | T FTT’ FT | F (E) | id

Nullable = { E, T }

FIRST(E) = FIRST(T) = FIRST(F) = {(, id}FIRST(E) = {+, } FIRST(T) = {, }

FOLLOW(E) = {),$} FOLLOW(E') = {),$}FOLLOW(T) = {+,),$} FOLLOW(T') = {+,),$}FOLLOW(F) = {,+,),$}

Page 8: 제  7  장  LL  구문 분석

연습문제 7.4 (3) - p.307

(3) S aAa | A abS | c

Page 9: 제  7  장  LL  구문 분석

기본적 개념 ::= no backup condition ::= the condition for deterministic parsing of top-down method.

input : 12 ... i-1i ...n

derived string : 12...i-1X

X 1 | 2 ... | m

i 를 보고 X-production 들 중에서 X 를 확장할 rule 을 결정적으로 선택 .

정의 : A | ∈ P,

1. FIRST() ∩ FIRST() = 2. if , FOLLOW(A) ∩ FIRST() = if ∈ FIRST(), FOLLOW(A) ∩ FIRST() =

*

Page 10: 제  7  장  LL  구문 분석

A aBc | Bc | dAaB bB |

FIRST(A) = {a,b,c,d} FOLLOW(A) = {$,a}FIRST(B) = {b, } FOLLOW(B) = {c}

LL condition 검사 1) A aBc | Bc | dAa 에서 ,

FIRST(aBc) ∩ FIRST(Bc) ∩ FIRST(dAa)= {a} ∩ {b,c} ∩ {d} =

2) B bB | 에서 ,

FIRST(bB) ∩ FOLLOW(B) = {b} ∩ {c} = 1), 2) 에 의해 LL 조건을 만족한다 .

Page 11: 제  7  장  LL  구문 분석

Recursive-descent parsing ::= A top-down method that uses a set of recursive procedures to recognize its input with no backtracking. Create a procedure for each nonterminal.

ex) G : S aA | bB A aA | c B bB | d procedure pS; begin if nextSymbol = ta then begin getNextSymbol; pA end else if nextSymbol = tb then begin getNextSymbol; pB end else error end;

Page 12: 제  7  장  LL  구문 분석

procedure pA; begin if nextSymbol = ta then begin getNextSymbol; pA end else if nextSymbol = tc then getNextSymbol else error end;

procedure pB; ...

/* main */ begin getNextSymbol; pS; if nextSymbol = '$' then accept else error end.

※ procedure call sequence ::= leftmost derivation

= aac$

Page 13: 제  7  장  LL  구문 분석

The main problem in constructing a recursive-descent syntax analyzer is the choice of productions when a procedure is first entered. To resolve this problem, we can compute the lookahead of each production.

LOOKAHEADLOOKAHEAD of a production

Definition : LOOKAHEAD(A)

= FIRST({ | S A V∈ T*}).

Meaning : the set of terminals which can be generated by

and if , then FOLLOW(A) is added to the set.

Computing formula: LOOKAHEAD(A X1X2...Xn)

= FIRST(X1X2...Xn) FOLLOW(A)

*

* *

Page 14: 제  7  장  LL  구문 분석

S aSA | A c

Nullable Set = {S}

FIRST(S) = {a, } FOLLOW(S) = {$,c}FIRST(A) = {c} FOLLOW(A) = {$,c}

LOOKAHEAD(S aSA) = FIRST(aSA) FOLLOW(S) = {a}LOOKAHEAD(S ) = FIRST() FOLLOW(S) = {$,c}LOOKAHEAD(A c) = FIRST(c) FOLLOW(A) = {c}

※ LOOKAHEAD 를 구하는 순서 :Nullable => FIRST => FOLLOW => LOOKAHEAD

Page 15: 제  7  장  LL  구문 분석

Definition : A | P, ∈ LOOKAHEAD(A ) ∩ LOOKAHEAD(A ) = .

Meaning : for each distinct pair of productions with the sameleft-hand side, it can select the unique alternate that

derives a string beginning with the input symbol.

The grammar G is said to be strong LL(1) if it satisfies the strong LL condition.

ex) G : S aSA | A c

LOOKAHEAD(S aSA) = {a} LOOKAHEAD(S ) = FOLLOW(S) = {$, c}

LOOKAHEAD(S aSA) ∩ LOOKAHEAD(S ) =

G 는 strong LL(1) 이다 .

Page 16: 제  7  장  LL  구문 분석

If a grammar is strong LL(1), we can construct a parser for sentences of the grammar using the following scheme.

Terminal procedure:a V∈ T,

procedure pa; /* getNextSymbol => scanner */

begin

if nextSymbol = ta then getNextSymbol

else error

end;

※ getNextSymbol : 스 캐 너 에 해 당 하 는 루 틴 으 로 입 력 스 트 림 으 로 부 터 토큰 한 개를 만들어 변수 nextSymbol 에 배정한다 .

Page 17: 제  7  장  LL  구문 분석

Text p.284

LL Parsing

A V∈ N, procedure pA;var i: integer;begin

case nextSymbol of

LOOKAHEAD(A X1X2...Xm): for i := 1 to m do pXi;

LOOKAHEAD(A Y1Y2...Yn): for i := 1 to n do pYi; :

LOOKAHEAD(A Z1Z2...Zr): for i := 1 to r do pZi; LOOKAHEAD(A ): ;

otherwise: error end /* case */ end;

Page 18: 제  7  장  LL  구문 분석

※ The input buffer contains the string to be parsed, followed by $.

Page 19: 제  7  장  LL  구문 분석

rnonterminals X

terminals

Current input symbol 과 stack top symbol 사이의 관계에 따라 parsing.

Initial configuration : STACK INPUT $S $ Parsing table(LL) : parsing action 을 결정지어 줌 .

※ M[X,a] = r : stack top symbol 이 X 이고 current symbol 이 a 일 때 , r 번 생성 규칙으로 expand.

Page 20: 제  7  장  LL  구문 분석

Parsing Actions

X : stack top symbol, a : current input symbol

1. if X = a = $, then accept.

2. if X = a, then pop X and advance input.

3. if X V∈ N, then if M[X,a] = r (XABC), then replace X by ABC

else error.

Page 21: 제  7  장  LL  구문 분석

Text p.291

Algorithm Predictive_Parser_Action;begin // set ip to point to the first symbol of $; repeat // let X be the top stack symbol and a the symbol pointed to by ip; if X is a terminal or $ then if X = a then pop X from the stack and advance ip else error(1) else /* X is nonterminal */ if M[X,a] = X Y1Y2...Yk then begin pop X from the stack; push YkYk-1,...,Y1 onto the stack, with Y1 on top; output the production X Y1Y2...Yk

end else error(2) until X = a = $ /* stack is empty */end.

Page 22: 제  7  장  LL  구문 분석

terminalsnonterminal a b $

S 1 2 .

A 3 4 .

• G : 1. S aSb

2. S bA

3. A Aa

4. A b

string : aabbbb

• Parsing Table:

Page 23: 제  7  장  LL  구문 분석

※ How to construct a predictive parsing table for the grammar.

STACK INPUT ACTIONS OUTPUT

$S

$bSa

$bS

$bbSa

$bbS

$bbAb

$bbA

$bbb

$bb

$b

$

aabbbb$

aabbbb$

abbbb$

abbbb$

bbbb$

bbbb$

bbb$

bbb$

bb$

b$

$

expand 1

pop a and advance expand 1

pop a and advance

expand 2

pop b and advance

expand 4

pop b and advance

pop b and advance

pop b and advance

Accept

1

1

2

4

Page 24: 제  7  장  LL  구문 분석

VTVN

X

a

main idea : If A is a production with a in FIRST(), then the parser will expand A by when the

current input symbol is a. And if , then we should again expand A by when the current input symbol is in FOLLOW(A).

parsing table(LL):

M[X,a] = r : expand X with r-production blank : error

*

Page 25: 제  7  장  LL  구문 분석

Construction Algorithm :

for each production A,

1. a FIRST(∈ ), M[A,a] := <A>

2. if * , then

b FOLLOW(A), M[A,b] := <A∈ >.

Page 26: 제  7  장  LL  구문 분석

G: 1. E TE’ 2. E’ +TE’ 3. E’ 4. T FT’ 5. T’ FT’ 6. T’

7. F (E) 8. F id

FIRST(E) = FIRST(T) = FIRST(F) = { ( , id } FIRST(E’) = { + , }FIRST(T’) = { , }

FOLLOW(E) = FOLLOW(E’) = { ) , $ } FOLLOW(T) = FOLLOW(T’) = { + , ) , $ }FOLLOW(F) = { + , , ) , $ }

Page 27: 제  7  장  LL  구문 분석

Parsing Table:

Terminal

Nonterminalid + * ( ) $

E 1 1

E’ 2 3 3

T 4 4

T’ 6 5 6 6

F 8 7

Page 28: 제  7  장  LL  구문 분석

LL(1) Grammar

::= a grammar whose parsing table has no multiply-defined entries.

multiply 정의되면 어느 rule 로 expand 해야 할 지 결정할 수 없기 때문에 deterministic 하게 parsing 할 수 없다 .

LL(1) condition:

A | ,

1. FIRST( ) ∩ FIRST() = .

2. if , then FOLLOW(A) ∩ FIRST() = .*

Page 29: 제  7  장  LL  구문 분석

G : 1. S iCtSS’ 2. S a 3. S’ eS 4. S’ 5. C b FIRST(S) = {i,a} FIRST(S') = {e, } FIRST(C) = {b}

FOLLOW(S) = {$,e} FOLLOW(S') = {$,e} FOLLOW(C) = {t}

Parsing Table:

M[S',e] := <3,4> 로 중복으로 정의되었음 . 여기서 , stack top 이 S' 이고 input symbol 이 e 일 때 3 번 rule 로

expand 해야 할 지 , 4 번 rule 로 expand 해야 하는지 알 수 없다 .그러므로 G 는 LL(1) grammar 가 아니다 .

TerminalsNonterminals a b e i t $

S 2 1

S’ 3,4 4

C 5

Page 30: 제  7  장  LL  구문 분석

[ 예제 7.15] --- text p.298

G : S aA | abA A Ab | a

: abab