Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
Narges S. Bathaeian
طراحی کامپایلر
Top Down ParsingLL(1)
Narges S. Bathaeian
top down parsingتعریف
Parse tree را از ریشه به سمت برگها می سازد .دو نوع
LL(1) , LL(k)Recursive descent
Narges S. Bathaeian
مثال
G = ({S},{‘(‘, ‘)’ }, P , S)
S ( S )S ε
S
(())$
Narges S. Bathaeian
مثال S ( S )S ε
(())$
Narges S. Bathaeian
مثال S ( S )S ε
(())$
Narges S. Bathaeian
مثال S ( S )S ε
(())$
Narges S. Bathaeian
پیاده سازی StackLL(1) parsing table
Narges S. Bathaeian
push S مثال
1. S ( S )2. S ε S
(())$
221S$)(N/T
S
Narges S. Bathaeian
rule 1مثال
1. S ( S )2. S ε
(())$
221S$)(N/T
)
S
(
Narges S. Bathaeian
) matchمثال
1. S ( S )2. S ε
(())$
221S$)(N/T
)
S
(
Narges S. Bathaeian
مثال 1. S ( S )2. S ε
(())$
221S$)(N/T
)
S
Narges S. Bathaeian
rule 1 مثال
1. S ( S )2. S ε
(())$
221S$)(N/T
)
)
S
(
Narges S. Bathaeian
) match مثال
1. S ( S )2. S ε
(())$
221S$)(N/T
)
)
S
(
Narges S. Bathaeian
مثال 1. S ( S )2. S ε
(())$
221S$)(N/T
)
)
S
Narges S. Bathaeian
rule 2 مثال
1. S ( S )2. S ε
(())$
221S$)(N/T
)
)
Narges S. Bathaeian
( match مثال
1. S ( S )2. S ε
(())$
221S$)(N/T
)
)
Narges S. Bathaeian
( match مثال
1. S ( S )2. S ε
(())$
221S$)(N/T
)
Narges S. Bathaeian
accept مثال
1. S ( S )2. S ε
(())$
221S$)(N/T
Narges S. Bathaeian
چگونگی ساختن جدول 1. S ( S )2. S ε
221S
$)(N/T
First (S) Follow (S)
Narges S. Bathaeian
چگونگی ساختن جدول جدولی که ستونهای آن الفبا و سطرهای آن متغیرهای
. گرامر هستند را ایجاد می کنیم هر متغیر و قانونهای مربوطه اش را پیدا firstمجموعه
. کرده و در خانه مربوطه شماره قانون را وارد می کنیم مجموعه , برای متغیرهایی که به رشته تهی می روند
follow نه دقیقا ( را پیدا می کنیم (!! .
Narges S. Bathaeian
follow و firstتعریف مجموعه های
زیر مجموعه ای از الفبا که نشان می دهد : firstمجموعه رشته هایی که از یک متغیر بدست می آیند با چه حرفی
.شروع می شوند زیر مجموعه ای از الفبا که نشان می : followمجموعه
دهد رشته هایی که در دنباله یک متغیر می آیند با چه .حرفی شروع می شوند
Narges S. Bathaeian
firstپیدا کردن مجموعه
A ε first(A) = {ε}A a first(A) = {a}A aβ first(A) = {a}A αβ first(A) = first(first(α)first(β))A α1 | α2 |…|αn first(A)= U first(A) A αi
Narges S. Bathaeian
مثال
S aAS | bCA cc | dA | εC AS
first(S) S aAS = {a}first(S) S bC = {b}first(S)={a,b}first(A) A cc = {c}first(A dA) A dA = {d}first(A)={c,d,ε}first(C) = first(first(A)first(S))=
first({c,d,ε}{a,b})= first({ca,cb,da,db,a,b})= {c,d,a,b}
Narges S. Bathaeian
followپیدا کردن مجموعه A is start symbol follow(A) ∃ $A αBβ follow(B) =
first(first(β)follow(Α))A1 α1Bβ1 | …A2 α2Bβ2 | …A3 α3Bβ3 | …
…. follow(B) = U follow(B) Ai αiBβi. هنگام افتادن در دور از محاسبه صرف نظر می کنیم
Narges S. Bathaeian
مثال S aAS | bCA cc | dA | εC AS
S is start symbol
follow(S) S aAS = first(first(ε)follow(S))= first(follow(S))= follow(S)
follow(S) C AS = first(first(ε)follow(C))= first(follow(C))= follow(C)
follow(S) ∃ $
follow(S)={$} U follow(C)
Narges S. Bathaeian
مثال S aAS | bCA cc | dA | εC AS
S is start symbol
follow(C) S bC = first(first(ε)follow(S))= first(follow(S))= follow(S)
Follow(S)= Follow(C)= {$}
Narges S. Bathaeian
مثال
S aAS | bCA cc | dA | εC AS
S is start symbol
follow(A) A dA = first(first(ε)follow(A))= first(follow(A))= follow(A)
follow(A) S aAS = first(first(S)follow(S))= first({a,b}{$})= {a,b}
follow(A) C AS = first(first(S)follow(C))= first({a,b}{$})= {a,b}
follow(A)={a,b}
Narges S. Bathaeian
مثال ساختن جدول 1. S ( S )2. S ε
S$)(N/T
Narges S. Bathaeian
مثال ساختن جدول 1. S ( S )2. S ε
1S$)(N/T
first ( S) ) S (S) = { ‘(‘ }
Narges S. Bathaeian
مثال ساختن جدول 1. S ( S )2. S ε
221S$)(N/T
first ( S ε ) = { ε }follow ( S ) ∃ $follow ( S ) S (S) =
first(first({ ‘)’})follow(S))=first({‘)’}follow(S))= {‘)’}
follow (S) = { $ , ‘)’}
Narges S. Bathaeian
مثال دیگر block_stmt stmt stmt_seqstmt_seq ; block_stmt | εstmt s
Narges S. Bathaeian
) ادامه (مثال دیگر block_stmt stmt stmt_seqstmt_seq ; block_stmt | εstmt s
first(block_stmt)= first(first(stmt)first(stmt_seq))= first({s}{; , ε})= first({s; , s}) = {s}
first(stmt_seq)= first(stmt_seq) stmt_seq ; block_stmt U first(stmt_seq) stmt_seq ε = {;} U {ε} = {; , ε}
first(stmt)= {s}
Narges S. Bathaeian
) ادامه (مثال دیگر 1. block_stmt stmt stmt_seq2. stmt_seq ; block_stmt 3. | ε4. stmt s
4stmt
2stmt_seq
1block_stmt
$;s
Narges S. Bathaeian
) ادامه (مثال دیگر block_stmt stmt stmt_seqstmt_seq ; block_stmt | εstmt s
follow(stmt_seq)= first(first(ε) follow(block_stmt)) = follow(block_stmt) = {$}
follow(block_stmt)= {$} U follow(block_stmt) stmt_seq ; block_stmt= {$} U first(first(ε) follow(stmt_seq)) = {$} U follow(stmt_seq)
Narges S. Bathaeian
) ادامه (مثال دیگر 1. block_stmt stmt stmt_seq2. stmt_seq ; block_stmt 3. | ε4. stmt s
4stmt
32stmt_seq
1block_stmt
$;s
Narges S. Bathaeian
نیستند؟ LL(1)چه گرامرهایی بیش از , گرامرهایی که در حداقل یکی از خانه های جدول
.یک قانون نوشته شده باشد : یا
A α1| α2 |…| αn first(αi) Λ first(αj) = {}
ε E first(A)first(A) Λ follow(A) = {}
Narges S. Bathaeian
. نیست LL(1)مثال از گرامری که stmt if_stmt | otherif_stmt if ( exp ) stmt else_partelse_part else stmt
| εexp 0
| 1
Narges S. Bathaeian
. نیست LL(1)مثال از گرامری که stmt if_stmt
| otherif_stmt if ( exp ) stmt else_partelse_part else stmt
| εexp 0
| 1
first(stmt)= first(if_stmt) U {other}= { if , other}
first(if_stmt)= {if}first(else_part)= {else , ε}first(exp)= {0,1}
follow(else_part)= follow(if_stmt)= follow(stmt)= first(first(else_part)follow(if_stmt)) U follow(else_part) U {$}= first({else, ε}follow(stmt) U {$}= {else , $ }
Narges S. Bathaeian
) ادامه (. نیست LL(1)مثال از گرامری که stmt if_stmt
| otherif_stmt if ( exp ) stmt else_partelse_part else stmt
| εexp 0
| 1
first(stmt)= first(if_stmt) U {other}= { if , other}
first(if_stmt)= {if}first(else_part)= {else , ε}first(exp)= {0,1}follow(else_part)= {else , $ }
first(else_part) Λ follow(else_part) <> {}
Narges S. Bathaeian
) ادامه (. نیست LL(1)مثال از گرامری که 1. stmt if_stmt 2. | other3. if_stmt if ( exp ) st else_part4. else_part else stmt 5. | ε6. exp 07. | 1
first(stmt)= first(if_stmt) U {other}= { if , other}
first(if_stmt)= {if}first(else_part)= {else , ε}first(exp)= {0,1}follow(else_part)= {else , $ }
76ex
54,5ep
3is
21s
$10else)(otif
Narges S. Bathaeian
دو مشکل که با رفع آنها ممکن است گرامر به LL(1)تبدیل شود .
Left recursionبازگشتی از چپ Common factorفاکتور مشترک
Narges S. Bathaeian
Left recursionبازگشتی از چپ
A Aa| b
first(A)= {b} U first(A)= ?
baa$
Narges S. Bathaeian
حذف بازگشتی از چپ A Aa
| b
A Aα1| Aα2|…| Aαn|β1| β2|…| βm
A bA’A’ aA’
| ε
A β1A’| β2A’|…| βmA’A’ α1A’| α2A’|…|
αnA’ | ε
Narges S. Bathaeian
مثال exp exp addop term
|term
exp term exp’exp’ addop term exp’
|ε
Narges S. Bathaeian
nonحذف بازگشتی از چپ غیر مستقیم immediate
A Bα | …B Aβ | …
. از جایگزینی باید استفاده کرد از افتادن در دور هنگام جایگذاری جلوگیری , با اولویت بندی متغیرها
. می کنیم
Narges S. Bathaeian
مثال A Ba | Aa | cB Bb | Ab | d
B > A اولویت-1
A حذف بازگشتی از چپ -2A BaA’ | cA’A’ aA’ | εB Bb | Ab | d
B در A جايگذاري -3A BaA’ | cA’A’ aA’ | εB Bb | BaA’b | cA’b | d
B حذف بازگشتي از چپ -4A BaA’ | cA’A’ aA’ | εB cA’bB’ | dB’B’ bB’ | aA’bB’ | ε
Narges S. Bathaeian
Common factorفاکتور مشترک
A αβ1 | αβ2 | …
first(A αβ1) Λ first(A αβ2) <> {}
Narges S. Bathaeian
حذف فاکتور مشترک A αβ1 | αβ2 | …
A αA’ | …A’ β1 | β2
Narges S. Bathaeian
مثال exp exp addop term
| termterm term mulop
fact | fact
fact (exp) | naddop +| -mulop *
Left recursion : expterm
Narges S. Bathaeian
مثال exp exp addop term
| termterm term mulop
fact | fact
fact (exp) | naddop +| -mulop *
exp term exp’exp’ addop term exp’
| εterm fact term’term’ mulop fact term’
| εfact (exp) | naddop +| -mulop *
Narges S. Bathaeian
مثال block_stmt stmt ; block_stmt
| stmtstmt s
Common factor block_stmt
Narges S. Bathaeian
مثال block_stmt stmt ; block_stmt
| stmtstmt s
block_stmt stmt block_stmt’block_stmt’ ; block_stmt
| εstmt s
Narges S. Bathaeian
جدول پارس و پارس رشته (مثال کلی n+n*n (
exp term exp’exp’ addop term exp’
| εterm fact term’term’ mulop fact term’
| εfact (exp) | naddop +| -mulop *
Narges S. Bathaeian
جدول پارس و پارس رشته (مثال کلی n+n*n (
first(exp)first(exp’) first(term)first(term’)first(fact)first(addop)first(mulop)
exp term exp’exp’ addop term exp’
| εterm fact term’term’ mulop fact term’
| εfact (exp) | naddop +| -mulop *
Narges S. Bathaeian
جدول پارس و پارس رشته (مثال کلی n+n*n (
first(exp)first(exp’) first(term)first(term’)first(fact) = {(,n}first(addop) = {+,-}first(mulop) = {*}
exp term exp’exp’ addop term exp’
| εterm fact term’term’ mulop fact term’
| εfact (exp) | naddop +| -mulop *
Narges S. Bathaeian
جدول پارس و پارس رشته (مثال کلی n+n*n (
first(exp)first(exp’) = {+,-,ε}first(term) = {(,n}first(term’) = {*, ε}first(fact) = {(,n}first(addop) = {+,-}first(mulop) = {*}
exp term exp’exp’ addop term exp’
| εterm fact term’term’ mulop fact term’
| εfact (exp) | naddop +| -mulop *
Narges S. Bathaeian
جدول پارس و پارس رشته (مثال کلی n+n*n (
first(exp) = {(,n}first(exp’) = {+,-,ε}first(term) = {(,n}first(term’) = {*, ε}first(fact) = {(,n}first(addop) = {+,-}first(mulop) = {*}
exp term exp’exp’ addop term exp’
| εterm fact term’term’ mulop fact term’
| εfact (exp) | naddop +| -mulop *
Narges S. Bathaeian
جدول پارس و پارس رشته (مثال کلی n+n*n (
follow(exp’) follow(term’)
exp term exp’exp’ addop term exp’
| εterm fact term’term’ mulop fact term’
| εfact (exp) | naddop +| -mulop *
Narges S. Bathaeian
جدول پارس و پارس رشته (مثال کلی n+n*n (
follow(exp’) follow(term’) follow(exp) = {$,)}
exp term exp’exp’ addop term exp’
| εterm fact term’term’ mulop fact term’
| εfact (exp) | naddop +| -mulop *
Narges S. Bathaeian
جدول پارس و پارس رشته (مثال کلی n+n*n (
follow(exp’) = {$, )}follow(term’) follow(exp) = {$,)}
exp term exp’exp’ addop term exp’
| εterm fact term’term’ mulop fact term’
| εfact (exp) | naddop +| -mulop *
Narges S. Bathaeian
جدول پارس و پارس رشته (مثال کلی n+n*n (
follow(exp’) = {$, )}follow(term’) follow(exp) = {$,)} follow(term) = first({+,-
,ε}{$,)})= {+,-,$,)}
exp term exp’exp’ addop term exp’
| εterm fact term’term’ mulop fact term’
| εfact (exp) | naddop +| -mulop *
Narges S. Bathaeian
جدول پارس و پارس رشته (مثال کلی n+n*n (
follow(exp’) = {$, )}follow(term’) = {+,-,$,)}follow(exp) = {$,)} follow(term) = first({+,-
,ε}{$,)})= {+,-,$,)}
exp term exp’exp’ addop term exp’
| εterm fact term’term’ mulop fact term’
| εfact (exp) | naddop +| -mulop *
Narges S. Bathaeian
جدول پارس و پارس رشته (مثال کلی n+n*n (
1. exp term exp’2. exp’ addop term exp’3. | ε4. term fact term’5. term’ mulop fact term’6. | ε7. fact (exp) 8. | n9. addop +10. | -11. mulop *
11mul109add
78fact65666term’
44term3223exp’
11exp$*-+)(n
Narges S. Bathaeian
جدول پارس و پارس رشته (مثال کلی n+n*n (
11mul
109add
78fact
65666term’
44term
3223exp’
11exp
$*-+)(n
n+n*n$exp’ term’ factn+n*n$exp’ termn+n*n$expInput Stack