Definition of DJ (Diminished Java)

Similar documents
Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Java Bytecode (binary file)

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Compaq Interview Questions And Answers

Declarations and Access Control SCJP tips

INHERITANCE. Spring 2019

Java Primer 1: Types, Classes and Operators

Introduction to Programming Using Java (98-388)

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS260 Intro to Java & Android 03.Java Language Basics

Rules and syntax for inheritance. The boring stuff

Lexical Considerations

Class, Variable, Constructor, Object, Method Questions

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

A Short Summary of Javali

IC Language Specification

Chapter 5 Object-Oriented Programming

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

Pace University. Fundamental Concepts of CS121 1

Object Oriented Programming. Java-Lecture 11 Polymorphism

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Java+- Language Reference Manual

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

15CS45 : OBJECT ORIENTED CONCEPTS

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

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

Java Overview An introduction to the Java Programming Language

Points To Remember for SCJP

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

FRAC: Language Reference Manual

COP 3330 Final Exam Review

3. Java - Language Constructs I

Lexical Considerations

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Brief Summary of Java

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

1 Lexical Considerations

CLASS DESIGN. Objectives MODULE 4

More on Objects in JAVA TM

Java Fundamentals (II)

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Arrays Classes & Methods, Inheritance

C# Programming for Developers Course Labs Contents

An overview of Java, Data types and variables

Compiler Errors. Flash CS4 Professional ActionScript 3.0 Language Reference. 1 of 18 9/6/2010 9:40 PM

Introduction to C# Applications

Java Programming Tutorial 1

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

Inheritance (Part 5) Odds and ends

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

JAVA MOCK TEST JAVA MOCK TEST II

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS


Decaf Language Reference Manual

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

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

CS-202 Introduction to Object Oriented Programming

Practice for Chapter 11

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

OBJECT ORİENTATİON ENCAPSULATİON

CMSC 132: Object-Oriented Programming II

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

The Decaf Language. 1 Lexical considerations

Java Object Oriented Design. CSC207 Fall 2014

Project 6 Due 11:59:59pm Thu, Dec 10, 2015

CSc Introduction to Computing

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

The MaSH Programming Language At the Statements Level

Full file at

Inheritance and Polymorphism

Program Fundamentals

Inheritance. CSE 142, Summer 2002 Computer Programming 1.

1 Shyam sir JAVA Notes

Typed Scheme: Scheme with Static Types

Lecture 3. Lecture

Language Fundamentals Summary

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Lecture Notes on Programming Languages

2 rd class Department of Programming. OOP with Java Programming

Decaf Language Reference

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Lecture 2 Tao Wang 1

Polymorphism. return a.doublevalue() + b.doublevalue();

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Transcription:

Definition of DJ (Diminished Java) version 0.5 Jay Ligatti 1 Introduction DJ is a small programming language similar to Java. DJ has been designed to try to satisfy two opposing goals: 1. DJ is a complete object-oriented programming language (OOPL): (a) DJ includes all the core features of OOPLs like Java, and (b) you can express any algorithm in DJ (more precisely, DJ is Turing complete; any Turing machine can be encoded as a DJ program). 2. DJ is simple, with only core features included. DJ can therefore be compiled straightforwardly; we can design and implement a working (non-optimizing but otherwise complete) DJ compiler in one semester. 2 An Introductory Example Here is a valid DJ program: // This DJ program outputs the sum 1 + 2 +... + 100 class Summer extends Object { // This method returns the sum 0 + 1 +.. + n nat sum(nat n) { nat toreturn; // note: nat variables automatically get initialized to 0 while(n>0) { toreturn = toreturn + n; n = n - 1; ; toreturn; main { // create a new object of type Summer Summer s; s = new Summer(); // print the sum 0 + 1 +... + 100 printnat( s.sum(100) ); Many additional examples of valid and invalid DJ programs are posted at: http://www.cse.usf.edu/~ligatti/compilers-15/as1/dj/. 1

3 Format of DJ Programs A DJ program must be contained in a single file that begins with a (possibly empty) sequence of class declarations and then must have a main block. A class declaration consists of an optional final keyword, then the class keyword, then a class name, then the extends keyword, then a superclass s name, then an open brace {, then a (possibly empty) sequence of variable declarations, then a (possibly empty) sequence of method declarations, and then a closing brace. A variable declaration consists of a type name (either nat for a natural number, or a class name for an object type) followed by a variable name followed by a semicolon. For example, nat i; declares a variable i of type nat. A method declaration consists of an optional final keyword, then a return type name, then a method name, then a left parenthesis (, then a parameter type name, then a parameter name, then a right parenthesis ), and then a variable-expression block. A variable-expression block consists of an open brace { followed by a (possibly empty) sequence of variable declarations followed by a nonempty sequence of expressions (with each expression followed by a semicolon) followed by a closing brace. A main block consists of the main keyword followed by a variableexpression block. An expression can be any of, but only, the following: A plus expression (expression1 + expression2). A minus expression (expression1 expression2). A times expression (expression1 * expression2). An equality test (expression1 == expression2). A greater-than test (expression1 > expression2). An assertion having the form assert expression1. A not operator (!expression1). An or operator (expression1 expression2). A natural number (0, 1, 2,...). The keyword null. An if-then-else expression having the form if(expression1) {expression-list1 else {expression-list2, where expressionlist1 and expression-list2 are nonempty sequences of expressions (with each expression followed by a semicolon). 2

A while-loop expression having the form while(expression1) {expression-list, where again, expression-list is a nonempty sequence of expressions (with each expression followed by a semicolon). A constructor expression having the form new Classname(). For example, new Summer() causes memory to be dynamically allocated and initialized for storing a Summer object. A this-object expression. As in Java, the keyword this in a method m refers to the object on which m was invoked. A print-natural-number expression: printnat(expression1). A read-natural-number expression: readnat(). An identifier id (e.g., a variable name). A dotted identifier having the form expression1.id, where id is a field of whatever object expression1 evaluates to. An undotted assignment having the form id = expression1. A dotted assignment of the form expression1.id = expression2. An undotted method call of the form id(expression1). A dotted method call of the form expression1.id(expression2). An expression inside a pair of parentheses: (expression1). Finally, comments may appear anywhere in a DJ program. A comment begins with two slashes (//). Anything to the right of the slashes on the same line is considered a comment and is ignored. Again, you can find many example DJ programs illustrating this format at: http://www.cse.usf.edu/~ligatti/compilers-15/as1/dj/ 4 Key Differences between DJ and Java Programs: In DJ, semicolons must appear after every expression in expression sequences. Semicolons must even appear after while loops and if-then-else expressions. The example program above (in Section 2) illustrates this requirement with a semicolon after a while loop. In DJ, all field declarations in a class must appear before any method declaration; similarly, all variable declarations in a variable-expression block must appear before any expressions. The main block in a DJ program is not a method and cannot be invoked. DJ has no type for Booleans; we use natural numbers (i.e., 0, 1, 2,...) in place of Booleans in if-then-else expressions. The natural number 0 gets interpreted as false, and everything else gets interpreted as true. DJ has no explicit return keyword. The example code in Section 2 illustrates how DJ uses the final expression in a method body to determine the return value. 3

All DJ methods must take exactly one argument and return exactly one result. DJ classes have no constructor methods. DJ does have a builtin new expression, though: calling new C() creates a new object of type C having default values for all of its fields (the default value for natural-number fields is 0, and the default for object fields is null). DJ has no explicit void or array types and does not support type casting. The only types one can explicitly write in DJ are nat and object types. Natural numbers can be input and output using the built-in readnat and printnat functions. DJ requires all if expressions to have both then and else branches. For example, if(true) {1; else {2; is a valid DJ expression, but if(true) {1; is not. Only classes and methods (not variables) may be declared to be final in DJ. DJ has no notion of super, import, public, private, abstract, try, catch, throw, package, synchronized, etc. It lacks all these keywords. DJ does not allow comments of the style /* */. 5 Additional Notes Case sensitivity Keywords and identifiers are case sensitive (i.e., case matters, so Class is not the same as class ). Identifiers Identifiers (which are used for naming classes, fields, methods, parameters, and local variables) must begin with a letter or an underscore character and must contain only digits (0-9), underscores, and ASCII upper- and lower-case English letters. Natural-number literals All numbers in DJ programs have nat type and must be natural numbers (0, 1, 2,...). Naturals may not have leading zeroes in DJ; e.g., 0 is a valid nat but 005 is not a valid nat (technically, 005 would be interpreted as three separate natural numbers). The Object Class A class called Object is always assumed to exist. Class Object is unique in that it extends no other class. Also, class Object is empty; it contains no members (neither fields nor methods). Recursion Methods and classes may be (mutually) recursive. A class C1 may define a variable field of type C2, while class C2 defines a 4

variable field of type C1 (these are called mutually recursive classes). Data Initialization All natural-number variables and fields get initialized to 0, and all object variables and fields to null. Inheritance As in Java, classes inherit all fields and methods in superclasses. In DJ, subclasses may override non-final methods, but not variable fields, defined in superclasses. For example, if class C1 has a variable field v1 and class C2 extends C1, then C2 may not declare any variable fields named v1. A subclass may override a superclass s non-final method only when the overriding and overridden methods have identical parameter and return types (though the overriding method s parameter name may differ from that of the overridden method). For example, if class C1 has a method m and class C2 extends C1, then C2 may declare a method m iff its parameter and return types match those of method m in class C1. However, final classes may not be subclassed, and final methods may not be overridden. How DJ programs evaluate DJ programs basically evaluate according to the rules for evaluating Java programs, with a few differences: printnat expressions evaluate to (and return) whatever natural number gets printed. readnat expressions evaluate to (and return) whatever natural number gets read. while loops, upon completion, always evaluate to (and return) the value 0. assert expressions, if successful, evaluate to (and return) whatever nonzero value was asserted. Unsuccessful assert expressions (e.g., assert 0) cause the program to terminate. When the then branch of an if-then-else expression is taken, the entire if-then-else expression evaluates to whatever the then branch evaluates to. Similarly, when the else branch of an if-then-else expression is taken, the entire if-then-else expression evaluates to whatever the else branch evaluates to. Expression lists evaluate to whatever value the final expression in the list evaluates to. Dynamic (i.e., virtual) method calls As in Java, the exact code that gets executed during a method invocation depends on the run-time type of the calling object. For instance, the following DJ program outputs 2 because testobj has run-time type C2. 5

class C1 extends Object { nat callwhoami(nat unused) {this.whoami(0); nat whoami(nat unused) {printnat(1); class C2 extends C1 { nat whoami(nat unused) {printnat(2); main { C1 testobj; testobj = new C2(); testobj.callwhoami(0); Assignment Expressions As in Java, DJ programs can make assignments to object-type variables. For example, the expression obj1=obj2 causes the obj1 variable to alias (i.e., point to the same object as) the obj2 variable. Typing Rules The typing rules for DJ also basically match those of Java. Beyond the normal Java restrictions, DJ requires that: The only types available to programmers are nat and object types. All class names must be unique. All method and field names within the same class must be unique. Although a subclass can override superclass methods, a subclass cannot override superclass variable fields. The then and else blocks in an if-then-else expression must have the same type. Boolean tests in the if part of an if-then-else expression must have nat type (nonzero is used for true and zero is used for false). Similarly, equality (==), or ( ), greater-than (>), and not (!) expressions all have nat (rather than boolean) type. A well-typed while loop has nat type (recall that it evaluates to 0 upon completion). A well-typed assertion expression has nat type because successful assertions return (nonzero) numbers. Also, only nat-type expressions can be asserted. printnat and readnat expressions have nat type because they evaluate to whatever number gets printed or read at run time. 6