




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Spring 2002Columbia University Department of Computer Science
Source Program
Input^
Interpreter
Output
Source Program
↓ Compiler ↓
Input^
→^ Executable Program
Output
Program Text Lexer^ ↓^
Token Stream Parser^ ↓^
Abstract Syntax Tree
Static semantics (type checking)
Annotated AST
Translation to intermediate form
Three-address code Code generation
Assembly Code
a,^ int
b)
{ while
(a^ !=
b)^ { if^ (a^
^ b)^ a
-=^ b; else b
-=^ a; } return
a; }
a,^ int^ b) {while^ (a
!=^ b)^ {if (a > b)^ a -= b; else^ b^ -=
a; }return^ a;} i n^ t
sp^ g
c^ d
(^ i
n^ t
sp^ a
,^ sp
i
n^ t^ sp
b^ )
nl^ {
nl^ sp
sp^ w
h^ i
l^ e
sp
(^ a^ sp
sp^ b
)^ sp
{^ nl
sp^ sp
sp^ sp
i
f^ sp^
(^ a^ sp
^ sp
b^ )
sp^ a
sp^ -
=^ sp
b
;^ nl^ sp
sp^ sp
sp^ e
l^ s
e^ sp
b^ sp
sp
a^ ;^ nl
sp^ sp
}^ nl
sp^ sp
r^ e
t^ u
r^ n
sp
a^ ;^ nl
}^ nl Text file is a sequence of characters
a,^ int^ b) {while^ (a
!=^ b)^ {if (a > b)^ a -= b; else^ b^ -=
a; }return^ a;} int^ gcd
(^ int
a^ ,^
int^ b^
)^ –^ while
(^ a
!=^ b^
)^ –^ if
(^ a^
^ b^
)^ a^ -=
b^ ;
else^ b
-=^ a
return^
a^ ;^ ˝
A stream of tokens. Whitespace, comments removed.
func int^ gcd
args arg int a
arg int^ b
seq while != a b
if
a b
-= a b return^ a^ -=^ b^ a
int^ gcd(int
a,^ int^ b) {while^ (a
!=^ b)^ {if (a > b)^ a -= b; else^ b^ -=
a; }return^ a;} Abstract syntax tree built from parsing rules.
func int^ gcd
args arg int a
arg int^ b
seq while != a b
if
a b
-= a b return^ a^ -=^ b^ a
Types checked; references to symbols resolved
a,^ b seq^
btrue^
while^
(a^ !=^
b)
sl^
$3,^ b,
a seq^
btrue^
if^ (a^
<^ b)
sub^
a,^ a,
b^ %^ a
-=^ b
jmp^
L4:^ sub
b,^
b,^ a^ %
b^ -=^
a
L5:^ jmp
L1:^ ret
a
int^ gcd(int
a,^ int^ b) {while^ (a
!=^ b)^ {if (a > b)^ a^ -= b; else^ b^ -=
a; }return^ a;}
Idealized assembly language w/ infinite registers
pushl^
%ebp^
% Save frame pointer
movl^
%esp,%ebp movl^
8(%ebp),%eax
% Load a from stack
movl^
12(%ebp),%edx
% Load b from stack
cmpl^
%edx,%eax je^
% while (a != b)
jle^
% if (a^ <
b)
subl^
%edx,%eax
% a
-=^ b
jmp^
subl^
%eax,%edx
% b
-=^ a
jmp^
leave^
% Restore SP, BP
ret
t^ sp^
g^ c^
d^ (^
i^ n^
t^ sp
a^ ,^ sp
i^ n
t^ sp
b
into a stream of tokens^ ID^ IDintgcd
ID^ IDinta
ID^ IDintb
Each token consists of a token type and its text.Whitespace and comments are discarded.
(’a’..’z’ |
i
f
a-z0- a-eg-z0-
a-z90-
a-hj-z^
a-z0- 0-
^ arcs. Conceptually,
^ arcs denote state equivalence. ^ arcs add the ability to make nondeterministic(schizophrenic) choices.When an NFA reaches a state with an
^ arc, it moves into
every^ destination.NFAs can be in multiple states at once.
a ee^12
e^1 e^2 e|e^12
e ^ ^1 e^2 ^ ∗^ (e)
^ e
(a|e)n produces^ w^
o^ m
a e
n
after simplification. Most
^ arcs disappear.
marking
of the NFA
w
m o
a e m
n
n 2 Tools often try to strike a balance between the tworepresentations.ANTLR uses a different technique.
if^ while
-^ punctuation (
-^ identifiers (
foo^ bar
-^ numbers (
10 -3.14159e+
-^ strings (
"A^ String"
’y’^ .OR.
’t’)^
Statement label
Normal
When column 6 is not a space, line is considered part ofthe previous.Fixed-length line makes it easy to allocate a one-linebuffer.Makes sense on punch cards.
i^ <^ 10:i =^ i^ +^
print^
i^ #
Prints
i^ =^0 while^
i^ <^ 10:i =^ i^ +^
print^
i^
#^ Just prints
This is succinct, but can be error-prone.How do you wrap a conditional around instructions?
semantics
—its meaning.
The syntax is aesthetic, but can be a religious issue.But aesthetics matter to people, and can be critical.Verbosity does matter: smaller is usually better.Too small can be a problem: APL is a compact, crypticlanguage with its own character set (!) E←A^ TEST B;LL←0.5E←((A
FORTRAN
example:
!^ Loop
header
(for^
i^ =^1
to^ 25)
!^ Assignment
to^ variable
Trying too hard to reuse existing syntax in C++: vector<
vector
^ foo;
vector<vector
foo;^
//^ Syntax
error
C distinguishes
^ and^ >>
as different operators.
Goal: discard irrelevant information to make it easier forthe next stage.Parentheses and most other forms of punctuationremoved.
Compared to regular languages, context-free languagesadd one important thing: recursion.Recursion allows you to count, e.g., to match pairs ofnested parentheses.Which languages do humans speak? I’d say it’s regular: Ido not not not not not not not not not not understand thissentence.
t^ is a terminal): A^ →^ t^1
... tB n A^ →^ t^1 ... t n Context-free languages (
P^ is terminal or a variable):
A^ →^ P^1
... P n Context-sensitive languages:^ αAα^1
→^ α 2 1 Bα^2 “B^ →^ A
only in the ‘context’ of
α· · ·^ α^1
’if’ expr ’then’
expr
|^ ’while’
expr ’do’ expr |^ expr ’:=’ expr ;expr :^ NUMBER
expr ’)’
AST stmt() –switch (next-token) –case ”if” : match(”if”); expr(); match(”then”); expr();case ”while” : match(”while”); expr(); match(”do”); expr();case NUMBER or ”(” : expr(); match(”:=”); expr();˝^ ˝
expr^
’+’^ term
|^ term
becomesAST expr() –switch (next-token) –case NUMBER : expr(); /* Infinite Recursion */
expr ’)’ |^ ID^ ’=’
expr becomesAST expr() –switch (next-token) –case ID : match(ID); match(’(’); expr(); match(’)’);case ID : match(ID); match(’=’); expr();
term |^ expr ’-’
term |^ term; becomes expr:^ expr (’+’ term |
’-’^ term )
|^ term;
term^ |
’-’^ term
|^ term; becomes expr^ :^ term
exprt
exprt^
term^ exprt |^ ’-’^
term^ exprt |^ /*^ nothing
*^ and^ -
expr :
expr ’+’
term |^ expr ’-’
term |^ term ; becomes expr :^ term (’+’ term |
’-’^ term)*
else? if^ (a)
if^ (b)
c(); else d(); if a if b c()^ d()
or^ if a^ if^ b^ c()
? d()
Grammars are usually ambiguous; manuals givedisambiguating rules such as C’s:As usual the “else” is resolved by connecting anelse with the last encountered elseless if.
"if"^
expr^ "then"
stmt^
iftail
|^ other-statements
iftail:^ "else"
stmt |^ /*^ nothing
; Problem comes when matching “iftail.”Normally, an empty choice is taken if the next token is inthe “follow set” of the rule. But since “else” can follow aniftail, the decision is ambiguous.
expr "then"
stmt (^ options
{greedy
=^ true;}
:^ "else"
stmt )?| other-statements;
b^ then a
else b
fi;
“fi” is “if” spelled backwards. The language also usesdo–od and case–esac.
T^ :^ int
|^ int
stack^
input^
action int * int + int
shift int^
shift int *^
int + int shift int * int^
reduce T : int
int * T^
reduce T : int ’*’ T
T^
shift
T +^
int^ shift T + int^
reduce T : int
T + T^
reduce E : T
T + E^
reduce E : T ’+’ E
E^
reduce E : T ’+’ E
;^ as a statement terminator. if^ (a<b)
printf("a
less");
else^ { printf("b");
printf("
less");
} Pascal uses
;^ as a statement separator. if^ a^ <
b^ then
writeln(’a
less’)
else^ beginwrite(’a’);
writeln(’
less’)
end Pascal later made a final
;^ optional.