A program is a sequence of statements. For example:
int y ; y = 6 ; print y + 9 ;
A program may contain comments, which are ignored by the parser.
Comments can start with the token //
and extend to the end of the line.
They can also start with /*
and extend to the next */
.
Any expression followed by a semicolon ;
can be used as a statement.
SExp. Stm ::= Exp ";" ;
Any declaration followed by a semicolon ;
can be used as a statement.
Declarations have one of the following formats:
int i ;
int i, j ;
int i = 6 ;Statements also include
return i + 9 ;
while (i < 10) ++i ;
if
with an expression in parentheses followed by
a statement, else
, and another statement,
if (x > 0) return x ; else return y ;
{ int i = 2 ; { } i++ ; }
print 2+3 ; print "hello" ;
Expressions are specified with the following table that gives their precedence levels. Infix operators are assumed to be left-associative, except assignments, which are right-associative. The arguments in a function call can be expressions of any level. Otherwise, the subexpressions are assumed to be one precedence level above the main expression.
level | expression forms | explanation |
---|---|---|
15 | literal | literal (integer, float, string, boolean) |
15 | identifier | variable |
14 | v++ , v-- |
post-increment, post-decrement |
13 | ++v , --v |
pre-increment, pre-decrement |
13 | -e |
numeric negation |
12 | e*e , e/e |
multiplication, division |
11 | e+e , e-e |
addition, subtraction |
9 | e<e , e>e , e>=e , e<=e |
comparison |
8 | e==e , e!=e |
(in)equality |
4 | e&&e |
conjunction |
3 | e||e |
disjunction |
2 | v=e |
assignment |
The available types are bool
, double
, int
, string
, and void
.
An identifier is a letter followed by a list of letters, digits, and underscores.
Your grammar must be able to parse the examples in [the example directory ./examples/].
An easy way to perform the test is to run the test file test1.sh.
In a Unix shell, you can use the command source test1.sh
.
This assumes that
TestMini
is in the same directory
examples/
or a symbolic link to it in the same directory.
Running the test produces the file log.txt
, which you must submit
together with the file Mini.cf
.
A program is a sequence of definitions. You can treat this as an alternative projection to programs defined as sequences of statements.
A function definition has a type, a name, an argument list, and a body. Example:
int foo(double x, int y) { return y + 9 ; }
An argument list is a comma-separated list of argument declarations.
It is enclosed in parentheses (
and )
.
An argument declaration has a type and an identifier, for instance
int x
Notice that argument declarations with multiple variables
(int x, y
) are not included, even though a declaration that occurs
as a statement (as shown above), can have multiple variables.
A function body is a list of statements enclosed
in curly brackets {
and }
.
A function call is an expression of level 15, of form f(exp,...,exp)
where f
is an identifier. The argument list exp,...,exp
has
zero or more comma-separated expressions of level 0.
Your parser must parse all additional example programs.