1. Trang chủ
  2. » Công Nghệ Thông Tin

slike bài giảng môn chương trình dịch chương 4 syntax analysis

65 332 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 65
Dung lượng 168,56 KB

Nội dung

Syntax Analysis II Quan Thanh Tho qttho@dit.hcmut.edu.vn LR Parsing • The most general parsing technique • Usually known as LR(k) parsing technique –Left-to-right scanning –Rightmost derivation detected in reverse –k lookahead tokens, (pratically k ≤ 1) Faculty of CSE Syntax Analysis II 2 LR Parsing Techniques • Simple LR (SLR): easy to implement but not practically powerful • Canonical LR: powerful but expensive in terms of computation • Lookahead LR (LALR): reasonably powerful and expensive Faculty of CSE Syntax Analysis II 3 LR Parsing Algorithm LR Parsing Program Input a+b$ Stack Output Grammatically correct? Which productions involved? s m X m s m-1 action goto Faculty of CSE Syntax Analysis II 4 Stack • Stack-type data structure • Store a string of the form s 0 X 1 s 1 X 2 …X m s m where s m is on the top •X i : a grammar symbol •s i : a state • When implemented, grammar symbols are not necessarily stored in the stack •X 1 X 2 …X m î a part of a sentential form corresponding to the scanned part of input Faculty of CSE Syntax Analysis II 5 Action – Goto Table • Rows: States • Columns: grammar symbols, consisting of two parts: action (for terminals) and goto (for nonterminal) • action[s m ,a i ] – shift s, where s is a state – reduce by a rule A å β – accept – error • goto[s m ,A i ]ås n Faculty of CSE Syntax Analysis II 6 Input • Similar to that of LL(1) table-driven predictive parser • When parsed, the contents of input imply the unexpended part of the initial input string Faculty of CSE Syntax Analysis II 7 Example 1 0 1 2 3 4 5 s3 1 2 + id $ E T s4 a 5 r2 r2 r3 r3 s3 5 r1 r1 (1) EåE+T (2) EåT (3) Tåid Faculty of CSE Syntax Analysis II 8 Configuration • Configuration = stack contents + unexpended input • A configuration represents a sentential form (s 0 X 1 s 1 X 2 …X m s m ,a i a i+1 …a n $) úX 1 X 2 …X m a i a i+1 …a n Faculty of CSE Syntax Analysis II 9 LR Parsing Operational Mechanism • Initial configuration: (0, a 1 …a n $)) • Moving from the current configuration to a new one until reaching accept or error status • action[s m ,a i ] = shift s (s 0 X 1 s 1 X 2 …X m s m ,a i a i+1 …a n $) å (s 0 X 1 s 1 X 2 …X m s m a i s,a i+1 …a n $) • action[s m ,a i ] = reduce Aåβ (s 0 X 1 s 1 X 2 …X m s m ,a i a i+1 …a n $) å (s 0 X 1 s 1 X 2 … X m-r s m-r As, a i a i+1 …a n $) where r = |β|, goto[s m-r ,A] = s Faculty of CSE Syntax Analysis II 10 [...]... T 1 1 s4 a 2 r2 s3 5 r1 Faculty of CSE E T 0E1 +id$ T id 0E1 +4 id$ 5 0 E 1 + 4 id 3 $ $ 0E1 r3 4 +id$ 0E1+4T5 r2 3 r3 2 T id 0T2 + +id$ $ E E+T accept 5 r1 id + id Syntax Analysis II T + id E+ id E+T E 11 Example 3 (1)EåE+T (2) EåT (3) Tåid Stack 0 Input Rule id+$ 0 id 3 0 id $ s3 E T 1 1 s4 a 2 r2 +$ E 0E1 +$ 0E1 +4 $ T error r2 3 r3 2 T id 0T2 + +$ r3 4 s3 5 r1 Faculty of CSE 5 5 r1 Syntax Analysis. .. Tåid Faculty of CSE Syntax Analysis II 27 Example 7 (cont’d) I0 = closure({[E’å E]}) E’å E Eå E+T Eå T Tå id I1 = goto(I0,E) E’åE EåE +T Faculty of CSE Syntax Analysis II 28 Example 7 (cont’d) I2 = goto(I0,T) EåT I3 = goto(I0,id) Tåid I4 = goto(I1,+) EåE+ T Tå id Faculty of CSE Syntax Analysis II 29 Example 7 (cont’d) I5 = goto(I4,T) EåE+T goto(I4,id) = I3 Faculty of CSE Syntax Analysis II 30 Shift... CSE Syntax Analysis II 14 In-depth Operational Mechanism Analysis (cont’d) • How can we keep track of stack and on the – Re-check the grammar whenever the stack contents are changed å high computational cost • How can we manage many instances of and that may occur concurrently Faculty of CSE Syntax Analysis II 15 Example 4 • Given configuration (E+T,+id$) – 1 Faculty of CSE = T, 2 = E+T Syntax Analysis. .. Faculty of CSE Syntax Analysis II 24 Kernel Items • Kernel Items: S’å S and all items whose dots are not at the left end • Nonkernel items: other else (generate by applying closure on kernel items) Faculty of CSE Syntax Analysis II 25 Construction of Item Sets • C = {closure({[S’å S]})} • For each item set I and each grammar symbol X – if I’ = goto(I,X) and I C • add I’ to C Faculty of CSE Syntax Analysis. .. current input symbol: id Tå id is moveable, meaning so is EåE+ T Faculty of CSE Syntax Analysis II 21 Goto Operation (cont’d) • Goto operation: goto(I,X)= closure([Aå X is an item in I Faculty of CSE Syntax Analysis II ]) where Aå X 22 Example 6 • I: EåE+ T EåT Tå id • goto(I,T) EåE+ T • goto(I,id) Tåid EåE+T Faculty of CSE Syntax Analysis II 23 Augmented Grammar • When would we know that we have reached... grammar given in Example 1, compare your result which that given in Example 1 Faculty of CSE Syntax Analysis II 33 Conventional Notations • SLR ú SLR(1) (1 is usually omitted by default in conventional notation) • SLR ú Canonical LR(0), or LR(0) for short • Why 1? Why 0? Faculty of CSE Syntax Analysis II 34 Conventional Notations • SLR ú SLR(1) (1 is usually omitted by default in conventional notation)... = set of items – A å XYZ – A å XYZ – A å X YZ – A å XY Z – A å XYZ Faculty of CSE Syntax Analysis II 17 Closure Operation • An item corresponds to a track of a production currently applied • Problem: there may be multiple tracks coexisting E å E+ T å id T • Solution: Closure operation introduced Faculty of CSE Syntax Analysis II 18 Closure Operation (cont’d) • Closure(I): find all of tracks (items)... action-goto table Faculty of CSE Syntax Analysis II 31 Simple LR Parsing Table Construction • Construct C = {I0,I1,I3,…,In} • Ii ústate i • if Aå a in Ii and goto(Ii,a) = Ij, set action(i,a) = shift j • if Aå X in Ii and goto(Ii,X) = Ij, set goto(i,X) = shift j • If Aå in Ii , set action(i,a) = reduce Aå for each a in Follow(A) • [S’åS ,$] = accept Faculty of CSE Syntax Analysis II 32 Hands-on Exercise... possibly inferred from a set of items I – Add all items in I to closure(I) – If Aå B in I and B å is a production, add Bå to closure(I) Faculty of CSE Syntax Analysis II 19 Example 5 • I: EåE+ T EåT • closure(I) EåE+ T EåT Tå id Faculty of CSE Syntax Analysis II 20 Goto Operation • How to follow tracks in an item set, given a new input symbol a? – Aå – Aå a : possible to move on b : impossible to move... – 1: 1 lookahead token for whole item set Faculty of CSE Syntax Analysis II 35 Conventional Notations • SLR ú SLR(1) (1 is usually omitted by default in conventional notation) • SLR ú Canonical LR(0), or LR(0) for short • Why 1? Why 0? – 1: 1 lookahead token for whole item set – 0: 0 lookahead token for individual item Faculty of CSE Syntax Analysis II 36 . input string Faculty of CSE Syntax Analysis II 7 Example 1 0 1 2 3 4 5 s3 1 2 + id $ E T s4 a 5 r2 r2 r3 r3 s3 5 r1 r1 (1) EåE+T (2) EåT (3) Tåid Faculty of CSE Syntax Analysis II 8 Configuration •. CSE Syntax Analysis II 10 Example 2 (1)Eå E+T (2) EåT (3) Tå id Stack 0 0 id 3 0 T 2 0 E 1 0 E 1 + 4 0 E 1 + 4 id 3 0 E 1 + 4 T 5 0 E 1 Input id+id$ +id$ +id$ +id$ id$ $ $ $ Faculty of CSE Syntax. 3 (1)Eå E+T (2) EåT (3) Tå id Faculty of CSE Syntax Analysis II 12 0 1 2 3 4 5 s3 1 2 + id $ E T s4 a 5 r2 r2 r3 r3 s3 5 r1 r1 Stack 0 0 id 3 0 T 2 0 E 1 0 E 1 + 4 Input id+$ +$ +$ +$ $ Rule T→id E → T error Hands-on

Ngày đăng: 23/10/2014, 17:33

TỪ KHÓA LIÊN QUAN