(Not Quite) Minijava

Similar documents
A Short Summary of Javali

IC Language Specification

1 Lexical Considerations

Lexical Considerations

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Lexical Considerations

The Decaf Language. 1 Lexical considerations

Operators. Java operators are classified into three categories:

Introduction to Programming Using Java (98-388)

Lecture 4 Abstract syntax

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

CA4003 Compiler Construction Assignment Language Definition

Lecture 3 Tao Wang 1

The Decaf language 1

Review of the C Programming Language for Principles of Operating Systems

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Reference 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.

6. Operatoren. 7. Safe Programming: Assertions. Table of Operators. Table of Operators - Explanations. Tabular overview of all relevant operators

The SPL Programming Language Reference Manual

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

1. Every program must have at least one class declaration. (*) 2. Every class declared in a program must have a distinct identifier.

GAWK Language Reference Manual

Java Primer 1: Types, Classes and Operators

Review of the C Programming Language

The Warhol Language Reference Manual

Decaf Language Reference

CS143 Handout 03 Summer 2012 June 27, 2012 Decaf Specification

Decaf Language Reference Manual

Reference 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.

The pixelman Language Reference Manual. Anthony Chan, Teresa Choe, Gabriel Kramer-Garcia, Brian Tsau

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Quick Reference Guide

Typescript on LLVM Language Reference Manual

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Decision Making in C

CS313D: ADVANCED PROGRAMMING LANGUAGE

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

X Language Definition

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

CGS 3066: Spring 2015 JavaScript Reference

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

The PCAT Programming Language Reference Manual

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Operators and Expressions

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

JFlat Reference Manual

M/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567)

Following is the general form of a typical decision making structure found in most of the programming languages:

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Language Reference Manual simplicity

C++ Programming: From Problem Analysis to Program Design, Third Edition

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Functional Programming. Pure Functional Programming

Chapter 4 - Notes Control Structures I (Selection)

4. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

Java+- Language Reference Manual

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Topics. Chapter 5. Equality Operators

Chapter 2: Basic Elements of C++

Intermediate Code Generation

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

MIT Semantic Analysis. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Chapter 4. Flow of Control

MIT Semantic Analysis. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

Chapter 2: Using Data

Operational Semantics of Cool

GBL Language Reference Manual

CS2255 HOMEWORK #1 Fall 2012

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

CSE 401 Midterm Exam Sample Solution 11/4/11

3. Logical Values. Boolean Functions; the Type bool; logical and relational operators; shortcut evaluation

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Object Oriented Software Design

Lecture Set 4: More About Methods and More About Operators

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

LOON. Language Reference Manual THE LANGUAGE OF OBJECT NOTATION. Kyle Hughes, Jack Ricci, Chelci Houston-Borroughs, Niles Christensen, Habin Lee

Java Basic Programming Constructs

Java Bytecode (binary file)

Acknowledgement. CS Compiler Design. Semantic Processing. Alternatives for semantic processing. Intro to Semantic Analysis. V.

CS4120/4121/5120/5121 Spring 2016 Xi Language Specification Cornell University Version of May 11, 2016

3. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

MATLIP: MATLAB-Like Language for Image Processing

Transcription:

(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 (extends identifier) opt { instvardecl methoddecl } instvardecl public opt type var (, var) ; var identifier ([]) methoddecl public opt rtyp identifier ( formal (, formal) ) block rtyp type void formal type var block { (locvardecl ; statement) } locvardecl type locvar (, locvar) opt locvar var (= init) opt init exp { init (, init) } statement block if ( exp ) statement (else statement) opt while ( exp ) statement do statement while ( exp ) ; for ( forinit ; exp opt ; explist opt ) statement exp ; return exp opt ; ; identifier : statement break identifier opt ; continue identifier opt ; forinit explist opt locvardecl 1

explist exp (, exp) exp exp binop exp (exp.) opt identifier ( explist opt ) integer true false this null new identifier ( ) new type [ exp ] uop exp exp? exp : exp ( exp ) lvalue lvalue = exp binop && < > <= >= ==!= + - * uop! - lvalue identifier exp. identifier exp [ exp ] type int boolean identifier type [] identifier... integer... 2 Classes and subtyping The language has no overloading; each class has at most one method by any given name. Let B extend A. If A has a method f and B wants to override f with its own version, then the type of B s f must be a subtype of A s f. For f s type to be a subtype of g s type, f and g must take equal number of arguments. If f has a result, then so must g (and vice versa), and the type of f s result must be a subtype of the type of g s result. Moreover, the type of each of g s arguments must be a subtype of the type of the corresponding argument of f. (Notice the role reversal here! This is called contravariance.) A class type B is a subtype of another class type A if B directly or indirectly extends A. Otherwise, an array type t[] is a subtype of another array type u[] if and only if t is a subtype of u. (This is, in some sense, a design bug in the Java language since it does not guarantee safety. To restore safety, assignment to arrays often require runtime type tests.) 3 Expressions constants Minijava programs can use the following constants: boolean true, false numerical integer array and object null 2

this refers to the current object the object o in the method call o.f(e 1,..., e k ) that invoked the current method identifier an identifier n refers to the variable n that is currently in scope; if n is an instance variable (and therefore declared in the class corresponding to the current object), then n is equivalent to this.n; this construct is an lvalue and therefore can appear on the left-hand side of the assignment operator selection the syntax o.n denotes access to an instance variable n in object o; which (of possibly many) instance variables by the name n is meant depends on the compile-time type of expression o; this construct is an lvalue array length the syntax a.length refers to the length (number of elements) of an array a; although syntactically identical to selection and therefore an lvalue, this construct is read-only array subscript the syntax a[i] refers to the i-th element of array a; this construct is an lvalue self method call the syntax f(e 1,..., e k ) (where f is an identifier) is equivalent to this.f(e 1,..., e k ) method invocation the syntax o.f(e 1,..., e k ) invokes the method named f in object o; which (of possibly many) methods by the name f is meant depends on the class that o was created from, i.e., the runtime type of o object creation an expression of the form new c() returns a freshly allocated object of runtime type c (c must be an identifier referring to a class); all instance variables of the object are cleared (integers become 0, booleans become false, arrays and objects become null) array creation an expression of the form new t[l] returns a freshly allocated array whose elements are of type t and whose size (number of elements) is l; all elements are cleared (integers become 0, booleans become false, arrays and objects become null) binary operations In general, binary operations have the form e 1 e 2 where is one of: short-circuiting logical and: boolean arguments and results short-circuiting logical or: && boolean arguments and results equality tests: ==!= arbitrary (matching) argument types, boolean result; in case of arrays and objects the test is for object identity (memory address equality) comparisons: < > <= >= integer operands, boolean result addition and subtraction: + - integer operands and result multiplication: * integer operands and result 3

These operators are listed in order of increasing precedence. unary operations There are two unary operations: boolean negation:!e boolean argument, boolean result arithmetic negation: -e integer argument, integer result conditional expression An expression of the form e 1?e 2 :e 3 evaluates the boolean condition e 1 and depending on the outcome evaluates either e 2 (when true) or e 3 (when false) and returns the respective value; the expression that is not needed does not get evaluated; notice that e 1 &&e 2 and e 1 e 2 are equivalent to e 1?e 2 :false and e 1?true:e 2, respectively. assignment l=e assigns the value of e into the location denoted by lvalue l; the result (which is the value that was assigned) has the same type as l. 4 Statements blocks Blocks are sequences of statements and variable declarations enclosed in curly braces {}. The scope of each variable declaration extends from the point of declaration until the end of the block. conditional The conditional statement has a condition, a then branch, and an optional else branch. It works like in C or Java. while loop The while-loop has a condition and a body. It alternates between evaluating the condition and executing the statement (beginning with the condition) as long as the condition is found to be true. If during the execution of the loop a continue statement is executed, then control is passed directly directly to the condition, thus starting a new round. do loop The do-loop is like the while-loop, except execution starts with the body, so the body gets executed at least once. A continue statement jumps back to the beginning of the body, thus starting a new round. for loop The for-loop consists of an optional initializer, an optional condition, an optional update part, and a body. The initializer is evaluated once, possibly declaring some loop-local variables which are available in all three other parts. Then the loop starts executing beginning with the condition followed by the body, then the update part (if present), and finally back to the condition. A missing condition is treated as true. The update part consists of a list of expressions which get evaluated for effect. (Usually these are used to update loop variables originally declared or initialized by the initializer.) The loop stops after the condition is found to be false for the first time. A continue statement jumps back to the update part, thus starting a new round. expression An expression can be used where a statement is expected by simply following it with a semicolon. 4

return The return statement terminates the current method. If the return type of the method is void, then return does not accept an expression. Otherwise return requires an expression which is used to specify the return value of the method. empty A semicolon by itself is an empty statement which does nothing. labeled Any statement s can follow l: where l is the name of a label. The label can be used by break- and continue-statements within s to refer to s. break A break-statement without a label terminates the innermost enclosing loop (while, do, or for) within the current method. (Such a loop must exist for the break to be legal.) A break-statement carrying a label l terminates the innermost enclosing statement s that is labeled with l. If there is no such statement, then break l; is illegal. continue All continue-statements refer to an enclosing loop statement within the current method. If no label was specified, that loop is the innermost enclosing loop (which has to exist); if label l was given, the statement refers to the innermost enclosing loop labeled with l (which has to exist). The precise semantics of continue depends on the kind of loop it refer to. See the description of while, do, and for for details. 5