Parser for Language Mini Aarne Ranta Assigment 1, Compiler Construction Hands-On, Sun Yat-Sen University, 2016 ==Programs== 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 ``*/``. ==Statements== 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: - a type and one variable (as in function parameter lists), ``` int i ; ``` - a type and many variables, ``` int i, j ; ``` - a type and one initialized variable, ``` int i = 6 ; ``` Statements also include - Statements returning an expression, ``` return i + 9 ; ``` - While loops, with an expression in parentheses followed by a statement, ``` while (i < 10) ++i ; ``` - Conditionals: ``if`` with an expression in parentheses followed by a statement, ``else``, and another statement, ``` if (x > 0) return x ; else return y ; ``` - Blocks: any list of statements (including the empty list) between curly brackets. For instance, ``` { int i = 2 ; { } i++ ; } ``` - Printing statements, to print the value of any expression. Examples: ``` print 2+3 ; print "hello" ; ``` ==Expressions== 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 | ``ee``, ``e>=e``, ``e<=e`` | comparison | 8 | ``e==e``, ``e!=e`` | (in)equality | | 4 | ``e&&e`` | conjunction | | 3 | ``e||e`` | disjunction | | 2 | ``v=e`` | assignment | ==Types== The available types are ``bool``, ``double``, ``int``, ``string``, and ``void``. ==Identifiers== An identifier is a letter followed by a list of letters, digits, and underscores. ==To test== 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 ./test1.sh]. In a Unix shell, you can use the command ``source test1.sh``. This assumes that - your parser ``TestMini`` is in the same directory - you have a copy of ``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``. =An optional extension: functions= ==Function definitions== 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 ``}`` . ==Function calls== 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. ==Test criteria== Your parser must parse all [additional example programs fun-examples/].