Tiger Language Reference Manual
Prof. Stephen A. Edwards
Columbia University
This document describes the Tiger language defined in Andrew
Appel’s book Modern Compiler Implementation in Java (Cam-
bridge University Press, 1998).
The Tiger language is a small, imperative language with in-
teger and string variables, arrays, records, and nested functions.
Its syntax resembles some functional languages.
1 Lexical Aspects
An identifier
is a sequence of letters, digits, and underscores that
starts with a letter. Case is significant.
Whitespace (spaces, tabs, newlines, returns, and formfeeds)
or comments may appear between tokens and is ignored. A com-
ment
begins with /*
and ends with */
. Comments may nest.
An integer constant is a sequence of one or more decimal dig-
its (i.e., 0123456789). There are no negative integer constants;
negative numbers may be obtained by negating an integer con-
stant using the unary -
operator.
A string constant is a sequence of zero or more printable char-
acters, spaces, or escape sequences surrounded by double quotes
"
. Each escape sequence starts with a backslash \ and stands
for some sequence of characters. The escape sequences are
Newline
Tab
\"
Double quote
\
Backslash
\^
c Control-c, where
c is one of
@A...Z[\]^_
.
\
ddd
The character with ASCII code ddd (three deci-
mal digits)
\···
\ Any sequence of whitespace characters (spaces,
tabs, newlines, returns, and formfeeds) sur-
rounded by
\
s is ignored. This allows string con-
stants to span multiple lines by ending and start-
ing each with a backslash.
The reserved words are array break do else end for
function if in let nil of then to type var while.
The punctuation symbols are , : ; ( ) [ ] { } . + -
* / = <> < <= > >= & | :=
2 Expressions
A Tiger program is a single expr
.
expr:
string-constant
integer-constant
nil
lvalue
-
expr
expr binary-operator expr
lvalue :=
expr
id
( expr-list
opt
)
( expr-seq
opt
)
type-id { field-list
opt
}
type-id
[ expr
] of expr
if
expr then
expr
if
expr
then
expr
else expr
while expr
do expr
for
id
:= expr
to
expr do expr
break
let
declaration-list in expr-seq
opt
end
expr-seq:
expr
expr-seq
;
expr
expr-list:
expr
expr-list
,
expr
field-list:
id =
expr
field-list
,
id
=
expr
2.1 Lvalues
lvalue:
id
lvalue
. id
lvalue [ expr
]
An l-value
represents a storage location that can be assigned
a value: variables, parameters, fields of records, and elements
of arrays. The elements of an array of size n
are indexed by
0
,
1
, . . . ,
n− 1.
2.2 Return values
Procedure calls, assignments, if-then, while, break, and some-
times if-then-else produce no value and may not appear where a
value is expected (e.g., (a:=b)+c
is illegal). A let
expression
with nothing between the
in
and end
returns no value.
A sequence of zero or more expressions in parenthesis (e.g.,
(a:=3; b:=a) separated by semicolons are evaluated in order
and returns the value produced by the final expression, if any.
An empty pair of parenthesis
()
is legal and returns no value.
2.3 Record and Array Literals
The expression type-id
{
field-list
opt
} (zero or more fields are
allowed) creates a new record instance of type
type-id
. Field
1
评论2
最新资源