طراحی کامپایلر Top Down Parsing LL1)
تعریف top down parsing Parse tree را از ریشه به سمت برگها می سازد. دو نوع LL1), LLk) Recursive descent
مثال G = {S},{, ) }, P, S) S S S ) S ε ))$
مثال S S ) S ε ))$
مثال S S ) S ε ))$
مثال S S ) S ε ))$
پیاده سازی Stack LL1) parsing table
مثال push S 1. S S ) 2. S ε S N/T ) $ S 1 2 2 ))$ S
مثال rule 1 1. S S ) 2. S ε N/T ) $ S 1 2 2 ))$ S )
مثال match 1. S S ) 2. S ε N/T ) $ S 1 2 2 ))$ S )
مثال 1. S S ) 2. S ε N/T ) $ S 1 2 2 ))$ S )
مثال rule 1 1. S S ) 2. S ε N/T S 1 ) 2 $ 2 S ))$ ) )
مثال match 1. S S ) 2. S ε N/T S 1 ) 2 $ 2 S ))$ ) )
مثال 1. S S ) 2. S ε N/T ) $ S 1 2 2 S ))$ ) )
مثال rule 2 1. S S ) 2. S ε N/T ) $ S 1 2 2 ))$ ) )
مثال ) match 1. S S ) 2. S ε N/T ) $ S 1 2 2 ))$ ) )
مثال ) match 1. S S ) 2. S ε N/T ) $ S 1 2 2 ))$ )
مثال accept 1. S S ) 2. S ε N/T ) $ S 1 2 2 ))$
چگونگی ساختن جدول 1. S S ) 2. S ε N/T ) $ S 1 2 2 First S) Follow S)
چگونگی ساختن جدول جدولی که ستونهای آن الفبا و سطرهای آن متغیرهای گرامر هستند را ایجاد می کنیم. مجموعه first هر متغیر و قانونهای مربوطه اش را پیدا کرده و در خانه مربوطه شماره قانون را وارد می کنیم. برای متغیرهایی که به رشته تهی می روند, follow را پیدا می کنیم نه دقیقا!!). مجموعه
تعریف مجموعه های first و follow مجموعه : first زیر مجموعه ای از الفبا که نشان می دهد رشته هایی که از یک متغیر بدست می آیند با چه حرفی شروع می شوند. مجموعه : follow زیر مجموعه ای از الفبا که نشان می دهد رشته هایی که در دنباله یک متغیر می آیند با چه حرفی شروع می شوند.
پیدا کردن مجموعه first A ε A a A aβ A αβ firsta) = {ε} firsta) = {a} firsta) = {a} firsta) = firstfirstα)firstβ)) A α1 α2 αn firsta)= U firsta) A αi
مثال S aas bc A cc da ε C AS firsts) S aas = {a} firsts) S bc = {b} firsts)={a,b} firsta) A cc = {c} firsta da) A da = {d} firsta)={c,d,ε} firstc) = firstfirsta)firsts))= first{c,d,ε}{a,b})= first{ca,cb,da,db,a,b})= {c,d,a,b}
پیدا کردن مجموعه follow A is start symbol followa) $ A αbβ followb) = firstfirstβ)followα)) A1 α1bβ1 A2 α2bβ2 A3 α3bβ3. followb) = U followb) Ai αibβi هنگام افتادن در دور از محاسبه صرف نظر می کنیم.
S aas bc A cc da ε C AS S is start symbol follows) S aas = firstfirstε)follows))= firstfollows))= follows) follows) C AS = firstfirstε)followc))= firstfollowc))= followc) follows) $ مثال follows)={$} U followc)
مثال S aas bc A cc da ε C AS followc) S bc = firstfirstε)follows))= firstfollows))= follows) S is start symbol FollowS)= FollowC)= {$}
S aas bc A cc da ε C AS S is start symbol followa) A da = firstfirstε)followa))= firstfollowa))= followa) followa) S aas = firstfirsts)follows))= first{a,b}{$})= {a,b} followa) C AS = firstfirsts)followc))= first{a,b}{$})= {a,b} followa)={a,b} مثال
مثال ساختن جدول 1. S S ) 2. S ε N/T ) $ S
مثال ساختن جدول 1. S S ) 2. S ε first S) ) S S) = { } N/T ) $ S 1
مثال ساختن جدول N/T 1. S S ) 2. S ε ) $ S 1 2 2 first S ε ) = { ε } follow S ) $ follow S ) S S) = firstfirst{ ) })follows))= first{ ) }follows))= { ) } follow S) = { $, ) }
مثال دیگر block_stmt stmt stmt_seq stmt_seq ; block_stmt ε stmt s
مثال دیگر ادامه) block_stmt stmt stmt_seq stmt_seq ; block_stmt ε stmt s firstblock_stmt)= firstfirststmt)firststmt_seq))= first{s}{;, ε})= first{s;, s}) = {s} firststmt_seq)= firststmt_seq) stmt_seq ; block_stmt U firststmt_seq) stmt_seq ε = {;} U {ε} = {;, ε} firststmt)= {s}
مثال دیگر ادامه) 1. block_stmt stmt stmt_seq 2. stmt_seq ; block_stmt 3. ε 4. stmt s block_stmt stmt_seq stmt s 1 4 ; 2 $
مثال دیگر ادامه) block_stmt stmt stmt_seq stmt_seq ; block_stmt ε stmt s followstmt_seq)= firstfirstε) followblock_stmt)) = followblock_stmt) = {$} followblock_stmt)= {$} U followblock_stmt) stmt_seq ; block_stmt = {$} U firstfirstε) followstmt_seq)) = {$} U followstmt_seq)
مثال دیگر ادامه) 1. block_stmt stmt stmt_seq 2. stmt_seq ; block_stmt 3. ε 4. stmt s block_stmt stmt_seq stmt s 1 4 ; 2 $ 3
چه گرامرهایی LL1) نیستند گرامرهایی که در حداقل یکی از خانه های جدول, بیش از یک قانون نوشته شده باشد. یا: A α1 α2 αn firstαi) Λ firstαj) = {} ε E firsta) firsta) Λ followa) = {}
مثال از گرامری که LL1) نیست. stmt if_stmt other if_stmt if exp ) stmt else_part else_part else stmt ε exp 0 1
مثال از گرامری که LL1) نیست. stmt if_stmt other if_stmt if exp ) stmt else_part else_part else stmt ε exp 0 1 firststmt)= firstif_stmt) U {other}= { if, other} firstif_stmt)= {if} firstelse_part)= {else, ε} firstexp)= {0,1} followelse_part)= followif_stmt)= followstmt)= firstfirstelse_part)followif_stmt)) U followelse_part) U {$} = first{else, ε}followstmt) U {$}= {else, $ }
مثال از گرامری که LL1) نیست. ادامه) stmt if_stmt other if_stmt if exp ) stmt else_part else_part else stmt ε exp 0 1 firststmt)= firstif_stmt) U {other}= { if, other} firstif_stmt)= {if} firstelse_part)= {else, ε} firstexp)= {0,1} followelse_part)= {else, $ } firstelse_part) Λ followelse_part) <> {}
مثال از گرامری که LL1) نیست. ادامه) 1. stmt if_stmt 2. other 3. if_stmt if exp ) st else_part 4. else_part else stmt s is if 1 3 ot 2 ) else 0 1 $ 5. ε 6. exp 0 7. 1 ep ex 4,5 6 7 5 firststmt)= firstif_stmt) U {other}= { if, other} firstif_stmt)= {if} firstelse_part)= {else, ε} firstexp)= {0,1} followelse_part)= {else, $ }
دو مشکل که با رفع آنها ممکن است گرامر به LL1) تبدیل شود. Left recursion Common factor بازگشتی از چپ فاکتور مشترک
بازگشتی از چپ Left recursion A Aa b firsta)= {b} U firsta)=? baa$
حذف بازگشتی از چپ A Aa b A ba A aa ε A Aα1 Aα2 Aαn β1 β2 βm A β1a β2a βma A α1a α2a αna ε
مثال exp exp addop term term exp term exp exp addop term exp ε
حذف بازگشتی از چپ غیر مستقیم non immediate A Bα B Aβ از جایگزینی باید استفاده کرد. با اولویت بندی متغیرها, از افتادن در دور هنگام جایگذاری جلوگیری می کنیم.
A Ba Aa c B Bb Ab d مثال -1 اولویت -3 B > A جايگذاري A در B A BaA ca A aa ε -2 حذف بازگشتی از چپ B Bb BaA b ca b d A A BaA ca 4- حذف بازگشتي از چپ B A aa ε A BaA ca B Bb Ab d A aa ε B ca bb db B bb aa bb ε
فاکتور مشترک Common factor A αβ1 αβ2 firsta αβ1) Λ firsta αβ2) <> {}
حذف فاکتور مشترک A αβ1 αβ2 A αa A β1 β2
مثال exp exp addop term term term term mulop fact fact fact exp) n addop + - mulop * Left recursion : exp term
مثال exp exp addop term term term term mulop fact fact fact exp) n addop + - mulop * exp term exp exp addop term exp ε term fact term term mulop fact term ε fact exp) n addop + - mulop *
مثال block_stmt stmt ; block_stmt stmt stmt s Common factor block_stmt
مثال block_stmt stmt ; block_stmt stmt stmt s block_stmt stmt block_stmt block_stmt ; block_stmt ε stmt s
مثال کلی جدول پارس و پارس رشته n+n*n exp term exp exp addop term exp ε term fact term term mulop fact term ε fact exp) n addop + - mulop *
مثال کلی جدول پارس و پارس رشته n+n*n exp term exp exp addop term exp ε term fact term term mulop fact term ε fact exp) n addop + - mulop * firstexp) firstexp ) firstterm) firstterm ) firstfact) firstaddop) firstmulop)
مثال کلی جدول پارس و پارس رشته n+n*n exp term exp exp addop term exp ε term fact term term mulop fact term ε fact exp) n addop + - mulop * firstexp) firstexp ) firstterm) firstterm ) firstfact) = {,n} firstaddop) = {+,-} firstmulop) = {*}
مثال کلی جدول پارس و پارس رشته n+n*n exp term exp exp addop term exp ε term fact term term mulop fact term ε fact exp) n addop + - mulop * firstexp) firstexp ) = {+,-,ε} firstterm) = {,n} firstterm ) = {*, ε} firstfact) = {,n} firstaddop) = {+,-} firstmulop) = {*}
مثال کلی جدول پارس و پارس رشته n+n*n exp term exp exp addop term exp ε term fact term term mulop fact term ε fact exp) n addop + - mulop * firstexp) = {,n} firstexp ) = {+,-,ε} firstterm) = {,n} firstterm ) = {*, ε} firstfact) = {,n} firstaddop) = {+,-} firstmulop) = {*}
مثال کلی جدول پارس و پارس رشته n+n*n exp term exp exp addop term exp ε term fact term term mulop fact term ε fact exp) n addop + - mulop * followexp ) followterm )
مثال کلی جدول پارس و پارس رشته n+n*n exp term exp exp addop term exp ε term fact term term mulop fact term ε fact exp) n addop + - mulop * followexp ) followterm ) followexp) = {$,)}
مثال کلی جدول پارس و پارس رشته n+n*n exp term exp exp addop term exp ε term fact term term mulop fact term ε fact exp) n addop + - mulop * followexp ) = {$, )} followterm ) followexp) = {$,)}
مثال کلی جدول پارس و پارس رشته n+n*n exp term exp exp addop term exp ε term fact term term mulop fact term ε fact exp) n addop + - mulop * followexp ) = {$, )} followterm ) followexp) = {$,)} followterm) = first{+,-,ε}{$,)})= {+,-,$,)}
مثال کلی جدول پارس و پارس رشته n+n*n exp term exp exp addop term exp ε term fact term term mulop fact term ε fact exp) n addop + - mulop * followexp ) = {$, )} followterm ) = {+,-,$,)} followexp) = {$,)} followterm) = first{+,-,ε}{$,)})= {+,-,$,)}
مثال کلی جدول پارس و پارس رشته 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. n 9. addop + 10. - 11. mulop * exp exp term term fact add n 1 4 8 1 4 7 ) 3 6 + 2 6 9-2 6 10 * 5 $ 3 6 mul 11
مثال کلی جدول پارس و پارس رشته n+n*n exp exp term n 1 4 1 4 ) 3 + 2-2 * $ 3 Stack exp exp term exp term fact Input n+n*n$ n+n*n$ n+n*n$ term 6 6 6 5 6 fact 8 7 add 9 10 mul 11