Assignment 2. Solutions. Compiler Design I (Kompilatorteknik I) 2011

Similar documents
Homework 3 Solutions

Java CUP. Java CUP Specifications. User Code Additions You may define Java code to be included within the generated parser:

Binary Representation of Numbers Autar Kaw

Polynomial Functions. Polynomial functions in one variable can be written in expanded form as ( )

Reasoning to Solve Equations and Inequalities

Experiment 6: Friction

Example 27.1 Draw a Venn diagram to show the relationship between counting numbers, whole numbers, integers, and rational numbers.

Solutions for Selected Exercises from Introduction to Compiler Design

Mathematics. Vectors. hsn.uk.net. Higher. Contents. Vectors 128 HSN23100

A Visual and Interactive Input abb Automata. Theory Course with JFLAP 4.0

Regular Sets and Expressions

Concept Formation Using Graph Grammars

Algebra Review. How well do you remember your algebra?

Unambiguous Recognizable Two-dimensional Languages

Math 135 Circles and Completing the Square Examples

Graphs on Logarithmic and Semilogarithmic Paper

Use Geometry Expressions to create a more complex locus of points. Find evidence for equivalence using Geometry Expressions.

9.3. The Scalar Product. Introduction. Prerequisites. Learning Outcomes

NQF Level: 2 US No: 7480

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 16 th May Time: 14:00 16:00

Cypress Creek High School IB Physics SL/AP Physics B MP2 Test 1 Newton s Laws. Name: SOLUTIONS Date: Period:

flex Regular Expressions and Lexical Scanning Regular Expressions and flex Examples on Alphabet A = {a,b} (Standard) Regular Expressions on Alphabet A

PROF. BOYAN KOSTADINOV NEW YORK CITY COLLEGE OF TECHNOLOGY, CUNY

Lecture 5. Inner Product

Physics 43 Homework Set 9 Chapter 40 Key

Warm-up for Differential Calculus

Solution to Problem Set 1

Helicopter Theme and Variations

Vectors Recap of vectors

Quick Reference Guide: One-time Account Update

Lecture 3 Gaussian Probability Distribution

The Velocity Factor of an Insulated Two-Wire Transmission Line

LINEAR TRANSFORMATIONS AND THEIR REPRESENTING MATRICES

Review guide for the final exam in Math 233

Section 5-4 Trigonometric Functions

Econ 4721 Money and Banking Problem Set 2 Answer Key

Or more simply put, when adding or subtracting quantities, their uncertainties add.

Small Business Networking

Small Business Networking

Regular Languages and Finite Automata

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall Final Exam

Rotating DC Motors Part II

FUNCTIONS AND EQUATIONS. xεs. The simplest way to represent a set is by listing its members. We use the notation

1. In the Bohr model, compare the magnitudes of the electron s kinetic and potential energies in orbit. What does this imply?

One Minute To Learn Programming: Finite Automata

Protocol Analysis / Analysis of Software Artifacts Kevin Bierhoff

Small Business Networking

and thus, they are similar. If k = 3 then the Jordan form of both matrices is

LECTURE #05. Learning Objectives. How does atomic packing factor change with different atom types? How do you calculate the density of a material?

I calculate the unemployment rate as (In Labor Force Employed)/In Labor Force

Small Business Networking

How To Network A Smll Business

Virtual Machine. Part II: Program Control. Building a Modern Computer From First Principles.

FORMAL LANGUAGES, AUTOMATA AND THEORY OF COMPUTATION EXERCISES ON REGULAR LANGUAGES

A.7.1 Trigonometric interpretation of dot product A.7.2 Geometric interpretation of dot product

15.6. The mean value and the root-mean-square value of a function. Introduction. Prerequisites. Learning Outcomes. Learning Style

Decision Rule Extraction from Trained Neural Networks Using Rough Sets

Section 7-4 Translation of Axes

e.g. f(x) = x domain x 0 (cannot find the square root of negative values)

Chapter. Contents: A Constructing decimal numbers

UNLOCKING TECHNOLOGY IVECO

Integration by Substitution

What is the phase sequence of a balanced three-phase circuit for which V an = V and V cn = V? Find V bn.

Bayesian Updating with Continuous Priors Class 13, 18.05, Spring 2014 Jeremy Orloff and Jonathan Bloom

5 a LAN 6 a gateway 7 a modem

FAULT TREES AND RELIABILITY BLOCK DIAGRAMS. Harry G. Kwatny. Department of Mechanical Engineering & Mechanics Drexel University

How To Set Up A Network For Your Business

Words Symbols Diagram. abcde. a + b + c + d + e

Treatment Spring Late Summer Fall Mean = 1.33 Mean = 4.88 Mean = 3.

Advanced Baseline and Release Management. Ed Taekema

9 CONTINUOUS DISTRIBUTIONS

g(y(a), y(b)) = o, B a y(a)+b b y(b)=c, Boundary Value Problems Lecture Notes to Accompany

EQUATIONS OF LINES AND PLANES

Unit 6: Exponents and Radicals

, and the number of electrons is -19. e e C. The negatively charged electrons move in the direction opposite to the conventional current flow.

6.2 Volumes of Revolution: The Disk Method

Novel Methods of Generating Self-Invertible Matrix for Hill Cipher Algorithm

Exponential and Logarithmic Functions

Module 2. Analysis of Statically Indeterminate Structures by the Matrix Force Method. Version 2 CE IIT, Kharagpur

Answer, Key Homework 10 David McIntyre 1

trademark and symbol guidelines FOR CORPORATE STATIONARY APPLICATIONS reviewed

Welch Allyn CardioPerfect Workstation Installation Guide

Module Summary Sheets. C3, Methods for Advanced Mathematics (Version B reference to new book) Topic 2: Natural Logarithms and Exponentials

SE3BB4: Software Design III Concurrent System Design. Sample Solutions to Assignment 1

Test Management using Telelogic DOORS. Francisco López Telelogic DOORS Specialist

5.6 POSITIVE INTEGRAL EXPONENTS

Unit 29: Inference for Two-Way Tables

c. Values in statements are broken down by fiscal years; many projects are

Quick Reference Guide: Reset Password

Lesson 4.1 Triangle Sum Conjecture

GENERAL APPLICATION FOR FARM CLASSIFICATION

Operations with Polynomials

Generating In-Line Monitors For Rabin Automata

AntiSpyware Enterprise Module 8.5

FIRST and FOLLOW sets a necessary preliminary to constructing the LL(1) parsing table

MATH 150 HOMEWORK 4 SOLUTIONS

Understanding Basic Analog Ideal Op Amps

PHY 222 Lab 8 MOTION OF ELECTRONS IN ELECTRIC AND MAGNETIC FIELDS

DlNBVRGH + Sickness Absence Monitoring Report. Executive of the Council. Purpose of report

Lec 2: Gates and Logic

Transcription:

Assignment 2 olutions Compiler Design I Kompiltorteknik I) 2011 1 Context-free grmmrs Give the definition of context free grmmr over the lphbet Σ = {, b} tht describes ll strings tht hve different number of s nd b s U V U T U T T V T bv T bt T T bt bt T ϵ The intuition is tht the string will hve either more s non-terminl U) or more b s non-terminl V ) Non-terminl T produces string with blnced s nd b s 2 Prsing nd semntic ctions The following context-free grmmr cn prse ll the lowercse romn numerls from 1-99 The terminl symbols re { c, l, x, v, i } nd the initil symbol is If you re unfmilir with romn numerls, plese hve look t http://enwikipediorg/wiki/romn_numerls 1 Drw prse tree for 42: xlii xtu lx X T c l X xx U U iy vi I Y x v I ii ϵ x T l U i I i I I ϵ 1

2 Is this grmmr mbiguous? No 3 Write semntic ctions for ech of the 14 rules in the grmmr remember X A B is short for X A nd X B) to clculte the deciml vlue of the input string You cn ssocite synthesized ttribute vl to ech of the non-terminls to store their vlue The finl vlue should be returned in vl Given cvl = 100, lvl = 50, xvl = 10, vvl = 5, ivl = 1 nd ϵvl = 0: 3 LL1) Prsers xt U {vl = Tvl xvl + Uvl} lx {vl = lvl + Xvl} X {vl = Xvl} T c {Tvl = cvl} T l {Tvl = lvl} X 1 xx 2 {X 1 vl = xvl + X 2 vl} X U {Xvl = Uvl} U iy {Uvl = Yvl ivl} U vi {Uvl = vvl + Ivl} U I {Uvl = Ivl} Y x {Yvl = xvl} Y v {Yvl = vvl} I 1 ii 2 {I 1 vl = ivl + I 2 vl} I ϵ {Ivl = ϵvl} In the following context-free grmmr, the symbols 0, 1, 2 nd 3 re terminls nd is the initil symbol 0 1 2 3 1 A 3 A A 1 Explin briefly why this grmmr is not LL1) This grmmr cnnot be prsed by recursive descent prser This cn be shown by the following two exmples: If the prser hs to expnd n non-terminl nd the next token is 1, it is not possible to choose between the 2 productions from tht strt with 1 with just this informtion However LL1) lnguges llow for just one look-hed symbol If the prser were to mke use of the A A production, for some look-hed symbol, then in the new stte it would still hve to expnd the new A with the sme look-hed, leding to n infinite loop 2 Convert this grmmr to n equivlent tht is LL1) Fctorize the productions nd eliminte immedite left recursion from the A productions: 0 1 2 3 A 3 A A A A ϵ 2

Inline singulr A production rule to uncover nother possible fctoriztion: 0 1 2 3 A 3 A A ϵ Fctorize the production nd inline the new singulr it in s production: 0 1 2 3 A 3 A A ϵ 3 For the grmmr of the previous subtsk, construct the complete LL1) prsing tble First) = {0, 1} Follow) = {0, 1, 2, 3, $} First ) = {0, 1, 2, 3} Follow ) = {0, 1, 2, 3, $} FirstA ) = {0, 1, ϵ} FollowA ) = {3} 0 1 2 3 $ 0 1 A 3 A 3 2 3 A 3 A A A A A A ϵ 4 how ll the steps required to prse the input string: 1 1 0 2 0 3 0 1 0 3 3 tck Input Action $ 1 1 0 2 0 3 0 1 0 3 3 $ 1 1 $ 1 1 0 2 0 3 0 1 0 3 3 $ terminl $ 1 0 2 0 3 0 1 0 3 3 $ 1 1 $ 1 0 2 0 3 0 1 0 3 3 $ terminl $ 0 2 0 3 0 1 0 3 3 $ 0 0 $ 0 2 0 3 0 1 0 3 3 $ terminl $ 2 0 3 0 1 0 3 3 $ 2 3 2 3 $ 2 0 3 0 1 0 3 3 $ terminl 3 $ 0 3 0 1 0 3 3 $ 0 0 3 $ 0 3 0 1 0 3 3 $ terminl 3 $ 3 0 1 0 3 3 $ terminl $ 0 1 0 3 3 $ A 3 A 3 $ 0 1 0 3 3 $ A A A 3 $ 0 1 0 3 3 $ 0 0 A 3 $ 0 1 0 3 3 $ terminl A 3 $ 1 0 3 3 $ A A A 3 $ 1 0 3 3 $ 1 1 A 3 $ 1 0 3 3 $ terminl A 3 $ 0 3 3 $ 0 0 A 3 $ 0 3 3 $ terminl A 3 $ 3 3 $ A 3 A 3 A 3 $ 3 3 $ A ϵ 3 A 3 $ 3 3 $ terminl A 3 $ 3 $ A ϵ 3 $ 3 $ terminl $ $ ACCEPT 3

4 LR1) Prsers In the following context-free grmmr, the symbols,, ) nd, re terminls nd is the initil symbol 1) L ) 2) 3) L L, 4) L Becuse, is symbol of the lnguge we re going to use s seprtor between the core of the LR1) items nd the lookhed symbols Lookheds with the sme core cn be seprted s usul with / 1 Clculte the closure of the LR1) item [ L ) $ ] [ L ) $ ] [ L L, )/, ] [ L )/, ] [ L ) )/, ] [ )/, ] 2 Construct the full LR1) DFA, showing ll items in ech stte New unique initil production: 0) E [ L ) $ ] [ E $ ] [ L L, )/, ] strt 0 [ L ) $ ] 3 [ L )/, ] 5 [ L )/, ] [ $ ] [ L ) )/, ] [ )/, ] 1 [ E $ ] 2 [ $ ] 7 [ L ) $ ] [ L L, )/, ] ) 8 [ L ) $ ], L 11 6 [ )/, ] [ L L, )/, ] [ L ) )/, ] [ )/, ] 12 [ L L, )/, ], 4 [ L ) )/, ] [ L L, )/, ] [ L )/, ] [ L ) )/, ] [ )/, ] L 9 [ L ) )/, ] [ L L, )/, ] ) 10 [ L ) )/, ] 4

3 Construct the LR1) prsing tble using the DFA For the reduce ctions, plese use the provided enumertion of the productions in the grmmr tte ), $ L 0 s3 s2 1 1 ACCEPT 2 r2 3 s4 s6 5 7 4 s4 s6 5 9 5 r4 r4 6 r2 r2 7 s8 s11 8 r1 9 s10 s11 10 r1 r1 11 s4 s6 12 12 r3 r3 4 how ll the steps required to prse the input string:, ),, ) tck ymbols Input Action 0, ),, )$ shift 0,3, ),, )$ shift 0,3,4, ),, )$ shift 0,3,4,6, ),, )$ reduce 0,3,4,5, ),, )$ reduce 0,3,4,9 L, ),, )$ shift 0,3,4,9,11 L, ),, )$ shift 0,3,4,9,11,6 L, ),, )$ reduce 0,3,4,9,11,12 L, ),, )$ reduce 0,3,4,9 L ),, )$ shift 0,3,4,9,10 L ),, )$ reduce 0,3,5,, )$ reduce 0,3,7 L,, )$ shift 0,3,7,11 L,, )$ shift 0,3,7,11,6 L,, )$ reduce 0,3,7,11,12 L,, )$ reduce 0,3,7 L, )$ shift 0,3,7,11 L, )$ shift 0,3,7,11,6 L, )$ reduce 0,3,7,11,12 L, )$ reduce 0,3,7 L )$ shift 0,3,7,8 L ) $ reduce 0,1 $ ACCEPT! 5