Key to Homework #8 1. For each of the following context-free grammars (a) and (b) below, construct an LL(k) parser with minimum k according to the following.

Slides:



Advertisements
Similar presentations
1 Chapter Parsing Techniques. 2 Section 12.3 Parsing Techniques We know (via a theorem) that the context- free languages are exactly those languages.
Advertisements

 CS /11/12 Matthew Rodgers.  What are LL and LR parsers?  What grammars do they parse?  What is the difference between LL and LR?  Why do.
Bottom-up Parsing A general style of bottom-up syntax analysis, known as shift-reduce parsing. Two types of bottom-up parsing: Operator-Precedence parsing.
Regular Grammars Formal definition of a regular expression.
142 Parsing start (a, Z 0 /aZ 0 ) ( a, a/aa ) (b, a/  ) ( , Z 0 /Z 0 ) For a given CFG G, parsing a string w is to check if w  L(G) and, if it is,
Chapter 4 Normal Forms for CFGs Chomsky Normal Form n Defn A CFG G = (V, , P, S) is in chomsky normal form if each rule in G has one of.
156 LR(k) Parsing We learned that the typical strategy of LL(k) parsing is that if a nonterminal symbol A appears at the stack top, the parser chooses.
1 Module 34 CFG --> PDA construction –Shows that for any CFL L, there exists a PDA M such that L(M) = L –The reverse is true as well, but we do not prove.
1 Lecture 32 CFG --> PDA construction –Shows that for any CFL L, there exists a PDA M such that L(M) = L –The reverse is true as well, but we do not prove.
79 Regular Expression Regular expressions over an alphabet  are defined recursively as follows. (1) Ø, which denotes the empty set, is a regular expression.
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
Bottom-up parsing Goal of parser : build a derivation
Chapter 12: Context-Free Languages and Pushdown Automata
1 Dr. torng CFG → PDA construction Shows that for any CFL L, there exists a PDA M such that L(M) = L The reverse is true, but we skip the proof Parsing.
Formal Methods in SE Theory of Automata Qasiar Javaid Assistant Professor Lecture # 06.
Lecture # 3 Regular Expressions 1. Introduction In computing, a regular expression provides a concise and flexible means to "match" (specify and recognize)
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
Module 2 How to design Computer Language Huma Ayub Software Construction Lecture 8.
L ECTURE 3 Chapter 4 Regular Expressions. I MPORTANT T ERMS Regular Expressions Regular Languages Finite Representations.
1 Bottom-Up Parsing  “Shift-Reduce” Parsing  Reduce a string to the start symbol of the grammar.  At every step a particular substring is matched (in.
6/4/2016IT 3271 The most practical Parsers: Predictive parser: 1.input (token string) 2.Stacks, parsing table 3.output (syntax tree, intermediate codes)
Context Free Grammars.
Section 12.4 Context-Free Language Topics
1 Section 12.3 Context-Free Parsing We know (via a theorem) that the context-free languages are exactly those languages that are accepted by PDAs. When.
1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions.
1 1. Let A ={r, p, i } and B = { w, o, r, l, d }. What does each of the following (a), (b) and (c) denote? Briefly explain in plain English. (a) A * B.
More Parsing CPSC 388 Ellen Walker Hiram College.
Chapter 7 Pushdown Automata
1 Chapter 6 Simplification of CFGs and Normal Forms.
Chapter 19 LL(k) Grammars. 2 LL(k) Parsers n Can be developed using PDAs for parsing CFGs by converting the machines directly into program statements.
Mathematical Foundations of Computer Science Chapter 3: Regular Languages and Regular Grammars.
Lecture 2 Theory of AUTOMATA
Transparency No. 1 Formal Language and Automata Theory Homework 5.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Lecture 5: LR Parsing CS 540 George Mason University.
Lecture 03: Theory of Automata:2014 Asif Nawaz Theory of Automata.
1 1. Eliminate all  -transitions from the following FA without changing the number of states and the language accepted by the automaton. You should also.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
1 Key to Homework #3 (Models of Computation, Spring, 2001) Finite state control with a 4-way read/write head Figure (a)Figure (b) (over)
Lecture 17: Theory of Automata:2014 Context Free Grammars.
CONTEXT-FREE LANGUAGES
Theory of Computation Lecture #
Lecture # 2.
Introduction to LR Parsing
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
LR(k) grammars The Chinese University of Hong Kong Fall 2009
Unit-3 Bottom-Up-Parsing.
Chomsky Normal Form CYK Algorithm
Theory of Automata.
Compiler Construction
Top-Down Parsing.
Subject Name:COMPILER DESIGN Subject Code:10CS63
Chapter 7 Regular Grammars
BOTTOM UP PARSING Lecture 16.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Design 7. Top-Down Table-Driven Parsing
Lecture 8: Top-Down Parsing
Bottom Up Parsing.
Midterm (Models of Computation, Fall, 2000)
LR Parsing. Parser Generators.
Bottom-Up Parsing “Shift-Reduce” Parsing
Key Answers for Homework #7
Kanat Bolazar February 16, 2010
Compiler Construction
Bottom-up parsing is also known as shift-reduce parsing
The Cocke-Kasami-Younger Algorithm
LR(k) grammars The Chinese University of Hong Kong Fall 2008
Chap. 3 BOTTOM-UP PARSING
Presentation transcript:

Key to Homework #8 1. For each of the following context-free grammars (a) and (b) below, construct an LL(k) parser with minimum k according to the following guide lines (i) and (ii). S  aA A  bS | bbb S  AB | aaab A  aAb | aaaaaaab B  bBa | bbbb (i) Choose a typical string w from the language of the grammar, and write the parsing profile (i.e., sequence of configurations, see the lecture note for an example) that your parser will take to parse w. Along this sequence of configurations, whenever your parser needs to look ahead, write a clear remark disclosing the minimum required look ahead length (i.e., the number of cells) and the reason why it needs to look ahead. (ii) Construct the parse table of your parser. (q0, ababbb, Z0)  (q1, ababbb, SZ0)  (q1, ababbb, aAZ0)  (q1, babbb, AZ0)  (q1, babbb, bSZ0)  (q1, abbb, SZ0)  (q1, abbb, aAZ0)  (q1, bbb, AZ0)  (q1, bbb, bbbZ0)  . . .  (q1, , Z0) // The LL(2) parsing is successful. We need 2 look ahead to choose one of the two rules for A. Since there are ba, the parser applies A  bS Here again we need 2 look ahead, and since it is bb, the parser applies rule A  bbb Answer: (a) S  aA A  bS | bbb (i) We choose w = ababbb, for which the parsing profile is (ii) Parse table: S A Stack top 2 look ahead ba bb XX aA bS bbb

Answer: (b) S  AB | aaab A  aAb | aaaaaaab B  bBa | bbbb (1) (2) (3) (4) (5) (6) (i) We choose w = aaaaaaaaabbbbbbbbbaa, for which the parsing profile is The parser needs 4 look ahead to make sure that next a is generated by rule (1) or (2). Since there is no b at the end, the parser applies rule (1). 1 (q0, aaaaaaaaabbbbbbbbbaa, Z0)  (q1, aaaaaaaaabbbbbbbbbaa, SZ0)  (q1, aaaaaaaaabbbbbbbbbaa, ABZ0)  The parser needs 8 look ahead to choose proper rule between rules (3) and (4). Since there is no b at the end, the parser applies rule (3). (q1, aaaaaaaaabbbbbbbbbaa, aAbBZ0)  (q1, aaaaaaaaabbbbbbbbbaa, AbBZ0)  (q1, aaaaaaaabbbbbbbbbaa, aAbbBZ0)  (q1, aaaaaaabbbbbbbbbaa, AbbBZ0)  (q1, aaaaaaabbbbbbbbbaa, aaaaaaabbbBZ0)  . . . . .  (q1, bbbbbbaa, BZ0)  The parser needs 8 look ahead to choose proper rule between rules (3) and (4). Since there is b at the end, the parser applies rule (4). It needs 5 look ahead to choose proper rule between rules (5) and (6). If 5 look ahead is bbbbb, apply rule (5), if it is bbbba, apply rule (6). (q1, bbbbbbaa, bBaZ0)  (q1, bbbbbaa, BaZ0)  (q1, bbbbbaa, bBaaZ0)  (q1, bbbbaa, BaaZ0)  (q1, bbbbaa, bbbbaaZ0)  . . . . (q1, , Z0)

Answer (b)-(ii): Parse table for grammar S  AB | aaab A  aAb | aaaaaaab B  bBa | bbbb Stack top 8 look ahead aaabxxxx aaaaxxxx aaaaaaaa aaaaaaab bbbbbxxx bbbbaxxx aaab AB aAb aaaaaaab bBa bbbb 2. (a) Why the grammar G below is not LL(k) grammar (i.e., it is impossible to construct an LL(k) parser, for any constant k)? (b) Construct an LL(k) grammar G’ that generates the same language as the grammar G. (c) Show the parse table of an LL(k) parser with minimum k for grammar G’. G: S  aAb | aBb A  aAb | c B  aBb | d Answer: (a) The language of grammar G is {aixbi | x  {c, d} }. Consider an LL(k) parser in the starting configuration with an input (q1, aa . . . . .acbb. . . .b, SZ0). The parser should know that S  aAb is the next rule applied for S. However, the c at the center of the input which is the only information available for the decision can be located arbitrarily far away from the first a. It is impossible for the parser to see the c (or d) by looking some constant k cells ahead. Hence, G is not LL(k) grammar for any constant k.

(b) Construct an LL(k) grammar G’ that generates the same language as the grammar G. Answer: G’: S  aSb | aAb A  c | d (c) Show the parse table of an LL(k) parser with minimum k for grammar G’. Answer: We examine how string aaacbbb can be parsed by an LL(k) parser. Clearly, the substring acb at the center of this string is generated by : S  aAb followed by A  c. All the other part of the string is generated by S  aSb. The parser will use this property of the language. (q0, aaacbbb, Z0)  (q1, aaacbbb, SZ0)  (q1, aaacbbb, aSbZ0)  (q1, aacbbb, SbZ0)  (q1, aacbbb, aSbbZ0)  (q1, acbbb, SbbZ0) (q1, acbbb, aAbbbZ0) (q1, cbbb, AbbbZ0) (q1, cbbb, cbbbZ0) . . . . (q1, , Z0) Since 2 look ahead is aa, apply S  aSb. 2 look ahead is ac. Apply S  aAb 1 look ahead is c. Apply rule A  c Based on the above analysis, we can construct the following parse table for an LL(2) parser. S A aa ac or ad cx dx aSb aAb c d Stack top 2 look ahead x: don’t care

3. For each of the following context-free grammars (a) and (b) below, construct an LR(k) parser with minimum k according to the guide lines (i) and (ii) above in problem 1. (a) S  ABC | BC | C A  aaa B  aa C  a (b) S  aSA | a A  aaaaab Answer: (a) S  ABC | BC | C A  aaa B  aa C  a (1) (2) (3) (4) (5) (6) We choose w = aaaaaa, which involves the most production rules, and examine how an LR(k) parser can parse this string. String w is derived according to the rightmost derivation as follows; S ABC ABa Aaaa aaaaaa. The parser will apply rules (4)(5)(6)(1) in this order which is the reverse order of the rightmost derivation. (q0, aaaaaa, Z0)  (q1, aaaaaa, Z0)  (q1, aaaaa, aZ0)  (q1, aaaa, aaZ0)  (q1, aaa, aaaZ0)  (q1, aaa, AZ0)   (q1, aa, aAZ0)  (q1, a, aaAZ0)  (q1, a, BAZ0)  (q1, , aBAZ0)  (q1, , CBAZ0)  (q1, , SZ0) Here it needs 1 look ahead to see if there is no more a’s such that rule (6) can be applicable. If 1 look ahead were blank, the parser should have applied rule (6). Here it needs 2 look ahead. Since it sees aa, the parser shifts the next a in onto the stack instead of applying rule (5). If there were only one a, rule (5) should’ve been applied.

(ii) Parse table. aa a ABC *Stack top portion 2 look ahead x : don’t care B : blank aaa aa aB Shift-in BB xx C A B S BC * Stack top portion is depth 3. The bottom of stack symbol Z0 is not include.

Answer: (b) S  aSA | a A  aaaaab (i): We choose w = aaaaaaaabaaaaab, which is derived as follows by the rightmost derivation; (1) (2) (3) S aSA aSaaaaab  aaSAaaaaab aaSaaaaabaaaaab aaaaaaaabaaaaab (1) (3) (1) (3) (2) Now we examine how this string can be parsed according to the LR(k) strategy applying the sequence of rules in the reverse order of the rightmost derivation. The first target to be brought up to the stack top and reduced is the a generated by rule (2). This a is next to the left of the first aaaaab appearing in the input. So, the parser shift in the input until it sees aaaaab. When the first a of the input is shifted in, if there remains no symbols in the input, rule (2) should be applied. (q0, aaaaaaaabaaaaab, Z0)  . . . . (q1, aaaaabaaaaab, aaaZ0)  (q1, aaaaabaaaaab, SaaZ0)  . . . .  (q1, aaaaab, baaaaaSaaZ0)  (q1, aaaaab, ASaaZ0)  (q1, aaaaab, SaZ0) . . . (q1, , baaaaaSaZ0)  (q1, , ASaZ0)  (q1, , SZ0) The parser looks aaaaab ahead, which implies that the a at the stack top should be reduced by applying rule (2). (ii) The reduction table: a aaaaab aSA 6 look ahead Stack top portion aaaaaa Shift-in S xxxxxx A BBBBBB

4. (a) Why the grammar G below is not LR(k) grammar (i. e 4. (a) Why the grammar G below is not LR(k) grammar (i.e., it is impossible to construct an LR(k) parser, for any constant k)? (b) Construct an LR(k) grammar G’ such that it generates the same language as the grammar G. (c) Show the parse table of an LR(k) parser with minimum k for grammar G’. * Notice that in the grammar capital letters (S, A, B) are nonterminals and the lower case letters (t, h, i, s, a, m , e) are terminals. G: S  Athis | Bthat A  Asame | same B  Bsame |same Answer: (a) For the convenience of argument lets transform the grammar as shown below with the following substitution. this = b that = c same = a S  Ab | Bc A  Aa | a B  Ba | a This grammar is the same grammar shown in slide number 172 of the lecture note. Refer to this slide for the argument that it is impossible to construct an LR(k) parser. (b) G’: S  sameS | sameA A  this | that (c) We can easily construct an LR(0) parser, which reads the input until this or that appear on the stack top portion, and begins reductions as the following example shows. Notice that the parser needs no look ahead information. (q0, samesamethis, Z0)  . . . . (q1, , sihtemasemasZ0)  (q1, , AemasemasZ0)  (q1, , SemasZ0)  (q1, , SemasZ0)  (q1, , SZ0)  this that sameS 0 look ahead A S Stack top portion x x: don’t care Parse table: sameA