Homework #2: CMPT-379 Distributed on Oct 2; due on Oct 16 Anoop Sarkar

Similar documents
CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 259] B-Trees

Programming Languages CRN Test 1 Version 1 CMSC 4023 Autumn 2013

Final Exam : Solutions

Lecture 1: Numerical Integration The Trapezoidal and Simpson s Rule

1 Recursive Descent (LL(1) grammars)

CSE 245: Computer Aided Circuit Simulation and Verification

CS 6353 Compiler Construction, Homework #1. 1. Write regular expressions for the following informally described languages:

Elementary Differential Equations and Boundary Value Problems

Ma/CS 6a Class 15: Flows and Bipartite Graphs

Let s look again at the first order linear differential equation we are attempting to solve, in its standard form:

3+<6,&6([DP. September 29, SID (last 5 digits): --

Chapter 5 The Laplace Transform. x(t) input y(t) output Dynamic System

UNIT #5 EXPONENTIAL AND LOGARITHMIC FUNCTIONS

Boyce/DiPrima 9 th ed, Ch 2.1: Linear Equations; Method of Integrating Factors

Spring 2006 Process Dynamics, Operations, and Control Lesson 2: Mathematics Review

Chapter 3: Fourier Representation of Signals and LTI Systems. Chih-Wei Liu

First derivative analysis

Midterm exam 2, April 7, 2009 (solutions)

Fourier Series and Parseval s Relation Çağatay Candan Dec. 22, 2013

4.1 The Uniform Distribution Def n: A c.r.v. X has a continuous uniform distribution on [a, b] when its pdf is = 1 a x b

2.1. Differential Equations and Solutions #3, 4, 17, 20, 24, 35

Transfer function and the Laplace transformation

u r du = ur+1 r + 1 du = ln u + C u sin u du = cos u + C cos u du = sin u + C sec u tan u du = sec u + C e u du = e u + C

Voltage v(z) ~ E(z)D. We can actually get to this wave behavior by using circuit theory, w/o going into details of the EM fields!

The transition:transversion rate ratio vs. the T-ratio.

Lecture 2: Current in RC circuit D.K.Pandey

Fitting Parameters in a Differential Equation With a Non-Analytical Solution

CS 361 Meeting 12 10/3/18

Abstract Interpretation: concrete and abstract semantics

I) Title: Rational Expectations and Adaptive Learning. II) Contents: Introduction to Adaptive Learning

Chap.3 Laplace Transform

Section 4.3 Logarithmic Functions

5. An object moving along an x-coordinate axis with its scale measured in meters has a velocity of 6t

Laplace Transform. National Chiao Tung University Chun-Jen Tsai 10/19/2011

Continous system: differential equations

Ali Karimpour Associate Professor Ferdowsi University of Mashhad. Reference: System Identification Theory For The User Lennart Ljung

Lecture 4: Parsing. Administrivia

Part I: Short Answer [50 points] For each of the following, give a short answer (2-3 sentences, or a formula). [5 points each]

Direct Approach for Discrete Systems One-Dimensional Elements

Source code. where each α ij is a terminal or nonterminal symbol. We say that. α 1 α m 1 Bα m+1 α n α 1 α m 1 β 1 β p α m+1 α n

Homework: Introduction to Motion

3.4 Repeated Roots; Reduction of Order

On the Derivatives of Bessel and Modified Bessel Functions with Respect to the Order and the Argument

Lecture 4: Laplace Transforms

Basic Logic Review. Rules. Lecture Roadmap Combinational Logic. Textbook References. Basic Logic Gates (2-input versions)

UNTYPED LAMBDA CALCULUS (II)

ECE 650 1/8. Homework Set 4 - Solutions

Aim To manage files and directories using Linux commands. 1. file Examines the type of the given file or directory

EEO 401 Digital Signal Processing Prof. Mark Fowler

Chapter 12 Introduction To The Laplace Transform

Mixing time with Coupling

ECE602 Exam 1 April 5, You must show ALL of your work for full credit.

Chapter 2 The Derivative Business Calculus 99

AR(1) Process. The first-order autoregressive process, AR(1) is. where e t is WN(0, σ 2 )

Homework #3. 1 x. dx. It therefore follows that a sum of the

Engine Thrust. From momentum conservation

Copyright 2012 Pearson Education, Inc. Publishing as Prentice Hall.

Circuits and Systems I

Consider a system of 2 simultaneous first order linear equations

Dealing with quantitative data and problem solving life is a story problem! Attacking Quantitative Problems

Math 34A. Final Review

EXERCISE - 01 CHECK YOUR GRASP

SOLUTIONS. 1. Consider two continuous random variables X and Y with joint p.d.f. f ( x, y ) = = = 15. Stepanov Dalpiaz

Propositional Logic. Combinatorial Problem Solving (CPS) Albert Oliveras Enric Rodríguez-Carbonell. May 17, 2018

FIRST-ORDER SYSTEMS OF ORDINARY DIFFERENTIAL EQUATIONS I: Introduction and Linear Systems

Longest Common Prefixes

MCB137: Physical Biology of the Cell Spring 2017 Homework 6: Ligand binding and the MWC model of allostery (Due 3/23/17)

The Variance-Covariance Matrix

Integrity Control in Nested Certificates

Inverse Fourier Transform. Properties of Continuous time Fourier Transform. Review. Linearity. Reading Assignment Oppenheim Sec pp.289.

H is equal to the surface current J S

MA 262, Spring 2018, Final exam Version 01 (Green)

Logistic equation of Human population growth (generalization to the case of reactive environment).

= x. I (x,y ) Example: Translation. Operations depend on pixel s Coordinates. Context free. Independent of pixel values. I(x,y) Forward mapping:

1 Finite Automata and Regular Expressions

The Procedure Abstraction Part II: Symbol Tables and Activation Records

SECTION where P (cos θ, sin θ) and Q(cos θ, sin θ) are polynomials in cos θ and sin θ, provided Q is never equal to zero.

Introduction to Arithmetic Geometry Fall 2013 Lecture #20 11/14/2013

That is, we start with a general matrix: And end with a simpler matrix:

CPS 616 W2017 MIDTERM SOLUTIONS 1

DEPARTMENT OF ELECTRICAL &ELECTRONICS ENGINEERING SIGNALS AND SYSTEMS. Assoc. Prof. Dr. Burak Kelleci. Spring 2018

Quasi-Classical States of the Simple Harmonic Oscillator

Alpha and beta decay equation practice

Section 11.6: Directional Derivatives and the Gradient Vector

Midterm Examination (100 pts)

Note If the candidate believes that e x = 0 solves to x = 0 or gives an extra solution of x = 0, then withhold the final accuracy mark.

Boyce/DiPrima 9 th ed, Ch 7.8: Repeated Eigenvalues

LR(0) Analysis. LR(0) Analysis

Higher order derivatives

An Indian Journal FULL PAPER. Trade Science Inc. A stage-structured model of a single-species with density-dependent and birth pulses ABSTRACT

Section 6.1. Question: 2. Let H be a subgroup of a group G. Then H operates on G by left multiplication. Describe the orbits for this operation.

The Equitable Dominating Graph

cycle that does not cross any edges (including its own), then it has at least

Wave Equation (2 Week)

4.3 Design of Sections for Flexure (Part II)

Roadmap. XML Indexing. DataGuide example. DataGuides. Strong DataGuides. Multiple DataGuides for same data. CPS Topics in Database Systems

First Lecture of Machine Learning. Hung-yi Lee

From Elimination to Belief Propagation

This test is for two independent Populations. The test is sometimes called the Mann-Whitney U test or the Rank Sum Wilcoxon. They are equivalent.

1 Minimum Cut Problem

Transcription:

Homwork #2: CMPT-379 Disribud on Oc 2 du on Oc 16 Anoop Sarkar anoop@cs.su.ca Rading or his homwork includs Chp 4 o h Dragon book. I ndd, rr o: hp://ldp.org/howto/lx-yacc-howto.hml Only submi answrs or qusions markd wih. Provid a makil such ha mak compils all your programs, and mak s runs ach program, and supplis h ncssary inpu ils. (1) Th ollowing program is yacc cod or a vry simpl (and incompl) xprssion inrprr. #includ <sdio.h> %okn NAME NUMBER samn: NAME = xprssion { prin("%c = %d\n", $1, $3) } xprssion { prin("%d\n", $1) } xprssion: xprssion + NUMBER { $$ = $1 + $3 } xprssion - NUMBER { $$ = $1 - $3 } NUMBER { $$ = $1 } Th... scion conains arbirary C/C++ cod and h %okn diniions is a lis o okns providd by h lxical analyzr. bison can b usd o convr his parsr diniion ino a parsr implmnaion by using h ollowing command: bison -osimpl-xpr.ab.c -d simpl-xpr.y Th -d opion producs a hadr il calld simpl-xpr.ab.h o convy inormaion abou h okns o h lxical analyzr. Examin h conns o his il. Th lxical analyzr can b dind in lx cod as ollows: #includ "simpl-xpr.ab.h" #includ <sdlib.h> xrn in yylval [0-9]+ { yylval = aoi(yyx) rurn NUMBER } [a-z] { yylval = yyx[0] rurn NAME } [ \\n]. rurn yyx[0] Th lx il can b compild o a C program using lx: lx -oxpr-wih-vars.lx.c xpr-wih-vars.lx Th inal inrprr binary is crad by compiling h oupu rom lx and bison wih a C/C++ compilr: gcc -o./simpl-xpr simpl-xpr.ab.c simpl-xpr.lx.c -ly -ll cho "a=2+3+5"./simpl-xpr 1

Convr h abov yacc and lx cod so ha i can handl mulipl xprssions, xacly on pr lin. For xampl, i should prin ou a valu or ach lin in h ollowing inpu: a = 10 b = 5 + 10-5 2 + 5 2 + 3 + 5 You will nd a rcursiv rul in ordr o handl mulipl lins o inpu. Try dirn ways o wriing his rcursiv rul. (2) Th yacc and lx cod in Q. (1) dos no y handl assignmns o variabls. In ordr o implmn his, w nd wo dirn kinds o valus o b rurnd rom h lxical analyzr: on or numbrs, and anohr or variabl nams. Th ollowing ragmn o lx cod now rurns wo dirn yps o valus o yacc: or numbrs i rurns yylval.rvalu and or variabl nams i rurns yylval.lvalu.... /* rmovd cod common wih prvious lx cod */ /* convr NUMBER okn valu o ingr */ [0-9]+ { yylval.rvalu = aoi(yyx) rurn NUMBER } /* convr NAME okn ino indx */ [a-z] { yylval.lvalu = yyx[0] - a rurn NAME }... /* rmovd cod common wih prvious lx cod */ Th wo yps o rurn valu, rvalu and lvalu ar dind in h yacc cod using h %union dclaraion, as shown in h ragmn blow: in symbl[26] %union { in rvalu /* valu o valuad xprssion */ in lvalu /* indx ino symbl or variabl nam */ } %okn <rvalu> NUMBER %okn <lvalu> NAME %yp <rvalu> xprssion samn: NAME = xprssion { symbl[$1] = $3 } xprssion { prin("%d\n", $1) } xprssion: xprssion + NUMBER { $$ = $1 + $3 } xprssion - NUMBER { $$ = $1 - $3 } NUMBER { $$ = $1 } 2

Th %union dclaraion can includ complx daayps. Th yacc cod dins a yp no jus or h okns, bu also or nonrminals, which is spciid in h %yp diniion abov. This allows yacc o chck ha h yp o h non-rminal xprssion is rvalu, an ingr yp. Us h abov cod ragmns, and add h ncssary lx and yacc cod in ordr o handl h ollowing inpu and oupu: Inpu: Oupu: a = 2 + 3 a b = a + a - 2 + 3 + a b 5 16 Exnd your xprssion inrprr o includ consans o yp doubl, and variabls ha can hold ihr ingr or doubl yps. Finally, add h uncions: xp, sqr, log so ha you can inrpr h inpu: a = 2.0 b = xp(a) b (3) Wri a Dca program ha implmns h quicksor algorihm o sor a lis. Your program should prin h sord lis by iraivly calling h prin in library uncion. Submi h program as h il quicksor.dca (4) Wri down a conx-r grammar or h srucur o Dca programs basd on h rrnc grammar in h Dca languag diniion (mak sur ha h non-rminal and rminal symbols usd in h CFG corrspond as much as possibl o h symbols usd in h rrnc grammar). Submi a il calld dcagrammar.x which conains h CFG in h ollowing x orma: For h CFG: sar A sar B ɛ h x orma you should us is: sar A sar B sar Follow h convnion ha h non-rminals in his x orma ar wrin in h sam orma as idniirs in Dca bu ar in lowrcas (.g. sar, and or hyphnad non-rminals lik mhod-nam rplac h hyphn wih an undrscor,.g. mhod nam) and wri h rminal symbols in h sam orma as idniirs bu nirly in upprcas (.g. A). You should vriy h corrcnss o your CFG ihr by xamining i closly or you can vriy aspcs o h CFG by wriing som simpl cod or chcking whhr non-rminals ar usd in h righ-hand sid o ruls bu no dind on h l-hand sid anywhr ls in h CFG, whhr h rminal symbols ar valid okns, c. (5) Wri down a yacc parsr or h ollowing conx-r grammar: PLUS TIMES LPAREN RPAREN Th okns PLUS, TIMES, LPAREN, RPAREN ar dind o b +, *, (, ) rspcivly. And h okn is dind o b an idniir as in h Dca spciicaion. Ths okns should b dind using a lxical analyzr producd using lx. 3

For h inpu sring x + y * ( z ) h oupu producd by h yacc parsr should b h pars r or h inpu sring producd in h orma shown in h l column blow. No h backslash prcding ach insanc o a liral parnhsis o avoid conusion wih h parnhss usd o dno h r srucur. No ha you may nd o augmn h grammar o produc h righ oupu. Do no bohr o indn your r, jus prin ou your pars r in a singl lin o oupu x. A graphical viw, using h Tcl/Tk program viwr, is shown in h righ column blow: ( ( ( ( ( x)))) (PLUS +) ( ( ( ( y))) (TIMES *) ( (LPAREN \() ( ( ( ( z)))) (RPAREN \))))) PLUS + TIMES * LPAREN RPAREN \( \) x y z (6) Grammar Convrsion: Considr h ollowing ragmn o a Dca program: class oo { in bar No ha w could coninu h abov ragmn wih a ild dclaraion, or a mhod dclaraion. This issu will no b a problm or a LR parsr i h CFG or Dca can b wrin as an LR grammar. Convr h ollowing CFG, which rprsns a small ragmn o Dca synax, ino an LR grammar. As a rsul, h LR parsing abl or such a grammar will hav no shi/rduc or rduc/rduc conlics. program CLASS LCB ild dcl lis mhod dcl lis RCB ild dcl lis ild dcl ild dcl lis ild dcl lis ɛ mhod dcl lis mhod dcl mhod dcl lis mhod dcl lis ɛ ild dcl yp ASSIGN INTCONSTANT SEMICOLON mhod dcl rurn yp LPAREN RPAREN rurn yp yp rurn yp VO yp INT yp BOOL 4

You can s i your rvisd grammar is an LR grammar by wriing down h grammar as yacc cod wih no acions and simply chck i you g any shi/rduc conlics. No ha in his sklon yacc cod, h okn diniions do no nd o b linkd wih a lxical analyzr in ordr o chck or shi/rduc conlics. (7) Dca Pars Trs: Wri a yacc program ha prins ou a pars r or any inpu Dca program. You will nd o mak sur ha h conx-r grammar you us is an LR grammar. Th pars r orma should b h sam as in Q. (5). Us h lxical analyzr you hav alrady buil basd on h Dca spciicaion. 5