Tutorial 2 solutions

8 0 0
Tutorial 2 solutions

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

Thông tin tài liệu

Solution 2 Final doc SOLUTION 2 1 S à (L) | a L à L,S | S a) What are the terminal, nonterminal and start symbol? Terminals {‘,’,a,’(‘,’)’};nonterminals {S, L}; start symbol S b) Find parse tree for t[.]

SOLUTION S (L) | a L L,S | S a) What are the terminal, nonterminal and start symbol? Terminals: {‘,’,a,’(‘,’)’};nonterminals: {S, L}; start symbol: S b) Find parse tree for the following sentences: (a,a) (a,(a,a)) (a,((a,a),(a,a))) c) Construct a leftmost derivation for each sentence given in (b) S ⇒ (L) ⇒ (L,S) ⇒ (S,S) ⇒ (a,S) ⇒ (a,a) S ⇒ (L) ⇒ (L,S) ⇒ (S,S) ⇒ (a,S) ⇒ (a,(L)) ⇒ (a,(L,S)) ⇒ (a,(S,S)) ⇒ (a,(a,S)) ⇒ (a,(a,a)) S ⇒ (L) ⇒ (L,S) ⇒ (S,S) ⇒ (a,S) ⇒ (a,(L)) ⇒ (a,(L,S)) ⇒ (a,(S,S)) ⇒ (a,((L),S)) ⇒ (a,((L,S),S)) ⇒ (a,((S,S),S)) ⇒ (a,((a,S),S)) ⇒ (a,((a,a),S)) ⇒ (a,((a,a),(L))) ⇒ (a,((a,a),(L,S))) ⇒ (a,((a,a),(S,S))) ⇒ (a,((a,a),(a,S))) ⇒ (a,((a,a),(a,a))) d) Construct a rightmost derivation for each sentence given in (b) S ⇒ (L) ⇒ (L,S) ⇒ (L,a) ⇒ (S,a) ⇒ (a,a) S ⇒ (L) ⇒ (L,S) ⇒ (L,(L)) ⇒ (L,(L,S)) ⇒ (L,(L,a)) ⇒ (L,(S,a)) ⇒ (L,(a,a)) ⇒ (S,(a,a)) ⇒ (a,(a,a)) S ⇒ (L) ⇒ (L,S) ⇒ (L,(L)) ⇒ (L,(L,S)) ⇒ (L,(L,(L,S))) ⇒ (L,(L,(L,a))) ⇒ (L,(L,(S,a))) ⇒ (L,(L,(a,a))) ⇒ (L,(S,(a,a))) ⇒ (L,((L),(a,a))) ⇒ (L,((L,S),(a,a))) ⇒ (L,((L,a),(a,a))) ⇒ (L,((S,a),(a,a))) ⇒ (L,((a,a),(a,a))) ⇒ (S,((a,a),(a,a))) ⇒ (a,((a,a),(a,a))) e) What is the language generated by this grammar? A type of expression in which ‘,’ is used as the only binary operator Operands can be either a or a nested parenthesized expression Consider the following grammar S aSbS| bSaS | ∈ a Find a rightmost derivation for abab S ⇒ aSbS ⇒ aSbaSbS ⇒ aSbaSb ⇒ aSbab ⇒ abab S ⇒ aSbS ⇒ aSb ⇒ abSaSb ⇒ aSbab ⇒ abab b Construct all possible parse trees for abab c Is this grammar ambiguous? Why? The grammar is ambiguous since we can construct more than one parse tree for abab d What is the language generated by this grammar? L = {w| na(w) = nb(w)} Write a grammar that generates all of boolean expressions Construct the corresponding parse tree for not (true or false) bexp bexp or bexp | bexp and bexp | not bexp | (bexp) | true | false | ID S (L) | a L L,S | S a Eliminate the left-recursion from the grammar in Exercise L L,S | S ó L SL' L’ ,SL’ | ∈ b Compute the First, Follow and Select sets for the transformed grammar Transformed grammar S (L) | a L SL' L’ ,SL’ | ∈ First sets: First(S) = {(,a} First(L) = {(,a} First(L’) = {,,∈} Follow sets: Follow(S) = {$,,,)} Follow(L) = {)} Follow(L’) = {)} Select sets: Select(Sà(L)) = {(} Select(Sàa) = {a} Select(LàSL’) = {(,a} Select(L’à,SL’) = {,} Select(L’à∈) = {)} c Construct a predictive parser for the transformed grammar S L L’ ( Sà (L) Là SL’ ) a Sàa Là SL’ , $ L’à,SL’ L’à∈ d Show the behavior of the parser for the sentences given in Exercise 1b (a,a) Stack $S $)L( $)L $)L’S $)L’a $)L’ $)L’S, $)L’S $)L’a $)L’ $) $ Input (a,a)$ (a,a)$ a,a)$ a,a)$ a,a)$ ,a)$ ,a)$ a)$ a)$ )$ )$ S Stack $S $)L( $)L $)L’S $)L’a $)L’ $)L’S, $)L’S $)L’)L( $)L’)L $)L’)L’S Input (a,(a,a))$ (a,(a,a))$ a,(a,a))$ a,(a,a))$ a,(a,a))$ ,(a,a))$ ,(a,a))$ (a,a))$ (a,a))$ a,a))$ a,a))$ Ouput Sà(L) LàSL’ Sàa L’à,SL’ Sàa L’à∈ (a,(a,a)) Ouput Sà (L) Là SL’ Sàa L’à,SL’ Sà (L) Là SL’ $)L’)L’a $)L’)L’ $)L’)L’S, $)L’)L’S $)L’)L’a $)L’)L’ $)L’) $)L’ $) $ a,a))$ ,a))$ ,a))$ a))$ a))$ ))$ ))$ )$ )$ $ Sàa L’à,SL’ Sàa L’à∈ L’à∈ (a,(a,a),(a,a))) Stack $S $)L( $)L $)L’S $)L’a $)L’ $)L’S, $)L’S $)L’)L( $)L’)L $)L’)L’S $)L’)L’)L( $)L’)L’)L $)L’)L’)L’S $)L’)L’)L’a $)L’)L’)L’ $)L’)L’)L’S, $)L’)L’)L’S $)L’)L’)L’a $)L’)L’)L’ $)L’)L’) $)L’)L’ $)L’)L’S, $)L’)L’S $)L’)L’)L( $)L’)L’)L Input (a,((a,a), (a,a)))$ (a,((a,a), (a,a)))$ a,((a,a), (a,a)))$ a,((a,a), (a,a)))$ a,((a,a), (a,a)))$ ,((a,a), (a,a)))$ ,((a,a), (a,a)))$ ((a,a), (a,a)))$ ((a,a), (a,a)))$ (a,a), (a,a)))$ (a,a), (a,a)))$ (a,a), (a,a)))$ a,a), (a,a)))$ a,a), (a,a)))$ a,a), (a,a)))$ ,a), (a,a)))$ ,a), (a,a)))$ a), (a,a)))$ a), (a,a)))$ ), (a,a)))$ ), (a,a)))$ , (a,a)))$ , (a,a)))$ (a,a)))$ (a,a)))$ a,a)))$ Ouput Sà (L) Là SL’ Sàa L’à,SL’ Sà (L) Là SL’ Sà (L) Là SL’ Sàa L’à,SL’ Sàa L’à∈ L’à,SL’ Sà (L) $)L’)L’)L’S $)L’)L’)L’a $)L’)L’)L’ $)L’)L’)L’S, $)L’)L’)L’S $)L’)L’)L’a $)L’)L’)L’ $)L’)L’) $)L’)L’ $)L’) $)L’ $) $ a,a)))$ a,a)))$ ,a)))$ ,a)))$ a)))$ a)))$ )))$ )))$ ))$ ))$ )$ )$ $ Là SL’ Sàa L’à,SL’ Sàa L’à∈ L’à∈ L’à∈ Eliminate left-recursion and left-factoring for the grammar constructed in Exercise 3, if present bexp bexp or bexp | bexp and bexp | not bexp | (bexp) | true | false | ID - Eliminate left-factoring bexp bexp bexp_tail | not bexp | (bexp) | true | false | ID bexp_tail or bexp | and bexp - Eliminate left-recursion bexp bexp_head bexp_tail’ bexp_head not bexp | (bexp) | true | false | ID bexp_tail’ bexp_tail bexp_tail’ | ∈ bexp_tail or bexp | and bexp TUTORIAL Consider the following grammar E T+E | T T id a Construct the collection of sets of LR(0) items for this grammar b Construct the parsing table using SLR method c Show the moves according to the table constructed above on the inputs of id+id and 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 Aà∈ Bà∈ Hint: Note that S •∈ is concurrently equivalent to S ∈•, or we can write S à• Show that the following grammar is LR(1) but not SLR(1) S Aa | bAc | dc | bda Aàd Show that the following grammar is LR(1) but not LALR(1) S As | bAc | Bc | bBa Aàd Bàd

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

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan