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

Tutorial 3 solutions

6 0 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 6
Dung lượng 130,55 KB

Nội dung

1 1 Consider the following grammar E T+E | T T id a Construct the collection of sets of LR(0) items for this grammar Augmented grammar (0) E’ E (1) E T+E (2) E T (3) T id I0 = closure(E’ E) E’ •E E •T[.]

1 Consider the following grammar E Ỉ T+E | T T Ỉ id a Construct the collection of sets of LR(0) items for this grammar Augmented grammar: (0): E’ Ỉ E (1): E Ỉ T+E (2): E Ỉ T (3): T Ỉ id I0 = closure(E’ Ỉ E) E’ Ỉ •E E Ỉ •T+E E Ỉ •T T Ỉ •id goto(I0,E) = I1 E ặ Eã goto(I0,T) = I2 E Æ T•+E E Æ T• goto(I0,id) = I3 T Æ id• goto(I4,T) = I2 goto(I4,id) = I3 goto(I2,+) = I4 E ặ T+ãE E ặ ãT+E E ặ ãT T Æ •id goto(I4,E) = I5 E Æ T+E• b Construct the parsing table using SLR method id + s3 s4 r3 s3 $ E T acc r2 r3 r1 c Show the moves according to the table constructed above on the inputs of id+id and id+id+id Input Stack id+id$ +id$ id +id$ 0T2 id$ 0T2+4 $ T + id $ 0T2+4T2 $ 0T2+4E5 $ 0E1 Stack 0 id 0T2 0T2+4 T + id 0T2+4T2 0T2+4T2+4 T + T + id 0T2+4T2+4T2 0T2+4T2+4E5 0T2+4E5 0E1 Input id+id+id$ +id+id$ +id+id$ id+id$ +id$ +id$ id$ $ $ $ $ $ Show that the following grammar is LL(1) but not SLR(1) Also, show that the grammar is LR(1) as well S Ỉ AaAb | BbBa ∈ BỈ∈ * The grammar is LL(1): For SỈ AaAb | BbBa Select(SỈAaAb) = {a} Select(SỈBbBa) = {b} For A Ỉ ∈ First(∈) = {∈} and Follow(∈) ={a,b} For B Ỉ ∈ First(BỈ∈) = {∈} and Follow(BỈ∈) ={a,b} No conflict found Ỵ the grammar is LL(1) * The grammar is not SLR(1) (0): S’ Ỉ S (1): S ỈAaAb (2): S ỈBbBa (3): A Ỉ ∈ (4): B Ỉ I0 = closure(SặS) S ặ ãAaAb S ặ ã BbBa Aặã Bặã Since Follow(A)Follow(B) , state (corresponding to I0) yields a reduce/reduce conflict between A Ỉ ∈ and B Ỉ ∈ Thus, the grammar is not SLR(1) * The grammar is LR(1) I0 = closure(SặãS, $) S ặ •S, $ S Ỉ •AaAb, $ S Ỉ •BbBa, $ Aặã,a Bặã,b I1 = goto(I0, A) S ặ AãaAb, $ I3 = goto(I1,a) S ặ AaãAb, $ Aặã,b I6 = goto(I4,B) S ặ BbBãa, $ I7 = goto(I5,b) S ặ AaAbã, $ I4 = goto(I2,b) S ặ BbãBa, $ Bặã,a I8 = goto(I6,a) S ặ BbBaã, $ I5 = goto(I3,A) S ặ AaAãb, $ I9 = goto(I0,S) Sặ Sã, $ I2 = goto(I0, B) S ặ BãbBa, $ a r3 s3 b r4 $ S s4 r3 s7 s8 r1 r2 acc No conflict found Ỵ the grammar is LR(1) Show that the following grammar is LR(1) but not SLR(1) Augmented grammar (0): S’ Ỉ S (1): S Ỉ Aa (2): S Ỉ bAc (3): S Ỉ dc (4): S Ỉ bda (5): A Ỉ d B r4 S Ỉ Aa | bAc | dc | bda d A * The grammar is not SLR I1 = goto(I0,d) S ặ dãc A ặ dã I0 = closure(SặãS) S ặ ãS S ặ ãAa S Æ •bAc S Æ •dc S Æ •bda A Æ •d Since Follow(A) = {a,c}, a shift/reduce conflict will occur at state * The grammar is LR(1) I0 = closure(SặãS, $) S ặ ãS, $ S ặ ãAa, $ S ặ ãbAc, $ S ặ ãdc, $ S ặ •bda, $ A Ỉ •d, a I1 = goto(I0,S) S’ỈS•, $ I7 = goto(I3,d) S ặ bdãa, $ A ặ dã, c I8 = goto(I4,d) S ặ dcã, $ I4 = goto(I0,d) S ặ dãc, $ A ặ dã, a I9 = goto(I6,c) S ặ bAcã, $ I5 = goto(I2,a) S ặ Aaã, $ I2 = goto(I0,A) SặAãa, $ a I3 = goto(I0,b) S ặ bãAc, $ S ặ bãda, $ A ặ ãd, c b s3 I6 = goto(I3,A) S ặ bAãc, $ c d s4 s5 s7 r5 s8 s9 s10 r5 10 No conflict found Ỵ the grammar is LR(1) I10 = goto(I7,a) S ặ bdaã, $ $ S A acc r1 r3 r2 r4 Show that the following grammar is LR(1) but not LALR(1) S Ỉ As | bAc | Bc | bBa d BỈd Augmented grammar: (0): S’ Ỉ S (1): S Ỉ As (2): S Ỉ bAc (3): S Ỉ Bc (4): S Ỉ bBa (5): A Ỉ d (6): B Ỉ d * The grammar is LR(1) I0 = closure(SặãS,$) Sặã S,$ S ặ ã As, $ S ặ • bAc, $ S Ỉ • Bc, $ S Ỉ • bBa, $ A Ỉ • d, s B Ỉ • d, c I1 = goto(I0,S) S’ Ỉ S•, $ I2 = goto(I0,A) S ặ Aãs, $ I3 = goto(I0,B) S ặ Bãc, $ I4 = goto(I0,b) S ặ bãAc, $ S ặ bãBa, $ A ặ ãd, c B Æ •d, a I8 = goto(I4,A) S Æ bA•c, $ I5 = goto(I0,d) A ặ dã, s B ặ dã, c I10 = goto(I4,d) A ặ dã, c B ặ dã, a I6 = goto(I2,s) S ặ Asã, $ I11 = goto(I8,c) S ặ bAcã, $ I7 = goto(I3,c) S Æ Bc•, $ I12 = goto(I9,a) S Æ bBa•, $ I9 = goto(I4,B) S ặ bBãa, $ s 10 11 12 a b s4 c d s5 $ S A B acc s6 s7 s10 r5 r6 r1 r3 s11 s12 r6 r5 r2 r4 No conflict found Ỵ the grammar is LR(1) * The grammar is not LALR(1) When merging state and 10, we have reduce/reduce conflict, caused by the fact that Follow(A) = {s,c}∩ Follow(B) = {c,a} ≠ ∅

Ngày đăng: 11/04/2023, 11:05

w