Programming Language Syntax & Semantics Machine Code

advertisement
CS256 Computer Science I
Kevin Sahr, PhD
Lecture 3:
Programming Languages
1
Programming Language
✴a language for writing programs that tell the
computer what you want it to do
✴defined by specifying the syntax and semantics of
the statements used to create programs
1-
2
1-
3
1-
4
Syntax & Semantics
✴syntax: rules that specify what makes a valid
statement in the language
• like the rules of grammar for the english language
✴semantics: the interpretation of language statements
• like determining the meaning of an english sentence
• what does it tell the computer to do?
Machine Code
✴Every CPU understands a set of simple instructions
represented by binary numbers
• called the machine code of the CPU
• unique to that CPU
✴on early computers, programs were written in
machine code
§ PROBLEM: very hard for humans to understand!
High Level Programming
Languages
✴instructions are english-like
✴one high level instruction can map to multiple
machine code instructions
1-
5
1-
6
1-
7
1-
8
Source Code
✴High level programming language programs are
written in text files called source code files
✴The high level instructions must be translated into
machine code instructions before the CPU can
execute them
✴Traditionally there are two approaches to translating
source code into machine code:
✴interpreted languages
✴compiled languages
Interpreted Languages
✴source code is translated into machine code one
line-at-a-time as the program is executed
✴the translation is performed by a special program
called an interpreter
✴the program can run on any computer that has the
appropriate interpreter
Compiled Languages
✴the entire source code is translated into machine
code before the program is executed
✴this translation is called compilation
✴compilation is performed by a special program
called a compiler
✴the result of compilation is usually called a binary or
executable file
✴compiled programs execute faster than interpreted
programs, since they’ve already been translated
✴but because machine code is specific to a
particular CPU, binary files can only be run on that
type of computer
✴programs must be compiled separately for each type of
computer where they will be executed
The Java situation
✴Java was developed for web-based applications, so
it was important that Java programs be able to
execute on any computer
✴but efficient execution of large programs was also
desirable
✴Solution:
✴a Java compiler creates Java bytecode, which is the
“machine” code for a Java Virtual Machine (JVM)
✴a JVM is a software program that runs bytecode programs
✴Java programs can run on any program that has a JVM
installed
✴so Java gets most of the benefits of both a
compiled and an interpreted language
✴but from the programmers’ standpoint Java looks
like a compiled language
1-
9
1-
10
1-
11
1-
12
The Programming Process
✴Writing programs in a compiled high level language
is a 3-step process:
1.Create the source code file
2.Compile the source code file
3.Execute the executable
Programming Tools
✴The programming process requires 3 tools:
1.A text editor to create/edit the source code file
2.A compiler to compile the source code file
3.A way of executing/testing the binary file
Programming Tools
✴These tools can be separate applications, OR
✴They can be bundled together into a single
application called an Integrated Development
Environment (IDE)
✴We will use an IDE called JGrasp
• reasonably powerful
• relatively simple interface
• free
Java Source Code
✴Source code files in Java have the file suffix .java
• EX: a program called MyProgram would be written in a
source code file called MyProgram.java
✴NOTE: Java is case sensitive!
• to Java, MyProgram.java and myprogram.java are two
different files
1-
13
1-
14
1-
15
1-
16
Java Class Files
✴The Java compiler produces binary (bytecode) files
with:
• the same file prefix as the source code file, and
• the file suffix .class
✴EX: compiling MyProgram.java would create the
binary file MyProgram.class
✴compiled Java files are often referred to as class
files
Our First Java Program
In Lab 1 Part A we will create, compile, and execute
a simple Java program using the JGrasp IDE
Lecture 3 Vocabulary
programming language
binary/executable file
syntax
bytecode
semantics
Java Virtual Machine (JVM)
machine code
high level programming language
integrated development
environment (IDE)
source code file
JGrasp
interpreted languages/interpreter
Java
compilation/compiler
class file
Download