13
http://plac.dongguk.ac.kr 제 7 강 모호한 문법 / LALR 파싱 테이블 구현 컴파일러 구성

plac.dongguk.ac · Comm. ACM 18:8, pp.441-452. . Every ambiguous grammar fails to be LR. So ambiguous grammars always arise the conflicts, shift-reduce or reduce-reduce. But some

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: plac.dongguk.ac · Comm. ACM 18:8, pp.441-452. . Every ambiguous grammar fails to be LR. So ambiguous grammars always arise the conflicts, shift-reduce or reduce-reduce. But some

http://plac.dongguk.ac.kr

제 7 강

모호한 문법 /

LALR 파싱 테이블 구현

컴파일러 구성

Page 2: plac.dongguk.ac · Comm. ACM 18:8, pp.441-452. . Every ambiguous grammar fails to be LR. So ambiguous grammars always arise the conflicts, shift-reduce or reduce-reduce. But some

http://plac.dongguk.ac.kr

Reference : Aho, A.V. and Johnson, S.C., and Ullman, J.D.

“Deterministic Parsing of Ambiguous Grammars” Comm. ACM 18:8, pp.441-452.

Every ambiguous grammar fails to be LR. So ambiguous grammars always arise the conflicts, shift-reduce or reduce-reduce. But some ambiguous grammars are quite useful in the specification of languages.

And also they can reduce the speed of a parser.

Page 3: plac.dongguk.ac · Comm. ACM 18:8, pp.441-452. . Every ambiguous grammar fails to be LR. So ambiguous grammars always arise the conflicts, shift-reduce or reduce-reduce. But some

http://plac.dongguk.ac.kr

충돌의 종류 shift-reduce conflict – can not decide whether to shift

or to reduce. reduce-reduce conflict – can not decide which of several

reductions to make.

충돌 해결 방법 These conflicts can be resolved using the

precedence and associativity information. Precedence : higher ⇒ shift

lower ⇒ reduce Associativity : left ⇒ reduce

right ⇒ shift

Page 4: plac.dongguk.ac · Comm. ACM 18:8, pp.441-452. . Every ambiguous grammar fails to be LR. So ambiguous grammars always arise the conflicts, shift-reduce or reduce-reduce. But some

http://plac.dongguk.ac.kr

+

I0[E′ →.E][E →.E+E][E →.E∗E][E →.(E)][E →.id]

I0[E′ →.E][E →.E+E][E →.E∗E][E →.(E)][E →.id]

[E →id.]I3

I6[E →(E.)][E →E.+E][E →E.∗E]

I1[E′ →E.][E →E.+E][E →E.∗E]

I4[E →E+.E][E →.E+E][E →.E∗E][E →.(E)][E →.id]

I5[E →.E∗E][E →.E+E][E →.E∗E][E →.(E)][E →.id]

I7

[E →E+E.][E →E.+E][E →E.∗E]

I8[E →E∗E.][E →E.+E][E →E.∗E]

[E →(E).]I9

E

id

(

id(

*)

*

E+

*

+E

*(

(

E

id

idI2[E →(.E)][E →.E+E][E →.E∗E][E →.(E)][E →.id]

E → E + E | E ∗ E | (E) | id

Page 5: plac.dongguk.ac · Comm. ACM 18:8, pp.441-452. . Every ambiguous grammar fails to be LR. So ambiguous grammars always arise the conflicts, shift-reduce or reduce-reduce. But some

http://plac.dongguk.ac.kr

states id + * ( ) $ EI7 r1,s4 r1,s5 r1 r1I8 r2,s4 r2,s5 r2 r2

Consider I7, I8

I7 에서 (I7, +) = r1, (I7, *) = s5

I8 에서 (I8, +) = r2, (I8, *) = r2

Page 6: plac.dongguk.ac · Comm. ACM 18:8, pp.441-452. . Every ambiguous grammar fails to be LR. So ambiguous grammars always arise the conflicts, shift-reduce or reduce-reduce. But some

http://plac.dongguk.ac.kr

[S →i.SeS][S →i.S][S →.iSeS][S →.iS][S →.a]

I2

[S' →.S][S →.iSeS][S →.iS][S →.a]

I0

[S →a.]I3

[S →iSe.S][S →.iSeS][S →.iS][S →.a]

I5

[S →iS.eS][S →iS.]

I4

[S →iSeS.]I6

[S' →S.]I1

S

i

i

ia

a S

e

S

.

.

a

[S →i.SeS][S →i.S][S →.iSeS][S →.iS][S →.a]

I2

[S' →.S][S →.iSeS][S →.iS][S →.a]

I0

[S →a.]I3

[S →iSe.S][S →.iSeS][S →.iS][S →.a]

I5

[S →iS.eS][S →iS.]

I4

[S →iSeS.]I6

[S' →S.]I1

S

i

i

ia

a S

e

S

.

.

a

The “Dangle-else ” Ambiguity S' → S

S → iSeS | iS | a

Page 7: plac.dongguk.ac · Comm. ACM 18:8, pp.441-452. . Every ambiguous grammar fails to be LR. So ambiguous grammars always arise the conflicts, shift-reduce or reduce-reduce. But some

http://plac.dongguk.ac.kr

ACTION Table GOTO Table

6 r1 r1

5 s2 s3

4 s5

3 r3

2 s2 s3

1

0 s3s2

symbolsstates i e a

6

r2

r3

4

acc

1

$ S

else : right associativity ∴ shift

string : iiaea

Page 8: plac.dongguk.ac · Comm. ACM 18:8, pp.441-452. . Every ambiguous grammar fails to be LR. So ambiguous grammars always arise the conflicts, shift-reduce or reduce-reduce. But some

http://plac.dongguk.ac.kr

Driver Routine

Token stream

Result of parsing

Parsing table

PGSGrammar

Parser Generating System

Page 9: plac.dongguk.ac · Comm. ACM 18:8, pp.441-452. . Every ambiguous grammar fails to be LR. So ambiguous grammars always arise the conflicts, shift-reduce or reduce-reduce. But some

http://plac.dongguk.ac.kr

symbols

states

Parsing Table 구조

ptbl[S,X] = shift : > 0

reduce : < 0 accept : NO_RULES + 1 error : 0

Page 10: plac.dongguk.ac · Comm. ACM 18:8, pp.441-452. . Every ambiguous grammar fails to be LR. So ambiguous grammars always arise the conflicts, shift-reduce or reduce-reduce. But some

http://plac.dongguk.ac.kr

...

X

Y

Z

...

Sm-2

Sm-1

Smsp

Parsing stack

Parsing stack은 병렬로 운행되는 Symbol stack과 State stack으로 구성

- Symbol stack : 문법 심벌 저장

- State stack : 상태 저장

Symbol Stack State Stack

Page 11: plac.dongguk.ac · Comm. ACM 18:8, pp.441-452. . Every ambiguous grammar fails to be LR. So ambiguous grammars always arise the conflicts, shift-reduce or reduce-reduce. But some

http://plac.dongguk.ac.kr

Text pp.361-365 에 수록

Mini C Grammar(Text. pp. 619-622) (1) number of rules : 97

(2) number of symbols : 85

(3) number of states : 153

AST를 위한 문법

Page 12: plac.dongguk.ac · Comm. ACM 18:8, pp.441-452. . Every ambiguous grammar fails to be LR. So ambiguous grammars always arise the conflicts, shift-reduce or reduce-reduce. But some

http://plac.dongguk.ac.kr

1. 부록 A : pp.619-622 LALR(1) 문법 AST를 구성하기에 적당한 형태 miniC.tbl

#define NO_RULES 97 #define GOAL_RULE (NO_RULES + 1) #define NO_SYMBOLS 85 #define NO_STATES 153

2. 10.3 절 : pp.434-437 부록 A에 있는 문법에 AST 정보를 추가 스탠포드 PGS의 입력 형태

Page 13: plac.dongguk.ac · Comm. ACM 18:8, pp.441-452. . Every ambiguous grammar fails to be LR. So ambiguous grammars always arise the conflicts, shift-reduce or reduce-reduce. But some

http://plac.dongguk.ac.kr

Scanner Parserperfect.mc right parse

Parsing table

Implement an LR parser for Mini C grammar. Problem Specifications

input : perfect.mc(Text p. 616-617) output : right parse methods :

use the existing scanner for Mini C. get the parsing table for Mini C at the web site.

http://plac.dongguk.ac.kr program an LR parser(Text pp.361-365)