Popl I
Popl I
Principles
of
Programming
Languages
Basic
Pragma;cs
BITS Pilani
Pilani | Dubai | Goa | Hyderabad Instructor: Prof. Santonu Sarkar
Concurrent
Programming
Parallel
execu>on
is
about
taking
a
set
of
instruc>ons
and
running
in
parallel,
w/o
viola>ng
the
dependencies.
9/8/15
CS/IS
F301
First
Semester
2014-15
3
Compilers
and
Interpreters
Compila(on
Interpreta(on
Transla>on
of
a
program
wri<en
Performing
the
opera>ons
in
a
source
language
into
a
implied
by
the
source
seman>cally
equivalent
program
program
wri<en
in
a
target
language
Source
Program
Interpreter Output
Input
Error messages Input
Compiler
Synthesis
Part
takes
the
Target Assembly Program tree
structure
and
translates
the
opera>ons
Assembler therein
into
the
target
Relocatable Object Code program
6
The
Phases
of
a
Compiler
Phase Output Sample
Programmer (source code Source code in a file A=B+C;
producer)
Scanner (performs lexical Token string A, =, B, +, C, ;
analysis) And symbol table with names
Parser (performs syntax analysis Parse tree or abstract syntax tree ;
based on the grammar of the |
programming language) =
!
! / \
A +
Annotated parse tree or abstract
Semantic analyzer (type checking, / \
syntax tree B C
etc)
Intermediate code generator Three-address code, quads, or RTL int2fp B t1
+ t2 C t1
:= t2 A
Optimizer Three-address code, quads, or RTL int2fp B t1
+ t1 #2.3 A
Code generator Assembly code MOVF #2.3,r1
ADDF2 r1,r2
MOVF r2,A
CS/IS
F301
First
Semester
2014-15
7
Lexical
Analyzer
Typical
tasks
of
the
lexical
analyzer:
Remove
white
space
and
comments
Encode
constants
as
tokens
Recognize
keywords
Recognize
iden>ers
and
store
iden>er
names
in
a
global
symbol
table
8
Token
A<ributes
#define NUM 256/* token returned by lex() */
#define ID 259 /* token returned by lex() */
factor(){
if (lookahead==(){match(();
expr(); match());
Lexical }
y= 31 + 28*x analyzer else if (lookahead == NUM){
lex() printf(%d, tokenval); match(NUM);
} else if (lookahead == ID){
printf(%s, symtable[lookup(tokenval)].
token lexptr);
(lookahead) match(ID);
}
else error(); provided by the lexer for ID
}
<id, y> <assign, > <num, 31> <+, > <num, 28> <*, > <id, x>
tokenval
(token attribute)
Parser
parse()
struct entry {
char *lexptr; /* lexeme (string) for tokenval */
int token;
};
struct entry symtable[];
insert(s, t): returns array index to new entry for string s token t
lookup(s): returns array index to entry for string s or 0
11
Deriva>on
Given
a
CF
grammar
we
can
determine
the
set
of
all
strings
(sequences
of
tokens)
generated
by
the
grammar
using
deriva(on
We
begin
with
the
start
symbol
In
each
step,
we
replace
one
nonterminal
in
the
current
senten(al
form
with
one
of
the
right-hand
sides
of
a
produc>on
for
that
nonterminal
12
Parse
Tree
expr
expr expr
expr expr
Parse tree of the string 9-5+2
num
9 - 5 + 2
14
A
Translator
for
Simple
Expressions
expr expr + expr { print(+) }
expr expr - expr { print(-) }
expr num
num 0 { print(0) }
num 1 { print(1) }
num 9 { print(9) }
18
Run>me
Data
Areas
pc Kernel address
space
Return heap
Heap address Static read-only
data/constants
Code
20
Frames
(3.6)
A
new
frame
(also
known
as
ac(va(on
record)
is
created
each
>me
a
method
is
invoked
A
frame
is
destroyed
when
its
method
invoca>on
completes
Each
frame
contains
an
array
of
variables
known
as
its
local
variables
indexed
from
0
Local
variable
0
is
this
(unless
the
method
is
sta>c)
Followed
by
method
parameters
Followed
by
the
local
variables
of
blocks
Each
frame
contains
an
operand
stack
21
FLOW
OF
CONTROL
int summation(int st, int fin) { int summation_iter(int st, int fn) {
if (st==fin) return st; int acc=0;
else for (int i=st;i<=fn;i++) acc += i;
return st+summation(st+1, fin); return acc;
} }
Cas>ng
Explicit
Conversion
Opera>ons
Type
conversions
specied
by
programmer
Can
be
used
to
violate
typing//
E.g.,
cas>ng
in
C
Boolean
type
The
conversions
from/to
bool
consider
false
equivalent
to
0;
true
is
equivalent
to
1.
Many
languages
like
Ada,
Pascal,
Fortran
and
C++11
allows
conformant
array
void addscalar(int n,int m,double arr[n][n*m+300], double x);
int main(void) {
double b[4][308];addscalar(4, 2, b, 2.17); return 0;
}
There
is
a
rela>onship
between
n,
m
and
a
which
indicates
the
expected
bound
of
a[][]
at
Elabora>on
>me
During
alloca>on
of
stack
space
for
local
objects
Here
the
array
can
be
allocated
to
a
stack
using
Dope
Vector
Dynamic part
dynamic
arrays
Dimension,
lower/upper
bound
of
arr
each
dimension
(to
avoid
recompu>ng
the
upper
bound)
Array
out
of
bound
can
be
checked
For
conformant
arrays,
the
shape
informa>on
is
known
during
Dope
vector
elabora>on
>me
Pointer
loca>on
Range
dimension1
Return
Address
Range
Dimension2
Fixed part
A
binding
is
an
associa>on
between
two
en>>es:
Name
and
memory
loca>on
(for
variables)
Name
and
func>on
in the programs execu>on during which the binding is ac>ve, i.e. not destroyed
Load
;me
Assign
machine
addresses
to
sta>c
data
Run
;me
Bind
values
to
variables
Allocate
dynamic
data
and
assign
to
variables
Allocate
local
variables
of
procedures
on
the
stack
9/8/15
CS/IS
F301
First
Semester
2014-15
75
Object
and
Binding
Life>me
Object
life;me
Period
between
the
crea>on
and
destruc>on
of
the
object
Example:
>me
between
crea>on
and
destruc>on
of
a
dynamically
allocated
variable
in
C++
using
new
and
delete
Binding
life;me
Period
between
the
crea>on
and
destruc>on
of
the
binding
(name-to-
object
associa>on)
Best
t
Find
the
smallest
block
large
enough
to
accommodate
the
alloca>on
request.
head
Cost
of
Alloca>on
Maintaining
a
single
free
list
will
have
linear
cost
to
nd
a
block
to
accommodate
each
alloca>on
request
Classes/Structure
Java
has
a
clean
lexical
scope.
Local:
dened
inside
the
method
Member:
within
a
class
and
Parameters:
Variables
in
method
declara>on
Func>ons
Local
variables
are
in
the
context
of
a
func>on.
Created
when
func>on
execu>on
starts
and
ends
when
func>on
execu>on
ends
(C,
Java,
Python)
In
C,
a
local
variable
can
be
sta>c
Blocks
Primarily
to
dene
a
control
ow.
Variables
can
be
dened
within
the
block
as
well
as
within
control
statements
(C
++11,
Java).
Primarily
for-loops
In
Java
and
C/C++,
variable
can
be
out
of
scope
once
the
execu>on
control
goes
out
of
the
block
Perl
is
block
scoped
but
Python
is
not
Examples:
C,
Java,
Prolog,
Scheme,
.
.
.
However
Overloading
One
name
""
more
than
one
object
Compiler
automa>cally
converts
an
object
into
an
object
of
another
type
when
required.
In
Java,
""
+
o
forces
o.toString()