Coursework Task 1: Interpreter Summary In this task you will write an interpreter for a simple programming language. We will use the parser generator ANTLR as our main tool to reduce
the amount of hand-written code required. The main resource is a 'skeleton' project that you can download here. Please carefully read the details below and the submission guidelines. Execution model and typing rules A program in our simple programming language starts execution from a function int main(...), which may occur anywhere in the function declarations. All arguments to functions are passed by value, and the only identifiers defined in any function are as follows: • The names of other functions (which may be defined before or after the current function); • The parameters taken by the current function; • The local variables (initialised at the top of the body) of the current function. In other words, all functions are of 'global scope', while parameters and local variables are of 'function scope'. There is one special case: when a local variable is being initialised, all the other local variables are yet defined and thus not available. We do not use an explicit return keyword - the return value of a function is the value of the last expression in its body. We assume that the input program satisfies the following conditions: • The input program has a main function, which has return type int; • No two functions may have the same name; • The definition of a function fnA may involve invocations of other functions that are defined before or after the function fnA; • No two parameters or local variables of the same function fnA may have the same name; • No parameter or local variable may have the same name as a function; • Parameters and local variables of fnA are only in-scope in the body of fnA; • All parameters or local variables must have either int or bool types;/n12/6/23, 12:42 AM o space, o newline, o skip, o x := y. o while x do {...}. Coursework Task 1: Interpreter o repeat {...} until x; • The type of a function invocation f(e1, ..., en) is the return type of f; • The type of a block { el; ...; en } is the type of the last expression en; • The type of an if expression is the type of its then block (and its else block); • In any comparison such as x < y, both x and y must have int type; • In any arithmetic operation such as x + y, both x and y must have int type; • In any Boolean operation such as x & y, both x and y must have bool type; • In any if expression, its then block and its else block have the same type; • In any if, while, or repeat expression, the condition must have bool type; • In any while or repeat expression, the loop body must have unit type; • In any assignment x := y, both x and y must have the same type; • In any print expression print x, either x is space or newline, or x has int type. In short, the input program is assumed to be 'well-formed' and 'well-typed'. The semantics of the language constructs should be self-explanatory; in particular, your interpreter will print to System.out as requested by print statements in the input program. Input / output specification and some technical details • Your grammar file is SimpleLang.g4 with a start rule called prog, and thus the lexer and parser classes generated by ANTLR are Simple LangLexer and Simple LangParser, respectively. • Your interpreter class Simple LangInterpreter implements SimpleLangVisitor, and it has an extra method visitProgram which also returns an Integer (return value of main) and takes two arguments: the first is the parse tree's root node (a Simple LangParser. ProgContext), the second is an array of strings (arguments) where each of them must either be an INTLIT or a BOOLLIT; we assume that they match the number and types of the parameters of main.