1 Review. 2 Creating a Runnable Program  What is the function of the compiler?  What is the function of the linker?  Java doesn't have a linker. If.

Slides:



Advertisements
Similar presentations
SYMBOL TABLES &CODE GENERATION FOR EXECUTABLES. SYMBOL TABLES Compilers that produce an executable (or the representation of an executable in object module.
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Names and Bindings.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Basic Semantics.
A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Outline Java program structure Basic program elements
CS201 – Expressions & I/O. Precedence What is the value of * 4 ? 20 or 14 ? Without parentheses, there are a set of rules to fall back on. Unary.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Computer Science 210 Computer Organization Introduction to C.
L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall Java Programming.
1 Introduction to Semantics The meaning of a language.
Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Compiler Construction
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
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.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Basic Semantics Associating meaning with language entities.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
Names, Scope, and Bindings Programming Languages and Paradigms.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Object Oriented Programming Lecture 2: BallWorld.
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
1 Binghamton University Exam 1 Review CS Binghamton University Birds eye view -- Topics Information Representation Bit-level manipulations Integer.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Design issues for Object-Oriented Languages
Lecture 3 Translation.
Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types
Chapter 6 – Data Types CSCE 343.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
CSCI206 - Computer Organization & Programming
Semantics CSE 340 – Principles of Programming Languages Spring 2016
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to Data Structure
Name Binding and Object Lifetimes
Identifiers & Lifetimes
Python fundamental.
Presentation transcript:

1 Review

2 Creating a Runnable Program  What is the function of the compiler?  What is the function of the linker?  Java doesn't have a linker. If the "main" class uses other classes, what happens? does javac combine all classes in one class file? does java load the classes? when?  what does this statement do? Class.forName("com.mysql.jdbc.Driver");

3 Linker  The linker "resolves" external references, it fills in the missing values in object files.  But, it doesn't check that the names match the expected type! Example: homework 6A problem 1: /* File 1 */ char *foo; extern double bar;... printf("%s", foo); printf("%f", bar); /* File 2 */ extern double foo; char* bar = "foo!";... printf("%d", foo); printf("%s", bar);

4 Homework 4 There are two parts to a language:  syntax is the grammar rules for writing (or saying) the language. It describes what are legal expressions. The grammar has 2 parts. lexical grammar - rules for tokens (words, punctuation, space) syntactic grammar - rules for statements  What notation is commonly used for these two parts?  semantics describes the meaning of the language usually given in a manual (in human language) formal semantics: axiomatic, denotational, and operational semantics precisely describe effect of language on the computer's "state", can be used to prove program correctness.

5 Homework 5 Representation of primitive data types:  how big is an int, long, float, double?  what's the largest value you can store in a long?  what format is used to store signed integers?  If you look at a (signed) integer in memory and see: what value does this represent?  If you look in memory using hexadecimal format and see: 7FFFFFFF is this a positive or negative value? What's the value?

6 Homework 5 Representation of primitive data types:  floating point numbers are stored as 3 components. What are the 3 components?  the largest float is about 2 x ( bias ), so the smallest value should be 1 x ( 0 - bias ), but its not!  why is the smallest float 1 x ( 1.4E-45 ). Why?  what floating point is returned in these cases, using IEEE standard floating point: float x = 1.0E20; float result1 = x*x*x*x; float result2 = 1.0/(x*x*x*x); double zero = 0.0; double result3 = 1.0/zero; double result4 = zero * result1;  In Java, how can you test if x (say, "double x") is Infinity?  Test for x is NaN?

7 Test for Infinity and NaN In Java... double x =...; if ( x == Double.POSITIVE_INFINITY )... if ( Double.isInfinite(x) && x > 0 )... if ( x == Double.NEGATIVE_INFINITY )... if ( Double.isInfinite(x) && x < 0 )... /* for NaN there is no constant. Use function */ if ( Double.isNaN(x) )... In C++ use: #include numeric_limits dbl; if ( x == dbl.infinity( ) )...

8 3 Memory areas  The memory area allocated for a program's data is divided into 3 areas. What are their names?

9 3 Memory areas  The memory area allocated for a program's data is divided into 3 areas. What are their names? Static area Stack Heap  How are these three areas used?  What sort of values are placed there? depends on the language: statically typed languages generally use them as discussed in class dynamic typed languages (Perl, SmallTalk, Scheme) use the heap for most things

10 Weird functions  A programmer writes a timer like this: time_t tstart = time(0); // starting time doSomeWork( ); time_t tstop = time(0); // stopping time /* now print when the program started/stopped */ char* start = ctime( tstart ); char* stop = ctime( tstop ); printf("Job started: %s\n", start); printf("Job finished: %s\n", stop); Job started: Wed Dec :14:39 PM Job finished: Wed Dec :14:39 PM  but both output values are always the same! Why?

11 Weird functions: static memory  ctime uses a static buffer to create a string of the formatted time ("Wed Dec 14...").  it returns a pointer to this buffer.  it reuses the same buffer each time it is invoked! char* start = ctime(time1); Tue Dec :00 AM char* start Wed Dec :00 PM char* stop = ctime(time2); Static buffer:

12 Weird functions: why static memory?  Why use static memory for the string buffer?  why not a local (stack automatic) variable? ctime( time_t time ) { char buf[80];... return buf; Wed Dec :00:00 PM\n char* time = ctime(time1);... x = fun(10,20,30);... printf("%s\n", time); stack:

13 Weird functions: why static memory?  why not use malloc (heap dynamic) variable? ctime( time_t time ) { char* buf = (char *)malloc(80);... return buf; Wed Dec :00 AM Wed Dec :00 AM Wed Dec :00 PM char* time; time = ctime(time1);... time = ctime(time2);... time = ctime(time3); heap:

14 Alignment  as described in class, int, float, and double values must usually be aligned on word boundaries (word = 4 bytes)  some CPU require "double" aligned on double word boundaries (8 bytes).  how much space is used by these struct variables? struct one { char c; double d; } x; /* try changing order */ struct two { double d; char c; } y; struct three { char c1; double d1; char c2; double d2; char c3; double d3 } z;

15 Data type compatibility  each language has rules for compatibility of data types  C++, Java, C#: "widening" conversions allowed; others require a cast and may throw an exception.  what about compatibility of user defined types? double dbl = 3.5; float flt = 1.2F; int nint = ; long nlng = ; /* OK */ dbl = flt; dbl = nlng; dbl = nint; flt = nlng; flt = nint; nlng = int; /* Error (but OK in C) */ flt = dbl; nint = nlng; nlng = float; is there a problem here?

16 Type Compatibility for user types struct A { float x; char c; }; struct B { float x; char c; }; struct C { float z; char c; }; struct D { char c; float x; }; int main( ) { struct A a; struct B b; struct C c; struct D d; a.x = 0.5; a.c = 'a'; b = a; // ?? c = a; // ?? d = a; // ?? if (b == a) // true? Which of these assignments are allowed? Is the test condition true?

17 Constants  Manifest constant (literal): const int BUFSIZE = 1024; char buf[BUFSIZE];  Compile time (everything is known): const int BUFSIZE = 8 * 128;  Load time (determined when program is loaded): static final String user = System.getenv("username"); /* java */  Run-time: int fun(const int n) { const size = n*n;... } final int width = 400; /* java instance var */

18 Scope of Names  Categories of scope rules: lexical (static) scope - scope known by looking at the code. dynamic scope - follows flow of execution  most languages use lexical scope, but each has its own rules about redefining names in inner scopes.  example of name conflict: #include /* hypot is an error in C but not in C++ ! */ float hypot(float x, float y) { return sqrt(x*x + y*y); }

19 Keeping Track of Names  Compiler (or interpreter) uses a symbol table to record known names and their attributes (bindings).  For lexical scope, there are two approaches: a new symbol table for each scope: a stack of symbol tables a linked list of definitions for each symbol (name), elements of the linked list have scope number to keep track of active scopes: scope stack  Symbol table is not needed after program is linked, but is usually hidden in the executable code to aid debugging.  To make program smaller or inhibit reverse engineering: strip  C#: "Dotfuscation" pun on "obfuscation" - to make confused or opaque