0% found this document useful (0 votes)
11 views

Lecture02 - Programming

This document summarizes a lecture on compilation and interpretation. It discusses how compilers translate source code into machine executable code by translating it once into target/object code that can then be run many times. Interpreters translate source code each time it is run by using an interpreter to directly execute the source instructions. Compilation is usually faster while interpretation is more flexible and easier to debug. Languages like Java use a compiler to translate to an intermediate bytecode that is then executed by a virtual machine, acting as an interpreter.

Uploaded by

Mariea Kissoon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lecture02 - Programming

This document summarizes a lecture on compilation and interpretation. It discusses how compilers translate source code into machine executable code by translating it once into target/object code that can then be run many times. Interpreters translate source code each time it is run by using an interpreter to directly execute the source instructions. Compilation is usually faster while interpretation is more flexible and easier to debug. Languages like Java use a compiler to translate to an intermediate bytecode that is then executed by a virtual machine, acting as an interpreter.

Uploaded by

Mariea Kissoon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Lecture 2: Compilation and Interpretation

COMP 524 Programming Language Concepts


Stephen Olivier
January 15, 2009

Based on notes by A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts

The University of North Carolina at Chapel Hill


Goals

•In this lecture, we will discuss how to translate source


code into machine executable code.

The University of North Carolina at Chapel Hill


Compilation and Interpretation

•There are two primary methods for translating high-level


code:
• Compilation
• Interpretation

The University of North Carolina at Chapel Hill


Compilation

Source Program

Compiler

Input Target Program Output

The University of North Carolina at Chapel Hill


The “target program” is called the object
Compilation code.

Source Program

Compiler

Input Target Program Output

The University of North Carolina at Chapel Hill


Compilation

Source Program

Compiler
You translate once and run many times.

Input Target Program Output

The University of North Carolina at Chapel Hill


Interpretation

Source Program Input

Interpreter

Output

The University of North Carolina at Chapel Hill


Interpretation

Source Program Input

Interpreter
You translate for each run.

Output

The University of North Carolina at Chapel Hill


Comparison

Source Source In

Interpreter
Compiler

In Target Out Out

The University of North Carolina at Chapel Hill


Compilers are usually faster than
Comparison interpreters.

Source Source In

Interpreter
Compiler

In Target Out Out

The University of North Carolina at Chapel Hill


Interpreters are usually more flexible and
Comparison easier to debug than compilers.

Source Source In

Interpreter
Compiler

In Target Out Out

The University of North Carolina at Chapel Hill


Compilation and Interpretation

Source Program

Translator

Intermediate
Program Virtual
Output
Machine
Input The University of North Carolina at Chapel Hill
Compilation and Interpretation

Source ProgramThe Virtual Machine acts as an


interpreter.

Translator

Intermediate
Program Virtual
Output
Machine
Input The University of North Carolina at Chapel Hill
Compilation and
The Interpretation
translator can be a compiler or an
interpreter. It is considered to be a compiler if:
1. There is a thorough analysis of the program
Source Program
2. The transformation is non-trivial.

Translator

Intermediate
Program Virtual
Output
Machine
Input The University of North Carolina at Chapel Hill
Compilation and Interpretation

Source Program

This is exactly the process


that Java uses.
Translator

Intermediate
Program Virtual
Output
Machine
Input The University of North Carolina at Chapel Hill
Linking Source Program

Compiler

Incomplete machine language

Machine
Library language
Linker program
Routines

The University of North Carolina at Chapel Hill


Linking Source Program

The Linker is used to connect subroutines that


Compiler
are not contained in the original code.

Incomplete machine language

Machine
Library language
Linker program
Routines

The University of North Carolina at Chapel Hill


Linking Source Program

Compiler

This is used by Fortran

Incomplete machine language

Machine
Library language
Linker program
Routines

The University of North Carolina at Chapel Hill


Preprocessing

Source Program

Preprocessor

Modified source program

The University of North Carolina at Chapel Hill


Preprocessing

Source Program
For example, this may be used to remove
comments or use replacement macros.

Preprocessor

Modified source program

The University of North Carolina at Chapel Hill


Preprocessing

Source Program

On the note about macros, consider the macro


#define FALSE 0.
Preprocessor
This code will replace all instances of the word
“FALSE” with the value 0.

Modified source program

The University of North Carolina at Chapel Hill


Tombstone Diagrams

•Useful for graphically representing programs and


translators.
• Program (in C): • Interpreter (for Perl, impl. in x86)

• Machine (x86): • Translator (C to x86, impl. in x86)

The University of North Carolina at Chapel Hill


Tombstone Diagrams

•Example: Compiling a C program to run on x86

The University of North Carolina at Chapel Hill


Bootstrapping

•Pascal came shipped with three things:


• A Pascal to P-Code Compiler, written in Pascal.
• A P-code interpreter, in Pascal.
• A Pascal to P-Code Compiler, written in P-Code.

The University of North Carolina at Chapel Hill


Bootstrapping

•Pascal came shipped with three things:


• A Pascal to P-Code Compiler, written in Pascal.
• A P-code interpreter, in Pascal.
• A Pascal to P-Code Compiler, written in P-Code.

P-Code is a very simple


language that can easily be
translated into any machine
language.

The University of North Carolina at Chapel Hill


Bootstrapping

•Pascal came shipped with three things:


• A Pascal to P-Code Compiler, written in Pascal.
• A P-code interpreter, in Pascal.
• A Pascal to P-Code Compiler, written in P-Code.

The University of North Carolina at Chapel Hill


Bootstrapping

•Pascal came shipped with three things:


• A Pascal to P-Code Compiler, written in Pascal.
The• interpreter is hand translated
A P-code interpreter, in Pascal. into a supported language.
• A Pascal to P-Code Compiler, written in P-Code.

The University of North Carolina at Chapel Hill


Bootstrapping

•Pascal came shipped with three things:


• A Pascal to P-Code Compiler, written in Pascal.
The• interpreter is hand translated
A P-code interpreter, in Pascal. into a supported language.
• A PascalAnd then compiled
to P-Code into
Compiler, machine
written code.
in P-Code.

The University of North Carolina at Chapel Hill


Bootstrapping

• To get a simple (but slow) compiler, one could use the


“Pascal to P-Code compiler, in P-Code” and the “P-Code to
machine lang. interpreter” to compile Pascal code.

The University of North Carolina at Chapel Hill


Bootstrapping
This interpreter is used to run the Pascal
toslow)
• To get a simple (but P-Code compiler
compiler, one and
couldthe program.
use the
“Pascal to P-Code compiler, in P-Code” and the “P-Code to
machine lang. interpreter” to compile Pascal code.

The University of North Carolina at Chapel Hill


Bootstrapping (Faster)

•To create a faster compiler, we modify the “Pascal to P-


Code compiler, in Pascal” so that it is a “Pascal to
machine language compiler, in Pascal.”

The University of North Carolina at Chapel Hill


Bootstrapping (Faster)

•To create a faster compiler, we modify the “Pascal to P-


This is MUCH HARDER than creating a P-Code to
Code compiler, in Pascal” so that it is a “Pascal to
Machine language interpreter.
machine language compiler, in Pascal.”

The University of North Carolina at Chapel Hill


Bootstrapping (Faster)

Now construct the “Pascal to machine


lang. compiler, in P-Code.”

The University of North Carolina at Chapel Hill


Bootstrapping (Faster)

Then run the “Pascal to


machine lang. compiler, in
Pascal” through the “P-
Code” version to produce
the “Machine lang.”
version.

The University of North Carolina at Chapel Hill


Bootstrapping (Faster)

The University of North Carolina at Chapel Hill


Compiling
Character Stream
Scanner (lexical analysis)
Token Stream
Parser (syntax analysis)
Parse Tree
Semantic analysis & Symbol Table
intermediate code gen.
Abstract syntax tree
Machine-independent
optimization (optional)
Modified intermediate form
Target code generation.
Machine language
Machine-specific
optimization (optional)
Modified target language
The University of North Carolina at Chapel Hill
Example Program GCD

program gcd(input, output);


var i, j: integer;
begin
read(i,j); // get i & j from read
while i<>j do
if i>j then i := i-j
else j := j-1;
writeln(i)
end.

The University of North Carolina at Chapel Hill


Lexical Analysis Scanner (lexical analysis)

•Recognize structures without regard to meaning and


groups them into tokens.
program gcd(input, ouput);

program gcd ( input , output ) ;

•The purpose of the scanner is to simplify the parser by


reducing the size of the input.

The University of North Carolina at Chapel Hill


Syntax Analysis Parser (syntax analysis)

•Parsing organizes the tokens into a context-free


grammar (i.e., syntax).

program gcd ( input , output ) ;

program

id(GCD) ( id(INPUT) more_ids ) ; block


, id(OUTPUT) more_ids Rest of code

empty of North Carolina at Chapel Hill


The University
Syntax Analysis Parser (syntax analysis)

•Parsing organizes the tokens into a context-free


grammar (i.e., syntax).

program gcd ( input , output ) ;

The Syntax analysis catches all


malformed statements
program

id(GCD) ( id(INPUT) more_ids ) ; block


, id(OUTPUT) more_ids Rest of code

empty of North Carolina at Chapel Hill


The University
Syntax Analysis Parser (syntax analysis)

•Parsing organizes the tokens into a context-free


grammar (i.e., syntax).

The parse
gcd tree
program ( is sometimes
input , output called
) ; a
concrete syntax tree because it contains
how all tokens are derived...
program

id(GCD) ( id(INPUT) more_ids ) ; block


, id(OUTPUT) more_ids Rest of code

empty of North Carolina at Chapel Hill


The University
Syntax Analysis Parser (syntax analysis)

•Parsing organizes the tokens into a context-free


grammar (i.e., syntax).
...however, much of this information
input , output
is
program gcd ( ) ;
extraneous for the “meaning” of the
code(e.g., the only purpose of “;” is to end
a statement).
program

id(GCD) ( id(INPUT) more_ids ) ; block


, id(OUTPUT) more_ids Rest of code

empty of North Carolina at Chapel Hill


The University
Semantic analysis &
Semantic Analysis intermediate code gen.

•Semantic analysis discovers the meaning of a program.


by creating an abstract syntax tree that removes
“extraneous” tokens.
•To do this, the analyzer builds & maintains a symbol table
to map identifiers to information known about it. (i.e.,
scope, internal structure, etc...)
•By using the symbol table, the semantic analyzer can
catch problems not caught by the parser. For example,
• Identifiers are declared before used
• subroutine calls provide correct number and type of arguments.
The University of North Carolina at Chapel Hill
Semantic analysis &
Semantic Analysis intermediate code gen.

•Not all semantic rules can be checked at compile time.


• Those that can are called static semantics of the language.
• Those that cannot are called dynamic semantics of the
language. For example,
• Arithmetic operations do not overflow.
• Array subscripts expressions lie within the bounds of the array.

The University of North Carolina at Chapel Hill


Example Program GCD

program gcd(input, output);


var i, j: integer;
begin
read(i,j); // get i & j from read
while i<>j do
if i>j then i := i-j
else j := j-1;
writeln(i)
end.

The University of North Carolina at Chapel Hill


Semantic analysis &
Semantic Analysis intermediate code gen.

program

id(GCD) ( id(INPUT) more_ids ) ; block

program Index Symbol Type


1 INTEGER type
2 TEXTFILE type
(5) read
3 INPUT 2
4 OUTPUT 2
(3) (6) read 5 GCD program
Rest of 6 I 1
code
(3) (7) 7 CarolinaJat Chapel Hill1
The University of North
Target code generation Target code generation.

•Code generation takes the abstract syntax tree and the


symbol table to produce machine readable code.
•Simple code follows directly from the abstract syntax
tree and symbol table.

The University of North Carolina at Chapel Hill


Machine-independent Machine-specific
Optimization optimization (optional) optimization (optional)

•The process so far will produce correct code, but it


may not be fast.
•Optimization will adjust the code to improve
performance.
• A possible machine-indp. optimization would be to keep the
variables i and j in registers throughout the main loop.
• A possible machine-spec. optimization would be to assign the
variables i and j to specific registers.

The University of North Carolina at Chapel Hill

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy