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



Similar documents
Homework 3 Solutions

Regular Sets and Expressions

Protocol Analysis / Analysis of Software Artifacts Kevin Bierhoff

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

Section 5-4 Trigonometric Functions

AntiSpyware Enterprise Module 8.5

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

One Minute To Learn Programming: Finite Automata

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

Concept Formation Using Graph Grammars

Solutions for Selected Exercises from Introduction to Compiler Design

Reasoning to Solve Equations and Inequalities

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

Algebra Review. How well do you remember your algebra?

On decidability of LTL model checking for process rewrite systems

Integration by Substitution

Object Semantics Lecture 2

0.1 Basic Set Theory and Interval Notation

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

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

Lecture 3 Gaussian Probability Distribution

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

Engineer-to-Engineer Note

Helicopter Theme and Variations

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

Engineer-to-Engineer Note

IaaS Configuration for Virtual Platforms

Network Configuration Independence Mechanism

Hillsborough Township Public Schools Mathematics Department Computer Programming 1

Scanning and parsing. Topics. Announcements Pick a partner by Monday Makeup lecture will be on Monday August 29th at 3pm

Engineer-to-Engineer Note

Pentominoes. Pentominoes. Bruce Baguley Cascade Math Systems, LLC. The pentominoes are a simple-looking set of objects through which some powerful

Appendix D: Completing the Square and the Quadratic Formula. In Appendix A, two special cases of expanding brackets were considered:

5 a LAN 6 a gateway 7 a modem

STRM Log Manager Installation Guide

9 CONTINUOUS DISTRIBUTIONS

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

Automated Grading of DFA Constructions

trademark and symbol guidelines FOR CORPORATE STATIONARY APPLICATIONS reviewed

Regular Languages and Finite Automata

CS99S Laboratory 2 Preparation Copyright W. J. Dally 2001 October 1, 2001

Lecture 5. Inner Product

Math 135 Circles and Completing the Square Examples

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

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

LINEAR TRANSFORMATIONS AND THEIR REPRESENTING MATRICES

Pre-Approval Application

Lec 2: Gates and Logic

Small Business Networking

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

How fast can we sort? Sorting. Decision-tree model. Decision-tree for insertion sort Sort a 1, a 2, a 3. CS Spring 2009

Lifestyles. 1 Warm-up. 2 Conversation. Talk about the pictures with a partner. Who are these people? Where are they? Listen and read.

Small Business Networking

Small Business Networking

Outline of the Lecture. Software Testing. Unit & Integration Testing. Components. Lecture Notes 3 (of 4)

How To Network A Smll Business

Small Business Networking

EQUATIONS OF LINES AND PLANES

Vector differentiation. Chapters 6, 7

Factoring Polynomials

Firm Objectives. The Theory of the Firm II. Cost Minimization Mathematical Approach. First order conditions. Cost Minimization Graphical Approach

Vectors Recap of vectors

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

1.2 The Integers and Rational Numbers

Answer, Key Homework 10 David McIntyre 1

Enterprise Risk Management Software Buyer s Guide

Euler Euler Everywhere Using the Euler-Lagrange Equation to Solve Calculus of Variation Problems

Unit 6: Exponents and Radicals

Review guide for the final exam in Math 233

body.allow-sidebar OR.no-sidebar.home-page (if this is the home page).has-custom-banner OR.nocustom-banner .IR OR.no-IR

Unambiguous Recognizable Two-dimensional Languages

Operations with Polynomials

MATH 150 HOMEWORK 4 SOLUTIONS

Small Businesses Decisions to Offer Health Insurance to Employees

Warm-up for Differential Calculus

VMware Horizon Mirage Web Manager Guide

Tool Support for Feature-Oriented Software Development

How To Set Up A Network For Your Business

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

Vectors and dyadics. Chapter 2. Summary. 2.1 Examples of scalars, vectors, and dyadics

Basic Research in Computer Science BRICS RS Brodal et al.: Solving the String Statistics Problem in Time O(n log n)

Graphs on Logarithmic and Semilogarithmic Paper

Vectors and dyadics. Chapter 2. Summary. 2.1 Examples of scalars, vectors, and dyadics

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

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

Complexity Results in Epistemic Planning

2 DIODE CLIPPING and CLAMPING CIRCUITS

Babylonian Method of Computing the Square Root: Justifications Based on Fuzzy Techniques and on Computational Complexity

Quick Guide to Lisp Implementation

Transcription:

Jv CUP Jv CUP is prser-genertion tool, similr to Ycc. CUP uilds Jv prser for LALR(1) grmmrs from production rules nd ssocited Jv code frgments. When prticulr production is recognized, its ssocited code frgment is executed (typiclly to uild n AST). CUP genertes Jv source file prser.jv. It contins clss prser, with method Symol prse() The Symol returned y the prser is ssocited with the grmmr s strt symol nd contins the AST for the whole source progrm. The file sym.jv is lso uilt for use with JLex-uilt scnner (so tht oth scnner nd prser use the sme token codes). If n unrecovered syntx error occurs, Exception() is thrown y the prser. CUP nd Ycc ccept exctly the sme clss of grmmrs ll LL(1) grmmrs, plus mny useful non- LL(1) grmmrs. CUP is clled s jv jv_cup.min < file.cup 220 221 Jv CUP Specifictions User Code Additions You my define Jv code to e included within the generted prser: ction code {: /*jv code */ This code is plced within the generted ction clss (which holds user-specified production ctions). prser code {: /*jv code */ This code is plced within the generted prser clss. init with{: /*jv code */ This code is used to initilize the generted prser. scn with{: /*jv code */ This code is used to tell the generted prser how to get tokens from the scnner. Jv CUP specifictions re of the form: Pckge nd import specifictions User code dditions Terminl nd non-terminl declrtions A context-free grmmr, ugmented with Jv code frgments Pckge nd Import Specifictions You define pckge nme s: pckge nme You dd imports to e used s: import jv_cup.runtime.* 222 223

Terminl nd Non-terminl Declrtions You define terminl symols you will use s: terminl clssnme nme 1, nme 2,... clssnme is clss used y the scnner for tokens (CSXToken, CSXIdentifierToken, etc.) You define non-terminl symols you will use s: non terminl clssnme nme 1, nme 2,... clssnme is the clss for the AST node ssocited with the non-terminl (stmtnode, exprnode, etc.) Production Rules Production rules re of the form nme ::= nme 1 nme 2... ction or nme ::= nme 1 nme 2... ction 1 nme 3 nme 4... ction 2... Nmes re the nmes of terminls or non-terminls, s declred erlier. Actions re Jv code frgments, of the form {: /*jv code */ The Jv oject ssocted with symol ( token or AST node) my e nmed y dding :id suffix to terminl or non-terminl in rule. 224 225 RESULT nmes the left-hnd side non-terminl. The Jv clsses of the symols re defined in the terminl nd non-terminl declrtion sections. For exmple, prog ::= LBRACE:l stmts:s RBRACE {: RESULT = new csxlitenode(s, l.linenum,l.colnum) This corresponds to the production prog { stmts } The left rce is nmed l the stmts non-terminl is clled s. In the ction code, new CSXLiteNode is creted nd ssigned to prog. It is constructed from the AST node ssocited with s. Its line nd column numers re those given to the left rce, l (y the scnner). To tell CUP wht non-terminl to use s the strt symol (prog in our exmple), we use the directive: strt with prog 226 227

Exmple Let s look t the CUP specifiction for CSX-lite. Recll its CFG is progrm { stmts } stmts stmt stmts λ stmt id = expr if ( expr ) stmt expr expr + id expr - id id The corresponding CUP specifiction is: /*** This Is A Jv CUP Specifiction For CSX-lite, Smll Suset of The CSX Lnguge, Used In Cs536 ***/ /* Preliminries to set up nd use the scnner. */ import jv_cup.runtime.* prser code {: pulic void syntx_error (Symol cur_token){ report_error( CSX syntx error t line + String.vlueOf(((CSXToken) cur_token.vlue).linenum), null)} init with {: scn with {: return Scnner.next_token() 228 229 /* Terminls (tokens returned y the scnner). */ terminl CSXIdentifierToken IDENTIFIER terminl CSXToken SEMI, LPAREN, RPAREN, ASG, LBRACE, RBRACE terminl CSXToken PLUS, MINUS, rw_if /* Non terminls */ non terminl csxlitenode prog non terminl stmtsnode stmts non terminl stmtnode stmt non terminl exprnode exp non terminl ident strt with prog prog::= LBRACE:l stmts:s RBRACE new csxlitenode(s, l.linenum,l.colnum) stmts::= stmt:s1 stmts:s2 new stmtsnode(s1,s2, s1.linenum,s1.colnum) stmtsnode.null stmt::= ident:id ASG exp:e SEMI new sgnode(id,e, id.linenum,id.colnum) rw_if:i LPAREN exp:e RPAREN stmt:s new ifthennode(e,s, stmtnode.null, i.linenum,i.colnum) exp::= exp:leftvl PLUS:op ident:rightvl new inryopnode(leftvl, sym.plus, rightvl, op.linenum,op.colnum) exp:leftvl MINUS:op ident:rightvl new inryopnode(leftvl, sym.minus,rightvl, op.linenum,op.colnum) ident:i {: RESULT = i 230 231

ident::= IDENTIFIER:i {: RESULT = new ( new (i.identifiertext, i.linenum,i.colnum), exprnode.null, i.linenum,i.colnum) Let s prse { = } First, is prsed using ident::= IDENTIFIER:i {: RESULT = new ( new (i.identifiertext, i.linenum,i.colnum), exprnode.null, i.linenum,i.colnum) We uild 232 233 Next, is prsed using ident::= IDENTIFIER:i {: RESULT = new ( new (i.identifiertext, i.linenum,i.colnum), exprnode.null, i.linenum,i.colnum) We uild Then s sutree is recognized s n exp: ident:i {: RESULT = i Now the ssignment sttement is recognized: stmt::= ident:id ASG exp:e SEMI new sgnode(id,e, id.linenum,id.colnum) We uild sgnode 234 235

The stmts λ production is mtched (indicting tht there re no more sttements in the progrm). CUP mtches stmts::= stmtsnode.null nd we uild This uilds stmtsnode sgnode nullstmtsnode nullstmtsnode Next, stmts stmt stmts is mtched using stmts::= stmt:s1 stmts:s2 new stmtsnode(s1,s2, s1.linenum,s1.colnum) As the lst step of the prse, the prser mtches progrm { stmts } using the CUP rule prog::= LBRACE:l stmts:s RBRACE new csxlitenode(s, l.linenum,l.colnum) 236 237 The finl AST reurned y the prser is csxlitenode stmtsnode sgnode nullstmtsnode Errors in Context-Free Grmmrs Context-free grmmrs cn contin errors, just s progrms do. Some errors re esy to detect nd fix others re more sutle. In context-free grmmrs we strt with the strt symol, nd pply productions until terminl string is produced. Some context-free grmmrs my contin useless non-terminls. Non-terminls tht re unrechle (from the strt symol) or tht derive no terminl string re considered useless. Useless non-terminls (nd productions tht involve them) cn e sfely removed from 238 239

grmmr without chnging the lnguge defined y the grmmr. A grmmr contining useless non-terminls is sid to e nonreduced. After useless non-terminls re removed, the grmmr is reduced. Consider S A B x B A A C d Which non-terminls re unrechle? Which derive no terminl string? Finding Useless Non-terminls To find non-terminls tht cn derive one or more terminl strings, we ll use mrking lgorithm. We itertively mrk terminls tht cn derive string of terminls, until no more non-terminls cn e mrked. Unmrked nonterminls re useless. (1) Mrk ll terminl symols (2) Repet If ll symols on the righthnd side of production re mrked Then mrk the lefthnd side Until no more non-terminls cn e mrked 240 241 We cn use similr mrking lgorithm to determine which non-terminls cn e reched from the strt symol: (1) Mrk the Strt Symol (2) Repet If the lefthnd side of production is mrked Then mrk ll non-terminls in the righthnd side Until no more non-terminls cn e mrked 242