// the current object. functioninvocation expression. identifier (expressionlist ) // call of an inner function
|
|
- Iris Chapman
- 6 years ago
- Views:
Transcription
1 SFU CMPT 379 Compilers Spring 2015 Assignment 4 Assignment due Thursday, April 9, by 11:59pm. For this assignment, you are to expand your Bunting-3 compiler from assignment 3 to handle Bunting-4. Project submission format is the same as for assignment 3 (an archive of your src directory inside a folder named with your sfu username). Bunting-4 is backwards compatible with Bunting-3. All restrictions and specifications from Bunting-3 are still in force, unless specifically noted. I also omit unchanged productions in the Tokens and Grammar sections. If "..." is listed as a possibility for a production, it means "all possibilities for this production from Bunting-3". Language Bunting-4 Grammar: S globaldefinition globaldefinition* main body functiondefinition typedefinition classdefinition classdefinition class identifier ( parameterlist ) classbody classbody { innerdefinition* main body functiondefinition* innerdefinition typedefinition declaration statement break ; continue ; release expressionlist ; expression create user? identifier (expressionlist) // object creation. no immutability annotation create user? mutarraytype ( expression ) [ user? mutannotation? expressionlist ] user? mutannotation? stringconstant copy user? mutannotation? expression [+ user? mutannotation? expression, expression (, expression)* +] this // the current object functioninvocation expression. identifier (expressionlist ) // call of an inner function type identifier // identifier can be a class name 1. Classes Classes are a new type in Bunting Class definitions
2 Classes are limited-functionality objects (they can perform information hiding, but one cannot subclass them or use polymorphism on them). The identifier following the keyword class is the name of the class (i.e. the class name). Class names may not be overloaded (there may not be two classes with the same name, even if the number of parameters are different.) Class names are in the same namespace as all other names, so one cannot declare a class and a global function of the same name The parts of a class The body of the class (body child of classdeclaration) consists of three distinct parts: (1) innerdefinitions. These are definitions for the class variables (fields), and type that will be used inside the class. We sometimes call a field an inner variable and a type declared here as an inner type(def). (2) main block. This is the constructor, called whenever an object of this class is created. (3) functiondefinitions. We call these inner functions. The class definition s parameterlist lists the parameters to be used when a create is issued for this class. They are to be declared as immutable variables in a new scope placed on the class declaration node. That is, they may be overridden in the main body scope (or in any nested scopes). The record for the class should include all parameters and inner variables, but not local variables for the main block or any functions. To implement this, creation parameters should be passed on the frame stack to the constructor but then copied to the class s record at the start of that constructor Class visibility All classes (and inner functions) are visible throughout the program, even before their definition. Collect the names and creation code signatures in a separate pass as was done for function names. You may combine this pass with the function-name collector, or make a separate pass Variable and inner function visibility Inner variables and inner types are visible only within the class definition, and only to code that is past the point of declaration of these variables or types. This is like java private fields. All inner functions are visible throughout the program (like java public ), even before their definition. Variables and types declared in a scope nested anywhere inside the class body are visible only within that nested scope. This includes variables and parameters declared inside the inner functiondeclarations Class creation The only way to create a class instance is with a class creation expression: create user? identifier (expressionlist) The identifier must be a class name. The expressionlist must be type-compatible with the corresponding class definition s parameterlist. This creation expression allocates memory for the class s record and runs the class s main block with the given arguments. The values of all creation parameters and inner variables are kept in the record until the record is released. The visibility of inner variables is discussed above. We will simply use the class name (identifier in the creation expression) as the indicator for the type of object generated by the creation. For example, in imm chessie := create Cat(9);
3 the type of the variable chessie is Cat Class records A class creation expression results in the allocation of a new record (block of memory obtained from the memory manager). The record for a class type Name has the following format: Type identifier (4 bytes) Status (4 bytes) Variables for instance of Name (size of the scope on Name s definition) The type identifier for a class is an integer that identifies the class type (i.e. the class). Start allocating class type identifiers with the integer 128. That is, give the integer 128 to the first class declared in the program, give 129 to the next class declared, etc. The entire record takes 8 + size-of-name s-definition-scope bytes. Note that the definition scope size includes the sizes of any creation parameters and inner variables. The variables in the main block or in inner functions are only visible while that block or function is executing, so you should use a stack frame for them. Use a subscope for the scope attached to the class s body node; it will (as subscopes do) use its parent s allocator, which in this case is the allocator given to the class definition node s scope (which is where I said to place the creation parameters). Allocation of offsets for variables inside a class definition scope is done in a positive sense, starting with the offset 8. Thus, if a class has a parameter x and inner variables f and g, with x being an integer (4 bytes) and both f and g being floats (8 bytes apiece), x will get offset 8, f will get offset 12, and g will get offset 20. You may have to create a new type of scope (with the proper MemoryAccessMethod) to handle these class definition scopes. The status flags used in arrays are also defined for classes: bit 0 (mask 1) contains the immutability status of the class, which is currently unused, so set it to zero. This bit is unused because the class provides immutability designation separately for each variable it contains. Bit 1 (mask 2) contains the subtype-is-reference status, which is also not used for classes (so set it to zero). (As we shall see later, classes have a more complex method for handling subelements that are reference variables. Bit 2 (mask 4) contains the user-managed status, which is set to 1 if the creation expression contains the optional keyword user, and set to 0 otherwise Class variables If Name is the name of a class, then a variable of type Name is a pointer to a class record; it thus occupies 4 bytes. It does not occupy 8 + size-of-name s-body-scope bytes. In other words, class types are all reference (a.k.a. pointer) types. Class variables obey semantics much like java objects (which are themselves reference types): Class variables declared with imm do not change their pointer; however, the contents of the record they point at may change. Imagine that the setcolor call in the following sets a variable inside the Cow s record: imm bessie := create Cow(1); call bessie.setcolor(4); This is valid Bunting-4 and results in bessie pointing at a record that now has color 4. Assignment or initialization of classes with other classes is simply a pointer copy. imm bessie := create Cow(2); imm freddie := bessie; is valid Bunting-4 and results in bessie and freddie being two pointers to the same record. If this is followed by
4 call bessie.setcolor(7); then not only will bessie s color be 7, but freddie s color will, as well. Class variables declared with mut may change their pointer as well as the record contents. mut bessie := create Cow(3); call bessie.setcolor(2); imm nextcolor := 15; imm tempcow := create user Cow; call tempcow.setcolor(nextcolor); mutate bessie := tempcow; is valid Bunting-4. However, class variables may not be assigned a class of a different type. imm bessie := create Cow(4); imm jessie := create Unicorn(5); mut pet := bessie; mutate pet := jessie; is not valid Bunting-4. The mut declaration sets pet s type as Cow, and the mutate tries to update it with a Unicorn, so this should generate a typechecking error Class printing If an expression in a print statement is of class type, then Bunting-4 will print the name of the class. class main { imm buzz := create Bee; print a& buzz& c& nl&; print a, buzz, c& nl&; Will produce: abeec a Bee c 2. Class types Each class definition in a Bunting-4 program creates a new type, known as a class type. You will need to create a new subclass of Type to handle class types, as you did for array types. Each class definition you encounter will lead to the creation of a new instance of ClassType (or whatever you call it). Each class type has a name, which is the name given in the class definition. No two class types (or two class definitions) may have the same name. Like arrays, classes are reference types (a.k.a. pointer types): the value of a class variable is a pointer to the class record. The user can define types based on class types (just like every other type).
5 3. The expression this The lexeme this is now a keyword. When used as an expression, it denotes the instance of the class that contains the function (or creation code) that is currently running. Note that it can not be used explicitly by the Bunting programmer for accessing variables in a class, but it can be used as a function argument or as a qualifier in a functioninvocation. To implement this, we will use a technique known as lambda lifting. In this technique, the compiler adds a parameter to each function (including the constructor) and passes in the correct value of this as the argument. In Bunting, we will put this new parameter at the end of the parameter list. For example, in: class Sheep(int numbaas) { mut wool := 2; main { def int makenoise(int n) { imm scaledwool = 3 * wool; The inner function makenoise is treated as if it were declared: def int makenoise(int n, Sheep thisptr) { Then, in the following sequence: imm garry := create Sheep(21); imm x := garry.makenoise(14); The second statement is treated as: imm x := Sheep.makeNoise(14, garry); Here Sheep.makenoise is not legal Bunting, it just indicates which makenoise to call. In general, expression.functionname(args) is treated as (Type of expression).functionname(args, expression) The last argument to a function is always at offset 0 from the frame pointer, so we can rely on the this pointer being located at the frame pointer. If a inner function accesses any variables or creation parameters of the class, it uses the this pointer to access the correct instance s version of the variable. In the example above, the statement imm scaledwool = 3 * wool; uses the variable wool from the creation code of the class Sheep, and so this can be thought of as imm scaledwool = 3 * thisptr.wool; (but note that the syntax thisptr.wool is not legal Bunting-4.) No explicit conversion to this form is necessary, however. You can simply create a new memory access method that uses double indirection from the frame pointer to calculate the base address. The code for this looks like: PushD $FRAME_POINTER LoadI
6 LoadI (the two LoadI s give it double indirection). The first LoadI gets the value of the frame pointer, and the second LoadI get the value of thisptr. Use your new memory access method for variables declared in the class body scope, and in nested scopes that are not within functions. this is not a targetable expression. Translate the expression this as thisptr (i.e., the last argument); in other words, address code for this is the first four instructions of the sequence above, or: PushD $FRAME_POINTER LoadI PushI 4 Add The type for this is the class type for the class that encloses it. 4. Handling create, the constructor, and its parameters The main block in a class definition can be thought of as a function that modifies the class being created, but has no return statements. We can pass the record for the class being created to the creation code as its this pointer, if we allocate the record before the call. Then the call can return the this pointer using the usual return mechanism, providing the value that the creation expression expects (the pointer to the new class). So implement create for class instances as a modified function call. This can be done (for example, for create Horse(expr1, expr2) ) by: 1) calculating the argument expressions, in order, and pushing them on the frame stack, 2) allocating a record (of size 8+Horse s-scope-size) and pushing the pointer to that record on the frame stack, so that it becomes the this-pointer argument to the constructor function, 3) calling the constructor, which should return its this pointer 4) performing the usual caller s-exit handshaking Note that steps 1 to 3 are just a modified version of the caller s-function-call handshaking. The constructor is made by turning the main block of the class into a function. It should: 1) do the usual callee-enter handshake 2) copy the parameters (not including the thisptr) from the frame stack into the record (thisptr) at the proper parameter offsets. 3) execute the main block s body code. 4) return the this pointer, 5) do the usual callee-exit handshake. Since the creation parameters are available to all functions nested in the class, we do step 2) above to ensure that these arguments are stored in the record for the class. This means that during semantic analysis we must allocate space for them in both the parameter scope for the function call to the main body, and in the class scope that gets made into the class record. The parameter scope need not enclose the class body scope, as these named variables can be referenced as part of the class record. Thus, we may place the parameter scope at any convenient node in the AST, perhaps at a ParameterListNode or something similar. Then, you can start a class scope (whatever you decide to call it) at the ClassDeclaration node, and enter the parameter definitions into both the parameter scope and the class scope. The class body scope then can be a subscope placed on the class s Body node.
7 The above is only one method that works; you are free to use others if you so desire. For instance, you could essentially return void from the creation code and use a saved value of the allocated record as the value for the creation expression. Or maybe you could allocate the new class record inside the creation-code call, in the callee-entrance handshake. 5. Class attribute tables Bunting-4 requires us to have information about each class type available at runtime. The two pieces of information we need are (1) a string name for the class (to use when printing the class), and (2) a list of all offsets in the class that contain a reference variable. The easiest way to do this is to have either one combined table (each element being a string pointer followed by a list pointer), or to have one table for the string pointers, and one for the list pointers. Either way is fine, but the combined table requires an extra offset to access (say) the list pointers. I ll give a few details of the separate-table idea. First, somewhere in RunTime, we need to place labelled strings in data memory, one for each class type. Suppose your program has three class types: Cat, Dog, and Rabbit, which have been allocated the typecodes 128, 129, and 130, respectively. Then you might issue the following ASM instructions to allocate the strings: DLabel $class-cat-string DataS Cat DLabel $class-dog-string DataS Dog DLabel $class-rabbit-string DataS Rabbit This would be done from a loop that loops over the defined class types, issuing a DLabel and and DataS for each. To create the table, you would issue something like: DLabel $class-names-table DataD $class-cat-string DataD $class-dog-string DataD $class-rabbit-string Then, to get the pointer to the name of the type of a record, take the record s typecode, and calculate the address $class-names-table + (typecode-128)*4 Where the 4 is the size of an entry in the table. This address contains the desired pointer. The idea for the list pointers is similar, where each class s list could be a negative-number terminated array of integers. For example, if Cat s list were to contain the numbers 4, 12, and 20, you d want something like: DLabel $class-cat-referenceoffsets DataI 4 DataI 12 DataI 20 DataI -1 We use a negative number here rather than zero because zero is a valid element for the offset list. You can use any other method you please to store these lists; you could for instance prefix the list with the number of elements rather than having an end sentinel: DLabel $class-cat-referenceoffsets
8 DataI 3 DataI 4 DataI 12 DataI Function invocations In the function invocation expression. identifier (expressionlist ) the expression must have a class type SomeClass that is a defined class, and identifier must be a inner function in SomeClass, with the argument types in the invocation matching the parameter types in the inner function. As stated above, we lambda-lift this invocation, treating it as if it were identifier (expressionlist, expression ) in order to implement the this pointer. Any function invocation without a member operator identifier ( expressionlist ) is treated as if it was a call to a inner function on this, viz.: this.identifier ( expressionlist ) and then lambda-lifting is applied. 7. The release statement, the user annotation, and hard release We now can use the keyword user to annotate any expression that creates a record. Using this notation will set the user-managed status bit of the created record to 1; otherwise it will be set to 0. The only way to release a record with the user-managed bit set is by having that record as one of the expressions in the release statement: release expressionlist ; Any expression in this immediately subjected to hard release (or explicit release). This is exactly like soft release, except that the user-managed bit is not checked in hard release, and the recursive releasing is hard. The recursive release is a little harder to implement, though. It involves going through the class s list of offsets that contain reference variables, and releasing each. Pseudocode for the release operation (hard or soft) becomes: if(classid == 0) done; if(classid == 9) { if(subtype-is-reference) { loop through array elements, releasing each
9 assert classid >= 128; classindex = classid-128; offsettable = offsettables[classindex]; for each offset in offsettable { release the element at that offset Aside from this complication in the recursion, the release mechanism for class records is the same as they were for arrays. Most of the release code that you already have should work with class records. 8. Break and continue statements These statements are only allowed inside the body of a while or for loop. This includes nested inside statements in the loop body. A break statement immediately jumps to the code immediately after the closest (most deeply nested) loop that contains it. A continue immediately jumps to the code for checking the condition on the closest loop. 9. Operator precedence The precedence of operators is Highest precedence (prefix unary operators are right-associative) parentheses populated array creation empty array creation class creation function invocation concatenation inner function access, array indexing not, copy, length, stringprinting casting : multiplicative operators * / additive operators + ( ) [ ] create[ ]( ) create ( ) ( ) [+ +]. [ ]! copy length $ comparisons < > <= >= ==!= and && Lowest precedence or These are all left-associative operators, except as noted.
1 Lexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler
More informationIntroduction to Programming Using Java (98-388)
Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;
More informationThe Decaf Language. 1 Lexical considerations
The Decaf Language In this course, we will write a compiler for a simple object-oriented programming language called Decaf. Decaf is a strongly-typed, object-oriented language with support for inheritance
More informationCSE 12 Abstract Syntax Trees
CSE 12 Abstract Syntax Trees Compilers and Interpreters Parse Trees and Abstract Syntax Trees (AST's) Creating and Evaluating AST's The Table ADT and Symbol Tables 16 Using Algorithms and Data Structures
More informationLexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler
More informationSFU CMPT 379 Compilers Spring 2018 Milestone 1. Milestone due Friday, January 26, by 11:59 pm.
SFU CMPT 379 Compilers Spring 2018 Milestone 1 Milestone due Friday, January 26, by 11:59 pm. For this assignment, you are to convert a compiler I have provided into a compiler that works for an expanded
More informationChapter 4 Defining Classes I
Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the
More informationLexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a
More informationIC Language Specification
CS 301 Spring 2016 IC Language Specification The IC Language For the implementation project, you will build a compiler for an object-oriented language called IC (for Irish Coffee 1 ), which is essentially
More informationSymbol Tables Symbol Table: In computer science, a symbol table is a data structure used by a language translator such as a compiler or interpreter, where each identifier in a program's source code is
More informationWeiss Chapter 1 terminology (parenthesized numbers are page numbers)
Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement
More informationThe SPL Programming Language Reference Manual
The SPL Programming Language Reference Manual Leonidas Fegaras University of Texas at Arlington Arlington, TX 76019 fegaras@cse.uta.edu February 27, 2018 1 Introduction The SPL language is a Small Programming
More informationChapter 6 Introduction to Defining Classes
Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of
More informationA Short Summary of Javali
A Short Summary of Javali October 15, 2015 1 Introduction Javali is a simple language based on ideas found in languages like C++ or Java. Its purpose is to serve as the source language for a simple compiler
More informationSemantic actions for declarations and expressions
Semantic actions for declarations and expressions Semantic actions Semantic actions are routines called as productions (or parts of productions) are recognized Actions work together to build up intermediate
More informationSemantic actions for declarations and expressions. Monday, September 28, 15
Semantic actions for declarations and expressions Semantic actions Semantic actions are routines called as productions (or parts of productions) are recognized Actions work together to build up intermediate
More informationThe Decaf language 1
The Decaf language 1 In this course, we will write a compiler for a simple object-oriented programming language called Decaf. Decaf is a strongly-typed, object-oriented language with support for inheritance
More informationShort Notes of CS201
#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system
More informationCS201 - Introduction to Programming Glossary By
CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with
More informationReference Grammar Meta-notation: hfooi means foo is a nonterminal. foo (in bold font) means that foo is a terminal i.e., a token or a part of a token.
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2002 Handout 7 Espresso Language Definition Wednesday, September 4 The project for the 18-unit
More informationProgramming Languages Third Edition. Chapter 7 Basic Semantics
Programming Languages Third Edition Chapter 7 Basic Semantics Objectives Understand attributes, binding, and semantic functions Understand declarations, blocks, and scope Learn how to construct a symbol
More informationCS 6353 Compiler Construction Project Assignments
CS 6353 Compiler Construction Project Assignments In this project, you need to implement a compiler for a language defined in this handout. The programming language you need to use is C or C++ (and the
More informationCSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:
Expression evaluation CSE 504 Order of evaluation For the abstract syntax tree + + 5 Expression Evaluation, Runtime Environments + + x 3 2 4 the equivalent expression is (x + 3) + (2 + 4) + 5 1 2 (. Contd
More informationDeclarations and Access Control SCJP tips
Declarations and Access Control www.techfaq360.com SCJP tips Write code that declares, constructs, and initializes arrays of any base type using any of the permitted forms both for declaration and for
More informationReview of the C Programming Language for Principles of Operating Systems
Review of the C Programming Language for Principles of Operating Systems Prof. James L. Frankel Harvard University Version of 7:26 PM 4-Sep-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights
More informationCS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation
CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation Assigned: Sunday, November 14, 2004 Due: Thursday, Dec 9, 2004, at 11:59pm No solution will be accepted after Sunday, Dec 12,
More informationDecaf Language Reference Manual
Decaf Language Reference Manual C. R. Ramakrishnan Department of Computer Science SUNY at Stony Brook Stony Brook, NY 11794-4400 cram@cs.stonybrook.edu February 12, 2012 Decaf is a small object oriented
More informationData Structures (list, dictionary, tuples, sets, strings)
Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in brackets: l = [1, 2, "a"] (access by index, is mutable sequence) Tuples are enclosed in parentheses: t = (1, 2, "a") (access
More informationToday's Topics. CISC 458 Winter J.R. Cordy
Today's Topics Last Time Semantics - the meaning of program structures Stack model of expression evaluation, the Expression Stack (ES) Stack model of automatic storage, the Run Stack (RS) Today Managing
More informationCOP 3330 Final Exam Review
COP 3330 Final Exam Review I. The Basics (Chapters 2, 5, 6) a. comments b. identifiers, reserved words c. white space d. compilers vs. interpreters e. syntax, semantics f. errors i. syntax ii. run-time
More informationJava Primer 1: Types, Classes and Operators
Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,
More informationMARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012
MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)
More informationThe PCAT Programming Language Reference Manual
The PCAT Programming Language Reference Manual Andrew Tolmach and Jingke Li Dept. of Computer Science Portland State University September 27, 1995 (revised October 15, 2002) 1 Introduction The PCAT language
More informationLecture Overview Code generation in milestone 2 o Code generation for array indexing o Some rational implementation Over Express Over o Creating
1 ecture Overview Code generation in milestone 2 o Code generation for array indexing o Some rational implementation Over Express Over o Creating records for arrays o Short-circuiting Or o If statement
More informationProcedure and Object- Oriented Abstraction
Procedure and Object- Oriented Abstraction Scope and storage management cs5363 1 Procedure abstractions Procedures are fundamental programming abstractions They are used to support dynamically nested blocks
More informationOverview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017
Overview of OOP Dr. Zhang COSC 1436 Summer, 2017 7/18/2017 Review Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in square brackets: l = [1, 2, "a"] (access by index, is mutable
More informationRun-Time Data Structures
Run-Time Data Structures Static Structures For static structures, a fixed address is used throughout execution. This is the oldest and simplest memory organization. In current compilers, it is used for:
More informationCS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11
CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11 CS 536 Spring 2015 1 Handling Overloaded Declarations Two approaches are popular: 1. Create a single symbol table
More informationLecture 16: Static Semantics Overview 1
Lecture 16: Static Semantics Overview 1 Lexical analysis Produces tokens Detects & eliminates illegal tokens Parsing Produces trees Detects & eliminates ill-formed parse trees Static semantic analysis
More informationCS 231 Data Structures and Algorithms, Fall 2016
CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard
More informationSemantic actions for declarations and expressions
Semantic actions for declarations and expressions Semantic actions Semantic actions are routines called as productions (or parts of productions) are recognized Actions work together to build up intermediate
More informationReview of the C Programming Language
Review of the C Programming Language Prof. James L. Frankel Harvard University Version of 11:55 AM 22-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Reference Manual for the
More informationCS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find
CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS
More informationTokens, Expressions and Control Structures
3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type
More informationG Programming Languages - Fall 2012
G22.2110-003 Programming Languages - Fall 2012 Lecture 2 Thomas Wies New York University Review Last week Programming Languages Overview Syntax and Semantics Grammars and Regular Expressions High-level
More informationStatic Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1
Static Semantics Lecture 15 (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1 Current Status Lexical analysis Produces tokens Detects & eliminates illegal tokens Parsing
More informationZhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson
Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Introduction History, Characteristics of Java language Java Language Basics Data types, Variables, Operators and Expressions Anatomy of a Java Program
More informationStatic Semantics. Winter /3/ Hal Perkins & UW CSE I-1
CSE 401 Compilers Static Semantics Hal Perkins Winter 2009 2/3/2009 2002-09 Hal Perkins & UW CSE I-1 Agenda Static semantics Types Symbol tables General ideas for now; details later for MiniJava project
More informationJava: introduction to object-oriented features
Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer
More informationIntermediate Code Generation
Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target
More informationObject-Oriented Programming
Object-Oriented Programming 1. What is object-oriented programming (OOP)? OOP is a technique to develop logical modules, such as classes that contain properties, methods, fields, and events. An object
More informationDOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS
Chapter 1 : Chapter-wise Java Multiple Choice Questions and Answers Interview MCQs Java Programming questions and answers with explanation for interview, competitive examination and entrance test. Fully
More informationCS 314 Principles of Programming Languages. Lecture 13
CS 314 Principles of Programming Languages Lecture 13 Zheng Zhang Department of Computer Science Rutgers University Wednesday 19 th October, 2016 Zheng Zhang 1 CS@Rutgers University Class Information Reminder:
More informationProperties of an identifier (and the object it represents) may be set at
Properties of an identifier (and the object it represents) may be set at Compile-time These are static properties as they do not change during execution. Examples include the type of a variable, the value
More informationJava Object Oriented Design. CSC207 Fall 2014
Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code
More informationProgramming Languages Third Edition. Chapter 10 Control II Procedures and Environments
Programming Languages Third Edition Chapter 10 Control II Procedures and Environments Objectives Understand the nature of procedure definition and activation Understand procedure semantics Learn parameter-passing
More informationSEMANTIC ANALYSIS TYPES AND DECLARATIONS
SEMANTIC ANALYSIS CS 403: Type Checking Stefan D. Bruda Winter 2015 Parsing only verifies that the program consists of tokens arranged in a syntactically valid combination now we move to check whether
More informationCPS311 Lecture: Procedures Last revised 9/9/13. Objectives:
CPS311 Lecture: Procedures Last revised 9/9/13 Objectives: 1. To introduce general issues that any architecture must address in terms of calling/returning from procedures, passing parameters (including
More informationCLASSES AND OBJECTS IN JAVA
Lesson 8 CLASSES AND OBJECTS IN JAVA (1) Which of the following defines attributes and methods? (a) Class (b) Object (c) Function (d) Variable (2) Which of the following keyword is used to declare Class
More informationLecture 7: Type Systems and Symbol Tables. CS 540 George Mason University
Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University Static Analysis Compilers examine code to find semantic problems. Easy: undeclared variables, tag matching Difficult: preventing
More informationCpt S 122 Data Structures. Introduction to C++ Part II
Cpt S 122 Data Structures Introduction to C++ Part II Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Objectives Defining class with a member function
More informationContents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix
PGJC4_JSE8_OCA.book Page ix Monday, June 20, 2016 2:31 PM Contents Figures Tables Examples Foreword Preface xix xxi xxiii xxvii xxix 1 Basics of Java Programming 1 1.1 Introduction 2 1.2 Classes 2 Declaring
More informationCompiler Theory. (Semantic Analysis and Run-Time Environments)
Compiler Theory (Semantic Analysis and Run-Time Environments) 005 Semantic Actions A compiler must do more than recognise whether a sentence belongs to the language of a grammar it must do something useful
More informationCA Compiler Construction
CA4003 - Compiler Construction David Sinclair When procedure A calls procedure B, we name procedure A the caller and procedure B the callee. A Runtime Environment, also called an Activation Record, is
More informationCS260 Intro to Java & Android 03.Java Language Basics
03.Java Language Basics http://www.tutorialspoint.com/java/index.htm CS260 - Intro to Java & Android 1 What is the distinction between fields and variables? Java has the following kinds of variables: Instance
More informationContents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance
Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass
More informationA declaration may appear wherever a statement or expression is allowed. Limited scopes enhance readability.
Scope vs. Lifetime It is usually required that the lifetime of a run-time object at least cover the scope of the identifier. That is, whenever you can access an identifier, the run-time object it denotes
More informationCS 2210 Programming Project (Part IV)
CS 2210 Programming Project (Part IV) April 25, 2018 Code Generation This project is intended to give you experience in writing a code generator as well as bring together the various issues of code generation
More informationCS143 Handout 03 Summer 2012 June 27, 2012 Decaf Specification
CS143 Handout 03 Summer 2012 June 27, 2012 Decaf Specification Written by Julie Zelenski and updated by Jerry Cain and Keith Schwarz. In this course, we will write a compiler for a simple object oriented
More informationUNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger
UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division CS 164 Spring 2005 P. N. Hilfinger Project #2: Static Analyzer for Pyth Due: Wednesday, 6 April
More informationWeeks 6&7: Procedures and Parameter Passing
CS320 Principles of Programming Languages Weeks 6&7: Procedures and Parameter Passing Jingke Li Portland State University Fall 2017 PSU CS320 Fall 17 Weeks 6&7: Procedures and Parameter Passing 1 / 45
More information2. Reachability in garbage collection is just an approximation of garbage.
symbol tables were on the first exam of this particular year's exam. We did not discuss register allocation in this This exam has questions from previous CISC 471/672. particular year. Not all questions
More informationQUIZ. What are 3 differences between C and C++ const variables?
QUIZ What are 3 differences between C and C++ const variables? Solution QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Solution The C/C++ preprocessor substitutes mechanically,
More informationEL6483: Brief Overview of C Programming Language
EL6483: Brief Overview of C Programming Language EL6483 Spring 2016 EL6483 EL6483: Brief Overview of C Programming Language Spring 2016 1 / 30 Preprocessor macros, Syntax for comments Macro definitions
More informationType Checking Binary Operators
Type Checking Binary Operators binaryopnode expr tree expr tree Type checking steps: 1. Type check left and right operands. 2. Check that left and right operands are both scalars. 3. binaryopnode.kind
More informationCS-201 Introduction to Programming with Java
CS-201 Introduction to Programming with Java California State University, Los Angeles Computer Science Department Lecture IX: Methods Introduction method: construct for grouping statements together to
More informationThe Procedure Abstraction
The Procedure Abstraction Procedure Abstraction Begins Chapter 6 in EAC The compiler must deal with interface between compile time and run time Most of the tricky issues arise in implementing procedures
More information3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.
3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.
More informationFundamental Concepts and Definitions
Fundamental Concepts and Definitions Identifier / Symbol / Name These terms are synonymous: they refer to the name given to a programming component. Classes, variables, functions, and methods are the most
More informationDeclaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:
Declarations Based on slides from K. N. King Declaration Syntax General form of a declaration: declaration-specifiers declarators ; Declaration specifiers describe the properties of the variables or functions
More informationProject Compiler. CS031 TA Help Session November 28, 2011
Project Compiler CS031 TA Help Session November 28, 2011 Motivation Generally, it s easier to program in higher-level languages than in assembly. Our goal is to automate the conversion from a higher-level
More informationCompilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam
Compilers Type checking Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Summary of parsing Parsing A solid foundation: context-free grammars A simple parser: LL(1) A more powerful parser:
More informationLecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]
1 Lecture Overview Types 1. Type systems 2. How to think about types 3. The classification of types 4. Type equivalence structural equivalence name equivalence 5. Type compatibility 6. Type inference [Scott,
More information(Not Quite) Minijava
(Not Quite) Minijava CMCS22620, Spring 2004 April 5, 2004 1 Syntax program mainclass classdecl mainclass class identifier { public static void main ( String [] identifier ) block } classdecl class identifier
More informationCHAPTER 7 OBJECTS AND CLASSES
CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and
More informationArgument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.
Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar
More informationLanguage Reference Manual simplicity
Language Reference Manual simplicity Course: COMS S4115 Professor: Dr. Stephen Edwards TA: Graham Gobieski Date: July 20, 2016 Group members Rui Gu rg2970 Adam Hadar anh2130 Zachary Moffitt znm2104 Suzanna
More informationCS321 Languages and Compiler Design I. Winter 2012 Lecture 2
CS321 Languages and Compiler Design I Winter 2012 Lecture 2 1 A (RE-)INTRODUCTION TO JAVA FOR C++/C PROGRAMMERS Why Java? Developed by Sun Microsystems (now Oracle) beginning in 1995. Conceived as a better,
More informationCE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings
CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings 19/10/2017 CE221 Part 2 1 Variables and References 1 In Java a variable of primitive type is associated with a memory location
More informationG Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University
G22.2110-001 Programming Languages Spring 2010 Lecture 4 Robert Grimm, New York University 1 Review Last week Control Structures Selection Loops 2 Outline Subprograms Calling Sequences Parameter Passing
More informationQUIZ. What is wrong with this code that uses default arguments?
QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code
More informationPreface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS
Contents Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS 1.1. INTRODUCTION TO COMPUTERS... 1 1.2. HISTORY OF C & C++... 3 1.3. DESIGN, DEVELOPMENT AND EXECUTION OF A PROGRAM... 3 1.4 TESTING OF PROGRAMS...
More informationConcepts Introduced in Chapter 7
Concepts Introduced in Chapter 7 Storage Allocation Strategies Static Stack Heap Activation Records Access to Nonlocal Names Access links followed by Fig. 7.1 EECS 665 Compiler Construction 1 Activation
More informationSMURF Language Reference Manual Serial MUsic Represented as Functions
SMURF Language Reference Manual Serial MUsic Represented as Functions Richard Townsend, Lianne Lairmore, Lindsay Neubauer, Van Bui, Kuangya Zhai {rt2515, lel2143, lan2135, vb2363, kz2219}@columbia.edu
More informationDecaf Language Reference
Decaf Language Reference Mike Lam, James Madison University Fall 2016 1 Introduction Decaf is an imperative language similar to Java or C, but is greatly simplified compared to those languages. It will
More informationCS-202 Introduction to Object Oriented Programming
CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction
More informationJava How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.
Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;
More informationG Programming Languages - Fall 2012
G22.2110-003 Programming Languages - Fall 2012 Lecture 4 Thomas Wies New York University Review Last week Control Structures Selection Loops Adding Invariants Outline Subprograms Calling Sequences Parameter
More informationJava+- Language Reference Manual
Fall 2016 COMS4115 Programming Languages & Translators Java+- Language Reference Manual Authors Ashley Daguanno (ad3079) - Manager Anna Wen (aw2802) - Tester Tin Nilar Hlaing (th2520) - Systems Architect
More informationAnatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?
Anatomy of a Compiler Program (character stream) Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Semantic Analysis Parse Tree Intermediate Code Generator Intermediate Code Optimizer Code Generator
More information