Key to Homework #8. (a) S aa A bs bbb (b) S AB aaab A aab aaaaaaab B bba bbbb

Similar documents
Parsing. For a given CFG G, parsing a string w is to check if w L(G) and, if it is, to find a sequence of production rules which derive w.

CS453 : Shift Reduce Parsing Unambiguous Grammars LR(0) and SLR Parse Tables by Wim Bohm and Michelle Strout. CS453 Shift-reduce Parsing 1

Chapter 4: Regular Expressions

ITEC2620 Introduction to Data Structures

CHAPTER TWO LANGUAGES. Dr Zalmiyah Zakaria

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

Talen en Compilers. Johan Jeuring , period 2. January 17, Department of Information and Computing Sciences Utrecht University

Lecture 7: Deterministic Bottom-Up Parsing

Automata Theory TEST 1 Answers Max points: 156 Grade basis: 150 Median grade: 81%

Lecture 8: Deterministic Bottom-Up Parsing

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Glynda, the good witch of the North

Proof Techniques Alphabets, Strings, and Languages. Foundations of Computer Science Theory

BSCS Fall Mid Term Examination December 2012

LR(0) Parsing Summary. LR(0) Parsing Table. LR(0) Limitations. A Non-LR(0) Grammar. LR(0) Parsing Table CS412/CS413

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant

UNIT III & IV. Bottom up parsing

JNTUWORLD. Code No: R

Monday, September 13, Parsers

Finite Automata Part Three

Finite Automata Part Three

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

Chapter 18: Decidability

Downloaded from Page 1. LR Parsing

Strings, Languages, and Regular Expressions

Chapter 4. Lexical and Syntax Analysis

CSE431 Translation of Computer Languages

CS143 Handout 14 Summer 2011 July 6 th, LALR Parsing

Introduction to Bottom-Up Parsing

LR Parsing, Part 2. Constructing Parse Tables. An NFA Recognizing Viable Prefixes. Computing the Closure. GOTO Function and DFA States

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

Principles of Programming Languages

Recursive Descent Parsers

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

University of Technology Department of Computer Sciences. Final Examination st Term. Subject:Compilers Design

Error Recovery. Computer Science 320 Prof. David Walker - 1 -

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

Notes for Comp 454 Week 2

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

Intro to Bottom-up Parsing. Lecture 9

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

LR Parsing. Leftmost and Rightmost Derivations. Compiler Design CSE 504. Derivations for id + id: T id = id+id. 1 Shift-Reduce Parsing.

CS 314 Principles of Programming Languages. Lecture 3

Formal Languages and Compilers Lecture VI: Lexical Analysis

Top down vs. bottom up parsing

Assignment No.4 solution. Pumping Lemma Version I and II. Where m = n! (n-factorial) and n = 1, 2, 3

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

SLR parsers. LR(0) items

Lesson 12: Angles Associated with Parallel Lines

Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

LALR Parsing. What Yacc and most compilers employ.

UNIT-III BOTTOM-UP PARSING

Bottom-Up Parsing. Lecture 11-12

CS 4120 Introduction to Compilers

Wednesday, August 31, Parsers

LR Parsing. The first L means the input string is processed from left to right.

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

CMPSCI 250: Introduction to Computation. Lecture #28: Regular Expressions and Languages David Mix Barrington 2 April 2014

Parser. Larissa von Witte. 11. Januar Institut für Softwaretechnik und Programmiersprachen. L. v. Witte 11. Januar /23

4. Lexical and Syntax Analysis

LR Parsing E T + E T 1 T

General Overview of Compiler

4. Lexical and Syntax Analysis

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Table-Driven Parsing

CS308 Compiler Principles Syntax Analyzer Li Jiang

Bottom-Up Parsing. Lecture 11-12

CS 314 Principles of Programming Languages

CS 403 Compiler Construction Lecture 3 Lexical Analysis [Based on Chapter 1, 2, 3 of Aho2]

Finite Automata Part Three

Syntax-Directed Translation. Lecture 14

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

CS375: Logic and Theory of Computing

LL Parsing: A piece of cake after LR

Outline CS412/413. Administrivia. Review. Grammars. Left vs. Right Recursion. More tips forll(1) grammars Bottom-up parsing LR(0) parser construction

PDA s. and Formal Languages. Automata Theory CS 573. Outline of equivalence of PDA s and CFG s. (see Theorem 5.3)

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

Formal Languages. Grammar. Ryan Stansifer. Department of Computer Sciences Florida Institute of Technology Melbourne, Florida USA 32901

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

Lexical and Syntax Analysis. Bottom-Up Parsing

LL(1) predictive parsing

Parsing - 1. What is parsing? Shift-reduce parsing. Operator precedence parsing. Shift-reduce conflict Reduce-reduce conflict

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing

Outline. The strategy: shift-reduce parsing. Introduction to Bottom-Up Parsing. A key concept: handles

Plan for Today. Regular Expressions: repetition and choice. Syntax and Semantics. Context Free Grammars

Lec-5-HW-1, TM basics

Models of Computation II: Grammars and Pushdown Automata

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

Introduction to Parsing. Lecture 8

CS311 / MATH352 - AUTOMATA AND COMPLEXITY THEORY

Transcription:

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). (a) a b bbb (b) B aaab ab 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. long 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. nswer: (a) a b bbb (i) We choose w = ababbb, for which the parsing profile is We need 2 look ahead to choose one of the two rules for. ince there are ba, the parser applies b (q 0, ababbb, Z 0 ) (q 1, ababbb, Z 0 ) (q 1, ababbb, az 0 ) (q 1, babbb, Z 0 ) (q 1, babbb, bz 0 ) (q 1, abbb, Z 0 ) (q 1, abbb, az 0 ) (q 1, bbb, Z 0 ) (q 1, bbb, bbbz 0 )... (q 1, ε, Z 0 ) // The LL(2) parsing is successful. Here again we need 2 look ahead, and since it is bb, the parser applies rule bbb (ii) Parse table: 2 look ahead ba bb XX tack top b bbb a 1

(1) (2) (3) (4) (5) (6) nswer: (b) B aaab ab aaaaaaab B bba bbbb (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). ince there is no b at the end, the parser applies rule (1). 1 (q 0, aaaaaaaaabbbbbbbbbaa, Z 0 ) (q 1, aaaaaaaaabbbbbbbbbaa, Z 0 ) (q 1, aaaaaaaaabbbbbbbbbaa, BZ 0 ) The parser needs 8 look ahead to choose proper rule between rules (3) and (4). ince there is no b at the end, the parser applies rule (3). (q 1, aaaaaaaaabbbbbbbbbaa, abbz 0 ) (q 1, aaaaaaaaabbbbbbbbbaa, bbz 0 ) (q 1, aaaaaaaabbbbbbbbbaa, abbbz 0 ) The parser needs 8 look ahead to choose proper rule between rules (3) and (4). ince there is b at the end, the parser applies rule (4). (q 1, aaaaaaabbbbbbbbbaa, bbbz 0 ) (q 1, aaaaaaabbbbbbbbbaa, aaaaaaabbbbz 0 )..... (q 1, bbbbbbaa, BZ 0 ) 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). (q 1, bbbbbbaa, bbaz 0 ) (q 1, bbbbbaa, BaZ 0 ) (q 1, bbbbbaa, bbaaz 0 ) (q 1, bbbbaa, BaaZ 0 ) (q 1, bbbbaa, bbbbaaz 0 ).... (q 1, ε, Z 0 ) 2

nswer (b)-(ii): Parse table for grammar B aaab ab aaaaaaab B bba bbbb tack top 8 look ahead aaabxxxx aaaaxxxx aaaaaaaa aaaaaaab bbbbbxxx bbbbaxxx aaab B ab aaaaaaab B 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) how the parse table of an LL(k) parser with minimum k for grammar G. G: ab abb ab c B abb d nswer: (a) The language of grammar G is {a i xb i x {c, d} }. Consider an LL(k) parser in the starting configuration with an input (q 1, aa.....acbb....b, Z 0 ). The parser should know that ab is the next rule applied for. 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. 3

(b) Construct an LL(k) grammar G that generates the same language as the grammar G. nswer: G : ab ab c d (c) how the parse table of an LL(k) parser with minimum k for grammar G. nswer: 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 : ab followed by c. ll the other part of the string is generated by ab. The parser will use this property of the language. (q 0, aaacbbb, Z 0 ) (q 1, aaacbbb, Z 0 ) (q 1, aaacbbb, abz 0 ) (q 1, aacbbb, bz 0 ) (q 1, aacbbb, abbz 0 ) ince 2 look ahead is aa, apply ab. 2 look ahead is ac. pply ab 1 look ahead is c. pply rule c (q 1, acbbb, bbz 0 ) (q 1, acbbb, abbbz 0 ) (q 1, cbbb, bbbz 0 ) (q 1, cbbb, cbbbz 0 ).... (q 1, e, Z 0 ) Based on the above analysis, we can construct the following parse table for an LL(2) parser. 2 look ahead aa ac or ad cx dx tack top ab ab c d x: don t care 4

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) BC BC C aaa B aa C a (b) a a aaaaab (1) (2) (3) (4) (5) (6) nswer: (a) BC BC C aaa B aa C a We choose w = aaaaaa, which involves the most production rules, and examine how an LR(k) parser can parse this string. tring w is derived according to the rightmost derivation as follows; BC Ba aaa aaaaaa. The parser will apply rules (4)(5)(6)(1) in this order which is the reverse order of the rightmost derivation. 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. ince 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. (q 0, aaaaaa, Z 0 ) (q 1, aaaaaa, Z 0 ) (q 1, aaaaa, az 0 ) (q 1, aaaa, aaz 0 ) (q 1, aaa, aaaz 0 ) (q 1, aaa, Z 0 ) (q 1, aa, az 0 ) (q 1, a, aaz 0 ) (q 1, a, BZ 0 ) (q 1, ε, abz 0 ) (q 1, ε, CBZ 0 ) (q 1, ε, Z 0 ) 5

(ii) Parse table. 2 look ahead aa ab BB xx *tack top portion aaa aa a hift-in hift-in B C x : don t care B : blank BC BC C * tack top portion is depth 3. The bottom of stack symbol Z 0 is not include. 6

(1) (2) (3) nswer: (b) a a aaaaab (i): We choose w = aaaaaaaabaaaaab, which is derived as follows by the rightmost derivation; (1) (3) (1) (3) (2) a aaaaaab aaaaaaab aaaaaaabaaaaab aaaaaaaabaaaaab 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. o, 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. The parser looks aaaaab ahead, which implies that the a at the stack top should be reduced by applying rule (2). (q 0, aaaaaaaabaaaaab, Z 0 ).... (q 1, aaaaabaaaaab, aaaz 0 ) (q 1, aaaaabaaaaab, aaz 0 ).... (q 1, aaaaab, baaaaaaaz 0 ) (q 1, aaaaab, aaz 0 ) (q 1, aaaaab, az 0 )... (q 1, ε, baaaaaaz 0 ) (q 1, ε, az 0 ) (q 1, ε, Z 0 ) (ii) The reduction table: tack top portion a aaaaab 6 look ahead aaaaaa aaaaab hift-in xxxxxx BBBBBB a 7

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) how the parse table of an LR(k) parser with minimum k for grammar G. * Notice that in the grammar capital letters (,, B) are nonterminals and the lower case letters (t, h, i, s, a, m, e) are terminals. G: this Bthat same same B Bsame same nswer: (a) For the convenience of argument lets transform the grammar as shown below with the following substitution. this = b that = c same = a b Bc a 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 : same same 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. (q 0, samesamethis, Z 0 ).... (q 1, ε, sihtemasemasz 0 ) (q 1, ε, emasemasz 0 ) (q 1, ε, emasz 0 ) (q 1, ε, emasz 0 ) (q 1, ε, Z 0 ) Parse table: tack top portion this that same same 0 look ahead x x: don t care 8