Prof. Fateman CS 164 Lecture 41 Overview of MiniJava Lecture 4.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Languages and Paradigms
Principles of programming languages 1: Introduction (with a simple language) Isao Sasano Department of Information Science and Engineering.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Names and Bindings.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Review of C++ Programming Part II Sheng-Fang Huang.
Language Evaluation Criteria
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Imperative Programming
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Adapted from Prof. Necula UCB CS 1641 Overview of COOL ICOM 4029 Lecture 2 ICOM 4029 Fall 2008.
CS 330 Programming Languages 09 / 04 / 2008 Instructor: Michael Eckmann.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Compiler Construction Dr. Noam Rinetzky and Orr Tamir School of Computer Science Tel Aviv University
Prof. Fateman CS 164 Lecture 111 Syntax  Simple Semantics Lecture 11.
Prof. Fateman CS 164 Lecture 281 Virtual Machine Structure Lecture 28.
Introduction CPSC 388 Ellen Walker Hiram College.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
CSE 413 Languages & Implementation Hal Perkins Autumn 2012 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1.
Programming Languages and Design Lecture 6 Names, Scopes and Binding Instructor: Li Ma Department of Computer Science Texas Southern University, Houston.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
CMSC 330: Organization of Programming Languages Operational Semantics.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Object Lifetime and Pointers
Chapter VII: Arrays.
Names and Attributes Names are a key programming language feature
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Information Science and Engineering
Introduction to Computer Science / Procedural – 67130
Syntax Simple Semantics
课程名 编译原理 Compiling Techniques
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE401 Introduction to Compiler Construction
Representation, Syntax, Paradigms, Types
CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics
Java Programming Language
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
ICOM 4029 Fall 2003 Lecture 2 (Adapted from Prof. Necula UCB CS 164)
Corresponds with Chapter 5
Presentation transcript:

Prof. Fateman CS 164 Lecture 41 Overview of MiniJava Lecture 4

Prof. Fateman CS 164 Lecture 42 Course Administration If you drop the course, please make it official If you are on the waiting list, see Michael- David Sasson in 379 Soda Hall I think that anyone enrolled can get ID card registered to get into Soda Hall after hours. –Go to CS reception (Soda Hall 3rd floor) Questions about course policies?

Prof. Fateman CS 164 Lecture 43 Andrew Appel, the textbook author is at Princeton University The first version of our text used a language named “Tiger” (the Princeton mascot). Designed by Appel for teaching about compilers. Why is designing a new language and describing it so hard? Why? –Except if it is trivial, there are many little details. –Describing a language: how? Axiomatically? Informally? Operationally?.

Prof. Fateman CS 164 Lecture 44 Why the change? –Except if it is trivial, there are many little details to a language that must be nailed down. Syntax Semantics Pragmatics –Tiger was simple/tricky/boring.(?) –How to be formal yet readable? Definition Axiomatically? Informally? Operationally?.

Prof. Fateman CS 164 Lecture 45 MJ Overview MJ is a language that is supposed to be strictly a subset of Java. Designed to –Provide a useful one-semester exercise –Give a taste of implementation of modern Abstraction Static typing Memory management Object Oriented implementation of functions How to leave out things?

Prof. Fateman CS 164 Lecture 46 Reminscent of the novel Gadsby by Ernest Wright The entire manuscript of Gadsby was written in a subset of the English language. Namely, without the letter “e”

Prof. Fateman CS 164 Lecture 47 A child’s brain starts functioning at birth; and has, amongst its many infant convolutions, thousands of dormant atoms, into which God has put a mystic possibility for noticing an adult’s act, and figuring out its purport. Up to about its primary school days a child thinks, naturally, only of play. But many a form of play contains disciplinary factors. “You can’t do this,” or “that puts you out,” shows a child that it must think, practically or fail. Now, if, throughout childhood, a brain has no opposition, it is plain that it will attain a position of “status quo,” as with our ordinary animals. Man knows not why a cow, dog or lion was not born with a brain on a par with ours; why such animals cannot add, subtract, or obtain from books and schooling, that paramount position which Man holds today. But a human brain is not in that class. Constantly throbbing and pulsating, it rapidly forms opinions; attaining an ability of its own; a fact which is startlingly shown by an occasional child “prodigy” in music or school work. And as, with our dumb animals, a child’s inability convincingly to impart its thoughts to us, should not class it as ignorant. From page one of Gadsby …

Prof. Fateman CS 164 Lecture 48 Anyway, Lexical matters in MJ are reasonable subset Identifiers begin with a letter Integers and Booleans (true, false) are the only basic data types. Binary arithmetic or logical operators are && < + - * Additional operators are ()[]{}=.!; NO Strings. NO floats. Comments are /* */ or // to end of line. NOT nested, contrary to p. 484.

Prof. Fateman CS 164 Lecture 49 Restrictions that are major simplifications in MJ vs Java There is a main class with additional class declarations at the top level. No overloading. No classes defined within classes. All methods are public. No initializers by calling class No INPUT (all data must be literal integers in the program body).

Prof. Fateman CS 164 Lecture 410 Semantics: what does an MJ program compute? Conditions for an alleged MJ program to be legal. 1.The program must grammatically fit in the terms of the (page 485) MiniJava grammar. 2.The program must be legal Java. In which case The meaning of the MJ program is the same as the corresponding Java program. Why does this help? –There is a readily available reference implementation (javac, java) –There are (many) books, including some standards, that can provide informal descriptions, formal descriptions, examples.

Prof. Fateman CS 164 Lecture 411 Semantics: Is this just a cheap trick? Yes, but you probably think you know Java, so you might not mind. Even if you don’t know Java –There is a readily available reference implementation (javac, java) for your computer. –There are (many) books, including some standards, that can provide informal descriptions, formal descriptions, examples of Java programs. –There are examples of MJ programs in the text’s web site (also locally)

Prof. Fateman CS 164 Lecture 412 Java and even more so, MJ, can be written in Lisp Java’s authors were influenced by Common Lisp (G.L. Steele, Jr. especially), for example, implicit pointers (like lisp, not C) Lisp, Java, and MJ all use garbage collection: We don’t have to write one, which is what would have to be done if we implemented MJ in C. We can implement operations in Lisp that are supersets of MJ and then cut them off at the knees if we want to be strict. E.g. Lisp can read integers of any length (or operate on them). A strict implementation would truncate them to what’s legal in Java.

Prof. Fateman CS 164 Lecture 413 MJ Primitive Types, Constructors Integer Boolean No strings, characters, floats Ways of combining Arrays Classes

Prof. Fateman CS 164 Lecture 414 MJ variables live in classes or instances The value for “a” is [a pointer to] a record with a slot for each attribute. class Point { int x; int y; public boolean Init (int newx, int newy) x = newx; y=newy; return true;} //… Point a; boolean z; a = new Point; z=a.Init(3,4); xy 34

Prof. Fateman CS 164 Lecture 415 Methods are really functions Have names, parameter lists with types, optional return type (else VOID), bodies, and lexical scope. The names are distinguished by the classes of the instance being referenced, as well as the types of the arguments. Names have scope within methods. A simple single-inheritance hierarchy exists for classes. All programs are defined in a global lexical environment which includes built-in functions like system.out.println -- and not much else.

Prof. Fateman CS 164 Lecture 416 Information Hiding in MJ Overloading is forbidden. Names are visible lexically (as in Scheme!) But not nesting makes most of this trivial.

Prof. Fateman CS 164 Lecture 417 Print is really very weak Print only prints integers, which it magically knows about, but doesn’t know about strings or anything else.

Prof. Fateman CS 164 Lecture 418 MJ has rather simplistic object oriented stuff in it So how will we learn about cool OO stuff, beyond that in Java? –We will do this in ANSI CL, later, and in a neater form.

Prof. Fateman CS 164 Lecture 419 MJ Types All variables must be declared –compiler cannot infer simple types for variables from initial values All intermediate expressions, e.g. in 3*(x+4*y) are of obvious types. No “casts”.

Prof. Fateman CS 164 Lecture 420 MJ Type Checking: any holes? Instances exist before they have values. If an implementation checks for this possibility at runtime, there should not be any other type errors remaining in a properly checked program.

Prof. Fateman CS 164 Lecture 421 Other components: Expressions, Statements Mostly a Statement language + *,- … Loops: while (E) do S Conditionals if E S else S if E S Arithmetic, logical operations Assignment x = E Access x.length, this.fun(..) Sequences {S ; S; …}

Prof. Fateman CS 164 Lecture 422 MJ Memory Management Memory is allocated every time an instance of a class or an array is created. Calls can be recursive requiring a stack. We can allocate space (in fact my MJ implementation does…) on a heap for environments (remember CS61a?) Memory is deallocated automatically when an object is not reachable anymore –Done by the garbage collector (GC) –We use the Lisp GC, but it is done “automagically”

Prof. Fateman CS 164 Lecture 423 Course Project A Lexical Analyzer - MJ source -> token-stream A type-checker –MJ source  token-stream  intermediate code = data for typechecker. A complete code tree for an interpreter –MJsource  token-stream  intermediate code = data for simple interpreter to be evaluated Add this to an interpreter and you can run MJ.

Prof. Fateman CS 164 Lecture 424 Course Project / II Modify interpreter so we have a complete compiler –MJ  …  our own assembly language for which we also have an assembler and a machine simulator. Split in 6 (or so) programming assignments (PAs) –Lex, Parse, IC (augmented parser), typechecker, interpreter (given to you?), compiler There is adequate time to complete assignments well before the end of the semester. Individual or team (max. 2 students)

Prof. Fateman CS 164 Lecture 425 Programming Assignment I Write a few Lisp programs (see assignment). Can be done individually or with partner Due on Thursday, 11:59PM –Turn in via the SUBMIT command: cd to your directory, say hw1, with files hw1.cl and README type submit hw1