Code Listing for Problem 4

Size: px
Start display at page:

Download "Code Listing for Problem 4"

Transcription

1 Code Listing for Problem 4 Phillip Cannata, arranged by Jonathan Bernard test.c 1 float x; 2 3 int main () { 4 int x; 5 x = 4 * 2 * * ; 6 } hmm.bat -cp bin proj. Hmm %* Hmm.java 1 package proj; 2 3 import java. io. FileInputStream ; 4 import java. io. IOException ; 5 import java. io. InputStream ; 6 7 import proj. AbstractSyntax.*; 8 import proj. parser. Parser; 9 import proj. parser. ParseException ; public class Hmm { public static void main( String args []) throws ParseException, IOException, InterpreterRuntimeError { 14 // If there is a command line argument, interpret it as a stdin. 15 InputStream instream = System. in; 16 if (args.length > 0 ) { 17 instream = new FileInputStream ( args [0]); 18 } Parser parser = new Parser( instream); 21 Program prog = parser. Program (); prog.display (); 1

2 24 25 } } Parser.jj 1 options { 2 STATIC = false ; 3 } 4 PARSER_BEGIN ( Parser) 5 package proj. parser; 6 7 import java.io.*; 8 import java.util.*; 9 import static proj. AbstractSyntax.*; 10 import proj. AbstractSyntax ; public class Parser { private Type curtopleveltype ; 15 private Token curtopleveltoken ; 16 } PARSER_END ( Parser) SKIP : 21 { 22 " " 23 "\t" 24 "\n" 25 "\r" 26 <"//" (~["\n","\r"])* ("\n" "\r")> 27 } TOKEN: 30 { 31 < IF: "if" > 32 < ELSE: " else" > 33 < WHILE: " while" > < INT: "int" > 36 < FLOAT: " float" > 37 < BOOL: " bool" > 38 < VOID: " void" > < TRUE: " true" > 41 < FALSE: " false" > < AND_OP: "&&" > 44 < OR_OP: " " > 45 < LPAREN: "(" > 46 < RPAREN: ")" > 47 < LBRACE: "[" > 48 < RBRACE: "]" > 49 < LCURLY: "{" > 50 < RCURLY: "}" > 2

3 51 < SEMI: ";" > 52 < EQ_OP: "==" > 53 < NE_OP: "!=" > 54 < LE_OP: " <=" > 55 < GE_OP: " >=" > 56 < ELEM: "<-" > 57 < PIPE: " " > 58 < EQUALS: "=" > 59 < LT_OP: "<" > 60 < GT_OP: ">" > 61 < MINUS: "-" > 62 < PLUS: "+" > 63 < MULT: "*" > 64 < DIV: "/" > 65 < PRCNT: "%" > 66 < BANG: "!" > 67 < COMMA: "," > 68 < SQUOTE: " " > 69 < DOT: "." > } TOKEN: /* Literals */ 74 { 75 < INTEGER: (["0"-"9"])+ > 76 < IDENTIFIER : [ "A"-"Z", "a"-"z", "_"] // Maybe we could add the "_" check by the grammar ( [ "A"-"Z", "a"-"z", "0"-"9", "_" ] )* 78 > 79 < CLFLOAT: (["0"-"9"])+ "." (["0"-"9"])* > 80 } TOKEN: 83 { 84 <ERROR: ~[] > 85 } Program Program () : 88 { List < Declaration > globals = new ArrayList < Declaration >(); 89 List < Declaration > declist = null; 90 List < Function > funclist = new ArrayList < Function >(); 91 List < Statement > stmntlist ; 92 Function f; Token t; 93 } 94 { ( LOOKAHEAD (3) declist = declaration () <SEMI > { globals. addall( declist ); } )* curtopleveltype = rettype () curtopleveltoken = <IDENTIFIER > f = function () 97 { funclist. add(f); return new Program( globals, funclist); } 98 } Function function () : 101 { Block b; List < Declaration > args = new ArrayList < Declaration >(); } 102 { 103 <LPAREN > <RPAREN > b = block () 104 { 3

4 105 return new Function ( curtopleveltype, curtopleveltoken, args, b); 106 } 107 } List < Declaration > declaration () : 112 { Type t; Declaration d; List < Declaration > result = new ArrayList < Declaration >() ;} 113 { 114 t = vartype () 115 d = singlevardeclaration (t) { result. add(d); } 116 ( <COMMA > d = singlevardeclaration (t) { result. add(d); } )* 117 { 118 return result; 119 } 120 } Declaration singlevardeclaration ( Type type) : 123 { Token id; Expression initvalue = null;} 124 { 125 id = <IDENTIFIER > [ <EQUALS > initvalue = expression () ] 126 { 127 return new Declaration ( type, id, initvalue ); 128 } 129 } /** 133 * The return type is either a Variable type or void 134 */ 135 Type rettype () : 136 { Type t;} 137 { 138 <VOID > { return BaseType. VOID; } 139 t = vartype () { return t; } 140 } /** 143 * A type that can be assigned to a variable. 144 */ 145 Type vartype () : 146 { Type t; List <Type > argtypes = new ArrayList <Type >() ;} 147 { 148 t = varbasetype () { return t; } 149 <LPAREN > ( LOOKAHEAD (2) t = vartype () <COMMA > { argtypes. add(t);} )* t = rettype () <RPAREN > 150 { return new FunctionType ( argtypes, t); } 151 } /** 154 * A Base Type, except for the void 155 */ 156 Type varbasetype () : 157 {} 158 { 159 <INT > { return BaseType. INT; } 4

5 160 <FLOAT > { return BaseType. FLOAT; } 161 <BOOL > { return BaseType. BOOL; } 162 } List < Statement > statements () : 165 { Statement s; List < Statement > statements = new ArrayList < Statement >() ;} 166 { 167 ( s = statement () {if (s!= null) { statements. add(s);} } )* { return statements ; } 168 } Statement statement () : 171 { Statement s; List < Declaration > decls; } 172 { 173 <SEMI > { return null; } 174 decls = declaration () { return new DeclContainer ( decls); } 175 s = block () { return s; } 176 s = assignment () { return s; } 177 s = ifstatement () { return s; } 178 s = whilestatement () { return s; } 179 } Block block () : 182 { List < Statement > ss; } 183 { 184 <LCURLY > ss = statements () <RCURLY > { return new Block( ss); } 185 } Assignment assignment () : 188 { Token id ; LValue target; Expression e;} 189 { 190 id = <IDENTIFIER > ( 191 <LBRACE > e = expression () <RBRACE > { target = new ListTupleReference (id, e);} 192 { target = new Variable( id); } 193 ) <EQUALS > e = expression () <SEMI > 194 { return new Assignment ( target, e);} 195 } Conditional ifstatement () : 198 { Expression e; Statement sif = null, selse = null; Conditional c; } 199 { 200 <IF > <LPAREN > e = expression () <RPAREN > sif = block () 201 [ <ELSE > ( selse = ifstatement () selse = block ()) ] 202 { 203 return new Conditional (e, sif, selse); 204 } 205 } Loop whilestatement () : 208 { Expression e; Loop l; Block s; } 209 { 210 <WHILE > <LPAREN > e = expression () <RPAREN > s = block () 211 { return new Loop(e, s); } 212 } Expression expression () : 5

6 215 { Expression e1, e2, current = null; Token t; Operator o = Operator. OR; } 216 { 217 e1 = conjunction () { current = e1; } 218 ( t = <OR_OP > e2 = conjunction () { current = new Binary(new OpTokenPair (o, t), current, e2); } 219 )* { return current; } 220 } Expression conjunction () : 223 { Expression e1, e2, current = null; Token t; Operator o = Operator. AND; } 224 { 225 e1 = equality () { current = e1; } 226 ( t = <AND_OP > e2 = equality () { current = new Binary(new OpTokenPair ( o, t), current, e2); } 227 )* { return current; } 228 } Expression equality () : 231 { Expression e1, e2, current = null; OpTokenPair o; } 232 { 233 e1 = relation () { current = e1; } 234 ( o = equop () e2 = relation () { current = new Binary(o, current, e2); } 235 )* { return current; } 236 } OpTokenPair equop () : 239 { Token t;} 240 { 241 t = <EQ_OP > { return new OpTokenPair ( Operator.EQ, t); } 242 t = <NE_OP > { return new OpTokenPair ( Operator.NE, t); } 243 } Expression relation () : 246 { Expression e1, e2, current = null; OpTokenPair o; } 247 { 248 e1 = addition () { current = e1; } 249 ( o = relop () e2 = addition () { current = new Binary(o, current, e2); } 250 )* { return current; } 251 } OpTokenPair relop () : 254 { Token t;} 255 { 256 t = <LT_OP > { return new OpTokenPair ( Operator.LT, t); } 257 t = <LE_OP > { return new OpTokenPair ( Operator.LE, t); } 258 t = <GT_OP > { return new OpTokenPair ( Operator.GT, t); } 259 t = <GE_OP > { return new OpTokenPair ( Operator.GE, t); } 260 } Expression addition () : 263 { Expression e1, e2, current = null; OpTokenPair o; } 264 { 265 e1 = term () { current = e1; } 266 ( LOOKAHEAD (2) o = addop () e2 = term () { current = new Binary(o, 6

7 current, e2); } 267 )* { return current; } 268 } OpTokenPair addop () : 271 { Token t;} 272 { 273 t = <PLUS > { return new OpTokenPair ( Operator. PLUS, t); } 274 t = <MINUS > { return new OpTokenPair ( Operator. MINUS, t); } 275 } Expression term () : 278 { Expression e1, e2, current = null; OpTokenPair o; } 279 { 280 e1 = factor () { current = e1; } 281 ( o = mulop () e2 = factor () { current = new Binary(o, current, e2); } 282 )* { return current; } 283 } OpTokenPair mulop () : 286 { Token t;} 287 { 288 t = <MULT > { return new OpTokenPair ( Operator. TIMES, t); } 289 t = <DIV > { return new OpTokenPair ( Operator. DIV, t); } 290 t = <PRCNT > { return new OpTokenPair ( Operator. MOD, t); } 291 } Expression factor () : 294 { Expression e; OpTokenPair o = null; } 295 { 296 [ o = unaryop () ] e = primary () { return (o == null)? e : new Unary(o, e); } 297 } OpTokenPair unaryop () : 300 { Token t; } 301 { 302 t = <MINUS > { return new OpTokenPair ( Operator. NEG, t); } 303 t = <BANG > { return new OpTokenPair ( Operator. NOT, t); } 304 } Expression primary () : 307 { Expression e; Token t;} 308 { 309 e = identifierorarrayref () { return e; } 310 e = literal () { return e; } 311 } Expression identifierorarrayref () : 314 { Token id; Expression ref = null; } 315 { 316 id = <IDENTIFIER > [ LOOKAHEAD (2) <LBRACE > ref = expression () <RBRACE > ] 317 { 318 if ( ref == null) { 319 return new Variable( id); 320 } else { 7

8 321 return new ListTupleReference (id, ref); 322 } 323 } 324 } Expression literal () : 327 { Token t;} 328 { 329 t = <INTEGER > { return new IntValue(t); } 330 t = <TRUE > { return new BoolValue (true, t. beginline ); } 331 t = <FALSE > { return new BoolValue (false, t. beginline ); } 332 t = <CLFLOAT > { return new FloatValue (t); } 333 } 8

1 options { 2 STATIC = false ; 3 } 4 PARSER_BEGIN(Parser) 5 package proj.parser; 6 7 import java.io.*; 8 import java.util.

1 options { 2 STATIC = false ; 3 } 4 PARSER_BEGIN(Parser) 5 package proj.parser; 6 7 import java.io.*; 8 import java.util. 1 options { 2 STATIC = false ; 3 } 4 PARSER_BEGIN(Parser) 5 package proj.parser; 6 7 import java.io.*; 8 import java.util.*; 9 import static proj.abstractsyntax.*; 10 import proj.abstractsyntax; 11 12

More information

COSE312: Compilers. Lecture 2 Lexical Analysis (1)

COSE312: Compilers. Lecture 2 Lexical Analysis (1) COSE312: Compilers Lecture 2 Lexical Analysis (1) Hakjoo Oh 2017 Spring Hakjoo Oh COSE312 2017 Spring, Lecture 2 March 12, 2017 1 / 15 Lexical Analysis ex) Given a C program float match0 (char *s) /* find

More information

Compiling Techniques

Compiling Techniques Lecture 7: Abstract Syntax 13 October 2015 Table of contents Syntax Tree 1 Syntax Tree Semantic Actions Examples Abstract Grammar 2 Internal Representation AST Builder 3 Visitor Processing Semantic Actions

More information

ChemLAB Final Report COMS W Programming Languages & Translators Professor Stephen Edwards

ChemLAB Final Report COMS W Programming Languages & Translators Professor Stephen Edwards ChemLAB Final Report COMS W4115 - Programming Languages & Translators Professor Stephen Edwards Alice Chang (avc2120) Gabriel Lu (ggl2110) Martin Ong (mo2454) December 17, 2014 Contents Introduction 3

More information

Lecture 5: Sep. 23 &25

Lecture 5: Sep. 23 &25 CIS 2168 Data Structures Fall 2014 Lecturer: Anwar Mamat Lecture 5: Sep. 23 &25 Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor. 5.1 Doubly Linked

More information

Elementary Sorts 1 / 18

Elementary Sorts 1 / 18 Elementary Sorts 1 / 18 Outline 1 Rules of the Game 2 Selection Sort 3 Insertion Sort 4 Shell Sort 5 Visualizing Sorting Algorithms 6 Comparing Sorting Algorithms 2 / 18 Rules of the Game Sorting is the

More information

CMSC 132, Object-Oriented Programming II Summer Lecture 6:

CMSC 132, Object-Oriented Programming II Summer Lecture 6: CMSC 132, Object-Oriented Programming II Summer 2016 Lecturer: Anwar Mamat Lecture 6: Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor. 6.1 Singly

More information

Compiler Construction Lent Term 2015 Lectures (of 16)

Compiler Construction Lent Term 2015 Lectures (of 16) Compiler Construction Lent Term 2015 Lectures 13 --- 16 (of 16) 1. Return to lexical analysis : application of Theory of Regular Languages and Finite Automata 2. Generating Recursive descent parsers 3.

More information

CS-140 Fall 2018 Test 2 Version Practice Nov. 12, Name:

CS-140 Fall 2018 Test 2 Version Practice Nov. 12, Name: CS-140 Fall 2018 Test 2 Version Practice Nov. 12, 2018 Name: 1. (10 points) For the following, Check T if the statement is true, or F if the statement is false. (a) T F : If a child overrides its parent

More information

Introduction to ANTLR (ANother Tool for Language Recognition)

Introduction to ANTLR (ANother Tool for Language Recognition) Introduction to ANTLR (ANother Tool for Language Recognition) Jon Eyolfson University of Waterloo September 27 - October 1, 2010 Outline Introduction Usage Example Demonstration Conclusion Jon Eyolfson

More information

Compiler Construction Lent Term 2015 Lectures (of 16)

Compiler Construction Lent Term 2015 Lectures (of 16) Compiler Construction Lent Term 2015 Lectures 13 --- 16 (of 16) 1. Return to lexical analysis : application of Theory of Regular Languages and Finite Automata 2. Generating Recursive descent parsers 3.

More information

Compiler Construction Lectures 13 16

Compiler Construction Lectures 13 16 Compiler Construction Lectures 13 16 Lent Term, 2013 Lecturer: Timothy G. Griffin 1 Generating Lexical Analyzers Source Program Lexical Analyzer tokens Parser Lexical specification Scanner Generator LEX

More information

CompA - Complex Analyzer

CompA - Complex Analyzer CompA - Complex Analyzer Xiping Liu(xl2639), Jianshuo Qiu(jq2253), Tianwu Wang(tw2576), Yingshuang Zheng(yz3083), Zhanpeng Su(zs2329) Septembee 25, 2017 1 Introduction The motivation for writing this language

More information

Compiling Techniques

Compiling Techniques Lecture 3: Introduction to 22 September 2017 Reminder Action Create an account and subscribe to the course on piazza. Coursework Starts this afternoon (14.10-16.00) Coursework description is updated regularly;

More information

MiniMat: Matrix Language in OCaml LLVM

MiniMat: Matrix Language in OCaml LLVM Terence Lim - tl2735@columbia.edu August 17, 2016 Contents 1 Introduction 4 1.1 Goals........................................ 4 1.1.1 Flexible matrix notation......................... 4 1.1.2 Uncluttered................................

More information

CS-140 Fall 2018 Test 2 Version Practice Nov. 12, 2018

CS-140 Fall 2018 Test 2 Version Practice Nov. 12, 2018 CS-140 Fall 2018 Test 2 Version Practice Nov. 12, 2018 Name: 1. (10 points) For the following, Check T if the statement is true, or F if the statement is false. (a) T X F : If a child overrides its parent

More information

Discovering Spam On Twitter

Discovering Spam On Twitter Virginia Commonwealth University VCU Scholars Compass Theses and Dissertations Graduate School 2014 Discovering Spam On Twitter Ioana-Alexandra Bara Virginia Commonwealth University Follow this and additional

More information

DM507 - Algoritmer og Datastrukturer Project 1

DM507 - Algoritmer og Datastrukturer Project 1 DM507 - Algoritmer og Datastrukturer Project 1 Christian Skjøth Mat-Øk 280588 Morten Olsen Mat-Øk 090789 19. marts 2012 Task 1 - Double for-loop So, first we needed to create an instance of the class File

More information

INF5110 Compiler Construction

INF5110 Compiler Construction INF5110 Compiler Construction Parsing Spring 2016 1 / 84 Overview First and Follow set: general concepts for grammars textbook looks at one parsing technique (top-down) [Louden, 1997, Chap. 4] before studying

More information

CMSC 132, Object-Oriented Programming II Summer Lecture 12

CMSC 132, Object-Oriented Programming II Summer Lecture 12 CMSC 132, Object-Oriented Programming II Summer 2016 Lecturer: Anwar Mamat Lecture 12 Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor. 12.1 Trees

More information

public void run ( ) { i f ( this. id == 0) System. out. p r i n t ( 3 ) ; bro. j o i n ( ) ; else System. out. p r i n t ( 2 ) ;

public void run ( ) { i f ( this. id == 0) System. out. p r i n t ( 3 ) ; bro. j o i n ( ) ; else System. out. p r i n t ( 2 ) ; 1 Unusual programs 1. Consider the following Java program : public c l a s s Thread2 extends Thread { public int id ; public Thread2 bro ; public Thread2 ( int id, Thread2 bro ) { this. id = id ; this.

More information

1. Write a program to calculate distance traveled by light

1. Write a program to calculate distance traveled by light G. H. R a i s o n i C o l l e g e O f E n g i n e e r i n g D i g d o h H i l l s, H i n g n a R o a d, N a g p u r D e p a r t m e n t O f C o m p u t e r S c i e n c e & E n g g P r a c t i c a l M a

More information

EDA045F: Program Analysis LECTURE 10: TYPES 1. Christoph Reichenbach

EDA045F: Program Analysis LECTURE 10: TYPES 1. Christoph Reichenbach EDA045F: Program Analysis LECTURE 10: TYPES 1 Christoph Reichenbach In the last lecture... Performance Counters Challenges in Dynamic Performance Analysis Taint Analysis Binary Instrumentation 2 / 44 Types

More information

Syntax Analysis Part III

Syntax Analysis Part III Syntax Analysis Part III Chapter 4: Top-Down Parsing Slides adapted from : Robert van Engelen, Florida State University Eliminating Ambiguity stmt if expr then stmt if expr then stmt else stmt other The

More information

Language for Linear Algebra

Language for Linear Algebra Language for Linear Algebra Author: Chenzhe Qian Guitian Lan Jin Liang Zhiyuan Guo UNI: cq2185 gl2510 jl4598 zg2201 December 22, 2015 Contents 1 Introduction 2 1.1 Background...............................

More information

[4203] Compiler Theory Sheet # 3-Answers

[4203] Compiler Theory Sheet # 3-Answers El-Shourouk cademy cad. Year : 2012 / 2013 Higher Institute for Computer & Term : Second Information Technology Year : Fourth Department of Computer Science [4203] Compiler Theory Sheet # 3-nswers 1- Write

More information

1 Java Night Countdown (30%)

1 Java Night Countdown (30%) Midterm Examination Problem Sheet TIME: 04/18/2009, 19:00 21:00 This is a open-textbook exam. You can use the Absolute Java textbook as your reference during the exam. Any other references are not allowed.

More information

Introduction to Programming (Java) 3/12

Introduction to Programming (Java) 3/12 Introduction to Programming (Java) 3/12 Michal Krátký Department of Computer Science Technical University of Ostrava Introduction to Programming (Java) 2008/2009 c 2006 2008 Michal Krátký Introduction

More information

Object Oriented Software Design (NTU, Class Even, Spring 2009) Final Examination Problem Sheet TIME: 06/16/2009, 14:20 17:20

Object Oriented Software Design (NTU, Class Even, Spring 2009) Final Examination Problem Sheet TIME: 06/16/2009, 14:20 17:20 Final Examination Problem Sheet TIME: 06/16/2009, 14:20 17:20 This is a closed-book exam. Any form of cheating or lying will not be tolerated. Students can get zero scores and/or fail the class and/or

More information

Tasks of lexer. CISC 5920: Compiler Construction Chapter 2 Lexical Analysis. Tokens and lexemes. Buffering

Tasks of lexer. CISC 5920: Compiler Construction Chapter 2 Lexical Analysis. Tokens and lexemes. Buffering Tasks of lexer CISC 5920: Compiler Construction Chapter 2 Lexical Analysis Arthur G. Werschulz Fordham University Department of Computer and Information Sciences Copyright Arthur G. Werschulz, 2017. All

More information

DNHI Homework 2 Solutions Recursion

DNHI Homework 2 Solutions Recursion Solutions Recursion Problem 1 Part A Write an iterative method that computes a value of x n for a positive integer n and a real number x. The return value of -1 indicates an error condition. 1 public static

More information

CSC 4181Compiler Construction. Context-Free Grammars Using grammars in parsers. Parsing Process. Context-Free Grammar

CSC 4181Compiler Construction. Context-Free Grammars Using grammars in parsers. Parsing Process. Context-Free Grammar CSC 4181Compiler Construction Context-ree Grammars Using grammars in parsers CG 1 Parsing Process Call the scanner to get tokens Build a parse tree from the stream of tokens A parse tree shows the syntactic

More information

CA Compiler Construction

CA Compiler Construction CA4003 - Compiler Construction Bottom Up Parsing David Sinclair Bottom Up Parsing LL(1) parsers have enjoyed a bit of a revival thanks to JavaCC. LL(k) parsers must predict which production rule to use

More information

Constructors - Cont. must be distinguished by the number or type of their arguments.

Constructors - Cont. must be distinguished by the number or type of their arguments. Constructors - Cont. 1 Constructors can be overloaded! must be distinguished by the number or type of their arguments. 2 When no constructor is defined, there is a default constructor with no arguments.

More information

ITI Introduction to Computing II

ITI Introduction to Computing II (with contributions from R. Holte) School of Electrical Engineering and Computer Science University of Ottawa Version of January 11, 2015 Please don t print these lecture notes unless you really need to!

More information

Richard Gibson CSC 421 Assignment #4 Apr. 4, 2006 #

Richard Gibson CSC 421 Assignment #4 Apr. 4, 2006 # Richard Gibson CSC 421 Assignment #4 Apr. 4, 26 #235677 Probability Theory Suppose that we are given cipher texts which encode messages in such a way that each letter of the alphabet is mapped to a sequence

More information

Plan for Today and Beginning Next week (Lexical Analysis)

Plan for Today and Beginning Next week (Lexical Analysis) Plan for Today and Beginning Next week (Lexical Analysis) Regular Expressions Finite State Machines DFAs: Deterministic Finite Automata Complications NFAs: Non Deterministic Finite State Automata From

More information

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name: CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, 2017 Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : If a child overrides

More information

Übung Informatik I - Programmierung - Blatt 7

Übung Informatik I - Programmierung - Blatt 7 RHEINISCH- WESTFÄLISCHE TECHNISCHE HOCHSCHULE AACHEN LEHR- UND FORSCHUNGSGEBIET INFORMATIK II RWTH Aachen D-52056 Aachen GERMANY http://programmierung.informatik.rwth-aachen.de LuFG Informatik II Prof.

More information

Compiler Design. Spring Syntactic Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Compiler Design. Spring Syntactic Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz Compiler Design Spring 2015 Syntactic Analysis Sample Exercises and Solutions Prof. Pedro C. Diniz USC / Information Sciences Institute 4676 Admiralty Way, Suite 1001 Marina del Rey, California 90292 pedro@isi.edu

More information

Predictive parsing as a specific subclass of recursive descent parsing complexity comparisons with general parsing

Predictive parsing as a specific subclass of recursive descent parsing complexity comparisons with general parsing Plan for Today Recall Predictive Parsing when it works and when it doesn t necessary to remove left-recursion might have to left-factor Error recovery for predictive parsers Predictive parsing as a specific

More information

Reasoning About Imperative Programs. COS 441 Slides 10b

Reasoning About Imperative Programs. COS 441 Slides 10b Reasoning About Imperative Programs COS 441 Slides 10b Last time Hoare Logic: { P } C { Q } Agenda If P is true in the initial state s. And C in state s evaluates to s. Then Q must be true in s. Program

More information

CS415 Compilers Syntax Analysis Top-down Parsing

CS415 Compilers Syntax Analysis Top-down Parsing CS415 Compilers Syntax Analysis Top-down Parsing These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Announcements Midterm on Thursday, March 13

More information

DARN: A Matrix Manipulation Language

DARN: A Matrix Manipulation Language DARN: A Matrix Manipulation Language Daisy Chaussee (dac2183) Anthony Kim (ak3703) Rafael Takasu (rgt2108) Ignacio (Nacho) Torras (it2216) December 20, 2016 1 Contents 1 Introduction to the Language 4

More information

pixelman Final Report Anthony Chan, Teresa Choe, Gabriel Kramer-Garcia, Brian Tsau

pixelman Final Report Anthony Chan, Teresa Choe, Gabriel Kramer-Garcia, Brian Tsau pixelman Final Report Anthony Chan, Teresa Choe, Gabriel Kramer-Garcia, Brian Tsau December 2017 Contents 1 Introduction 3 2 Language Tutorial 3 2.1 Statements and Variable declaration...........................

More information

CS153: Compilers Lecture 5: LL Parsing

CS153: Compilers Lecture 5: LL Parsing CS153: Compilers Lecture 5: LL Parsing Stephen Chong https://www.seas.harvard.edu/courses/cs153 Announcements Proj 1 out Due Thursday Sept 20 (2 days away) Proj 2 out Due Thursday Oct 4 (16 days away)

More information

ITI Introduction to Computing II

ITI Introduction to Computing II (with contributions from R. Holte) School of Electrical Engineering and Computer Science University of Ottawa Version of January 9, 2019 Please don t print these lecture notes unless you really need to!

More information

INF5110 Compiler Construction

INF5110 Compiler Construction INF5110 Compiler Construction Spring 2017 1 / 330 Outline 1. Parsing First and follow sets Top-down parsing Bottom-up parsing References 2 / 330 INF5110 Compiler Construction Parsing Spring 2017 3 / 330

More information

CSE 311: Foundations of Computing. Lecture 10: Set Operations & Representation, Modular Arithmetic

CSE 311: Foundations of Computing. Lecture 10: Set Operations & Representation, Modular Arithmetic CSE 311: Foundations of Computing Lecture 10: Set Operations & Representation, Modular Arithmetic Definitions A and B are equal if they have the same elements A = B x (x A x B) A is a subset of B if every

More information

More About Methods. Hsuan-Tien Lin. Deptartment of CSIE, NTU. OOP Class, March 8-9, 2010

More About Methods. Hsuan-Tien Lin. Deptartment of CSIE, NTU. OOP Class, March 8-9, 2010 More About Methods Hsuan-Tien Lin Deptartment of CSIE, NTU OOP Class, March 8-9, 2010 H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 0 / 24 Methods: the Basic Method (1/2, Callee s View) 1 p

More information

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10 Merge Sort 1 / 10 Outline 1 Merging 2 Merge Sort 3 Complexity of Sorting 4 Merge Sort and Other Sorts 2 / 10 Merging Merge sort is based on a simple operation known as merging: combining two ordered arrays

More information

Bottom-Up Syntax Analysis

Bottom-Up Syntax Analysis Bottom-Up Syntax Analysis Mooly Sagiv http://www.cs.tau.ac.il/~msagiv/courses/wcc13.html Textbook:Modern Compiler Design Chapter 2.2.5 (modified) 1 Pushdown automata Deterministic fficient Parsers Report

More information

INTRODUCTION. This is not a full c-programming course. It is not even a full 'Java to c' programming course.

INTRODUCTION. This is not a full c-programming course. It is not even a full 'Java to c' programming course. C PROGRAMMING 1 INTRODUCTION This is not a full c-programming course. It is not even a full 'Java to c' programming course. 2 LITTERATURE 3. 1 FOR C-PROGRAMMING The C Programming Language (Kernighan and

More information

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages CS 314 Principles of Programming Languages Lecture 7: LL(1) Parsing Zheng (Eddy) Zhang Rutgers University February 7, 2018 Class Information Homework 3 will be posted this coming Monday. 2 Review: Top-Down

More information

Functional Programming with F# Overview and Basic Concepts

Functional Programming with F# Overview and Basic Concepts Functional Programming with F# Overview and Basic Concepts Radu Nicolescu Department of Computer Science University of Auckland 27 Sep 2017 1 / 52 1 Background 2 Overview 3 Type System and Type Inference

More information

2 - Strings and Binomial Coefficients

2 - Strings and Binomial Coefficients November 14, 2017 2 - Strings and Binomial Coefficients William T. Trotter trotter@math.gatech.edu Basic Definition Let n be a positive integer and let [n] = {1, 2,, n}. A sequence of length n such as

More information

Introduction to functions

Introduction to functions Introduction to functions Comp Sci 1570 Introduction to C++ Outline 1 2 Functions A function is a reusable sequence of s designed to do a particular job. In C++, a function is a group of s that is given

More information

Google Go illustrated on the basis of Fibonacci numbers

Google Go illustrated on the basis of Fibonacci numbers Google Go illustrated on the basis of Fibonacci numbers Jan Pennekamp RWTH University Aachen Chair for Data Management and Data Exploration Prof. Dr. T. Seidl Supervision: Dipl.-Ing. Marwan Hassani Friday,

More information

Simple Type Extensions

Simple Type Extensions Simple Type Extensions Type Systems, Lecture 4 Jevgeni Kabanov Tartu, 14.02.2006 PREVIOUSLY ON TYPE SYSTEMS Lambda Calculus Embedded Booleans and Arithmetical expressions Fixpoints and Recursion Simple

More information

Lecture 5: Jun. 10, 2015

Lecture 5: Jun. 10, 2015 CMSC 132, Object-Oriented Programming II Summer 2015 Lecturer: Anwar Mamat Lecture 5: Jun. 10, 2015 Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor.

More information

CS 406: Bottom-Up Parsing

CS 406: Bottom-Up Parsing CS 406: Bottom-Up Parsing Stefan D. Bruda Winter 2016 BOTTOM-UP PUSH-DOWN AUTOMATA A different way to construct a push-down automaton equivalent to a given grammar = shift-reduce parser: Given G = (N,

More information

Syntax Directed Transla1on

Syntax Directed Transla1on Syntax Directed Transla1on Syntax Directed Transla1on CMPT 379: Compilers Instructor: Anoop Sarkar anoopsarkar.github.io/compilers-class Syntax directed Translation Models for translation from parse trees

More information

Programming Language Concepts, CS2104 Lecture 3

Programming Language Concepts, CS2104 Lecture 3 Programming Language Concepts, CS2104 Lecture 3 Statements, Kernel Language, Abstract Machine 31 Aug 2007 CS2104, Lecture 3 1 Reminder of last lecture Programming language definition: syntax, semantics

More information

CMSC 132, Object-Oriented Programming II Summer Lecture 1:

CMSC 132, Object-Oriented Programming II Summer Lecture 1: CMSC 132, Object-Oriented Programming II Summer 2016 Lecturer: Anwar Mamat Lecture 1: Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor. 1.1 Course

More information

Compiler Principles, PS4

Compiler Principles, PS4 Top-Down Parsing Compiler Principles, PS4 Parsing problem definition: The general parsing problem is - given set of rules and input stream (in our case scheme token input stream), how to find the parse

More information

Problem Decomposition: One Professor s Approach to Coding

Problem Decomposition: One Professor s Approach to Coding Problem Decomposition: One Professor s Approach to Coding zombie[1] zombie[3] Fewer Buuuuugs zombie[4] zombie[2] zombie[5] zombie[0] Fundamentals of Computer Science I Overview Problem Solving Understand

More information

More on Methods and Encapsulation

More on Methods and Encapsulation More on Methods and Encapsulation Hsuan-Tien Lin Deptartment of CSIE, NTU OOP Class, March 31, 2009 H.-T. Lin (NTU CSIE) More on Methods and Encapsulation OOP(even) 03/31/2009 0 / 38 Local Variables Local

More information

Introduction to Algorithmic Complexity. D. Thiebaut CSC212 Fall 2014

Introduction to Algorithmic Complexity. D. Thiebaut CSC212 Fall 2014 Introduction to Algorithmic Complexity D. Thiebaut CSC212 Fall 2014 http://youtu.be/p0tlbl5lrj8 Case Study: Fibonacci public class RecurseFib {! private static long computefibrecursively( int n ) { if

More information

Compiling Techniques

Compiling Techniques Lecture 5: Top-Down Parsing 26 September 2017 The Parser Context-Free Grammar (CFG) Lexer Source code Scanner char Tokeniser token Parser AST Semantic Analyser AST IR Generator IR Errors Checks the stream

More information

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing CMSC 330: Organization of Programming Languages Pushdown Automata Parsing Chomsky Hierarchy Categorization of various languages and grammars Each is strictly more restrictive than the previous First described

More information

Bottom up parsing. General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing);

Bottom up parsing. General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing); Bottom up parsing General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing); 1 Top-down vs Bottom-up Bottom-up more powerful than top-down; Can process

More information

The Midterm Exam. Hsuan-Tien Lin. Department of CSIE, NTU. OOP Class, April 26-27, 2010

The Midterm Exam. Hsuan-Tien Lin. Department of CSIE, NTU. OOP Class, April 26-27, 2010 The Midterm Exam Hsuan-Tien Lin Department of CSIE, NTU OOP Class, April 26-27, 2010 H.-T. Lin (NTU CSIE) The Midterm Exam OOP 04/26-27/2010 0 / 20 Java Night Countdown I 1 (2%) What is the fully-qualified

More information

Python. chrysn

Python. chrysn Python chrysn 2008-09-25 Introduction Structure, Language & Syntax Strengths & Weaknesses Introduction Structure, Language & Syntax Strengths & Weaknesses Python Python is an interpreted,

More information

CSE 311: Foundations of Computing. Lecture 10: Set Operations & Representation, Modular Arithmetic

CSE 311: Foundations of Computing. Lecture 10: Set Operations & Representation, Modular Arithmetic CSE 311: Foundations of Computing Lecture 10: Set Operations & Representation, Modular Arithmetic Definitions A and B are equalif they have the same elements A = B x(x A x B) A is a subsetof B if every

More information

Lecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS Berkley University 1

Lecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS Berkley University 1 Lecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS 164 -- Berkley University 1 Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient

More information

Notater: INF3331. Veronika Heimsbakk December 4, Introduction 3

Notater: INF3331. Veronika Heimsbakk December 4, Introduction 3 Notater: INF3331 Veronika Heimsbakk veronahe@student.matnat.uio.no December 4, 2013 Contents 1 Introduction 3 2 Bash 3 2.1 Variables.............................. 3 2.2 Loops...............................

More information

Administrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example

Administrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example Administrivia Test I during class on 10 March. Bottom-Up Parsing Lecture 11-12 From slides by G. Necula & R. Bodik) 2/20/08 Prof. Hilfinger CS14 Lecture 11 1 2/20/08 Prof. Hilfinger CS14 Lecture 11 2 Bottom-Up

More information

Fall 2015 Lecture 14: Modular congruences. cse 311: foundations of computing

Fall 2015 Lecture 14: Modular congruences. cse 311: foundations of computing Fall 2015 Lecture 14: Modular congruences cse 311: foundations of computing If a and b are positive integers, then gcd a, b = gcd (b, a mod b) Useful GCD Fact Proof: By definition a = a div b b + (a mod

More information

THEORY OF COMPILATION

THEORY OF COMPILATION Lecture 04 Syntax analysis: top-down and bottom-up parsing THEORY OF COMPILATION EranYahav 1 You are here Compiler txt Source Lexical Analysis Syntax Analysis Parsing Semantic Analysis Inter. Rep. (IR)

More information

JChartLib ver 1.0. Silvio Schneider, June 7, Introduction 3. 2 Chart and Dataset 4. 3 Adding data to a chart 4

JChartLib ver 1.0. Silvio Schneider, June 7, Introduction 3. 2 Chart and Dataset 4. 3 Adding data to a chart 4 JChartLib ver 1.0 Silvio Schneider, contact@suvi.org June 7, 2012 Contents 1 Introduction 3 2 Chart and Dataset 4 3 Adding data to a chart 4 4 Linechart 5 4.1 Linechart Example........................................

More information

Sharing Objects. Pieter van den Hombergh. Fontys Hogeschool voor Techniek en Logistiek. February 15, 2017

Sharing Objects. Pieter van den Hombergh. Fontys Hogeschool voor Techniek en Logistiek. February 15, 2017 Pieter van den Hombergh Fontys Hogeschool voor Techniek en Logistiek February 15, 2017 and /FHTenL February 15, 2017 is safe Idioms 1/34 and and is safe Idioms /FHTenL February 15, 2017 2/34 visibility

More information

The Life Cycle of Grammarware. CWI Scientific Meeting Vadim Zaytsev, SWAT, CWI 2012

The Life Cycle of Grammarware. CWI Scientific Meeting Vadim Zaytsev, SWAT, CWI 2012 The Life Cycle of Grammarware CWI Scientific Meeting Vadim Zaytsev, SWAT, CWI 2012 Grammarware Software Languages Language: make all: test: make clean make build make test./converge.py master.bgf base/

More information

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ; 1 Trees The next major set of data structures belongs to what s called Trees. They are called that, because if you try to visualize the structure, it kind of looks like a tree (root, branches, and leafs).

More information

Lecture 11 Context-Free Languages

Lecture 11 Context-Free Languages Lecture 11 Context-Free Languages COT 4420 Theory of Computation Chapter 5 Context-Free Languages n { a b : n n { ww } 0} R Regular Languages a *b* ( a + b) * Example 1 G = ({S}, {a, b}, S, P) Derivations:

More information

Geometry with complex numbers

Geometry with complex numbers University of Cape Town 10 May 2012 Outline 1 Complex numbers Definition Geometric interpretation 2 Outline Complex numbers Definition Geometric interpretation 1 Complex numbers Definition Geometric interpretation

More information

Motivation. Dictionaries. Direct Addressing. CSE 680 Prof. Roger Crawfis

Motivation. Dictionaries. Direct Addressing. CSE 680 Prof. Roger Crawfis Motivation Introduction to Algorithms Hash Tables CSE 680 Prof. Roger Crawfis Arrays provide an indirect way to access a set. Many times we need an association between two sets, or a set of keys and associated

More information

Lists, Stacks, and Queues (plus Priority Queues)

Lists, Stacks, and Queues (plus Priority Queues) Lists, Stacks, and Queues (plus Priority Queues) The structures lists, stacks, and queues are composed of similar elements with different operations. Likewise, with mathematics: (Z, +, 0) vs. (Z,, 1) List

More information

Syntactic Analysis. Top-Down Parsing

Syntactic Analysis. Top-Down Parsing Syntactic Analysis Top-Down Parsing Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in Compilers class at University of Southern California (USC) have explicit permission to make

More information

SFWR ENG/COMP SCI 2S03 Principles of Programming

SFWR ENG/COMP SCI 2S03 Principles of Programming (Slide 1 of 78) Dr. Ridha Khedri Department of Computing and Software, McMaster University Canada L8S 4L7, Hamilton, Ontario Acknowledgments: Material based on Java actually: A Comprehensive Primer in

More information

Introduction to Computer Programming, Spring Term 2018 Practice Assignment 1 Discussion:

Introduction to Computer Programming, Spring Term 2018 Practice Assignment 1 Discussion: German University in Cairo Media Engineering and Technology Prof. Dr. Slim Abdennadher Dr. Rimon Elias Dr. Hisham Othman Introduction to Computer Programming, Spring Term 2018 Practice Assignment 1 Discussion:

More information

Parsing Algorithms. CS 4447/CS Stephen Watt University of Western Ontario

Parsing Algorithms. CS 4447/CS Stephen Watt University of Western Ontario Parsing Algorithms CS 4447/CS 9545 -- Stephen Watt University of Western Ontario The Big Picture Develop parsers based on grammars Figure out properties of the grammars Make tables that drive parsing engines

More information

Syntax Analysis Part I

Syntax Analysis Part I 1 Syntax Analysis Part I Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2013 2 Position of a Parser in the Compiler Model Source Program Lexical Analyzer

More information

Syntax Analysis Part I

Syntax Analysis Part I 1 Syntax Analysis Part I Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2013 2 Position of a Parser in the Compiler Model Source Program Lexical Analyzer

More information

1. For the following sub-problems, consider the following context-free grammar: S AA$ (1) A xa (2) A B (3) B yb (4)

1. For the following sub-problems, consider the following context-free grammar: S AA$ (1) A xa (2) A B (3) B yb (4) ECE 468 & 573 Problem Set 2: Contet-free Grammars, Parsers 1. For the following sub-problems, consider the following contet-free grammar: S $ (1) (2) (3) (4) λ (5) (a) What are the terminals and non-terminals

More information

CAAM420: Week 3 Wednesday Notes

CAAM420: Week 3 Wednesday Notes CAAM420: Week 3 Wednesday Notes Justin DeVito 09/11/13 1 Overview of Homework 3 For an example function declaration: int rand ( ) int - return type rand - function name () - argument list (which can be

More information

Computer Science 160 Translation of Programming Languages

Computer Science 160 Translation of Programming Languages Computer Science 160 Translation of Programming Languages Instructor: Christopher Kruegel Top-Down Parsing Top-down Parsing Algorithm Construct the root node of the parse tree, label it with the start

More information

CSE 311 Lecture 11: Modular Arithmetic. Emina Torlak and Kevin Zatloukal

CSE 311 Lecture 11: Modular Arithmetic. Emina Torlak and Kevin Zatloukal CSE 311 Lecture 11: Modular Arithmetic Emina Torlak and Kevin Zatloukal 1 Topics Sets and set operations A quick wrap-up of Lecture 10. Modular arithmetic basics Arithmetic over a finite domain (a.k.a

More information

Types and Programming Languages (15-814), Fall 2018 Assignment 4: Data Representation (Sample Solutions)

Types and Programming Languages (15-814), Fall 2018 Assignment 4: Data Representation (Sample Solutions) Types and Programming Languages (15-814), Fall 2018 Assignment 4: Data Representation (Sample Solutions) Contact: 15-814 Course Staff Due Tuesday, October 16, 2018, 10:30am This assignment is due by 10:30am

More information

Basic Java OOP 10/12/2015. Department of Computer Science & Information Engineering. National Taiwan University

Basic Java OOP 10/12/2015. Department of Computer Science & Information Engineering. National Taiwan University Basic Java OOP 10/12/2015 Hsuan-Tien Lin ( 林軒田 ) htlin@csie.ntu.edu.tw Department of Computer Science & Information Engineering National Taiwan University ( 國立台灣大學資訊工程系 ) Hsuan-Tien Lin (NTU CSIE) Basic

More information

Syntax Analysis - Part 1. Syntax Analysis

Syntax Analysis - Part 1. Syntax Analysis Syntax Analysis Outline Context-Free Grammars (CFGs) Parsing Top-Down Recursive Descent Table-Driven Bottom-Up LR Parsing Algorithm How to Build LR Tables Parser Generators Grammar Issues for Programming

More information