

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!


th
th
Guidelines:
Objective: Consider a language called Micro which is defined by the following grammar
<Micro-program > begin <statement-list> end <statement-list> <statement-list> ;
Microโs Lexical classes are defined as follows:
ID :: An identifier can consist of letters, digits and underscores and must start with a letter or an underscore and can be followed optionally by letters, digits or underscores; an underscore must be followed by at least a letter or a digit and identifier can not be longer than 15 characters.
INTNUM :: Can be a signed or unsigned integer without any leading zeroes.
:= is an assignment operator, , (comma) is a separator, * is a multiplication operator and ** is an exponentiation operator, ; is a statement delimiter and + and โ are addition and subtraction operators respectively.
Our objective is to write an interpreter for this language. The interpreter will take any legal program written in Micro as an input and will interpret it statement by statement and generate the output. The language is simple and consists of only straight-line statements โ notice the absence of declarative statement in Micro, all variable names are implicitly declared at the point of their definition or use and the language consists of only an assignment and print statement in the language. The assignment statement assigns the value of
An interpreter of a language consists of a scanner, parser and evaluator which will first convert the Micro program into an internal representation based on Abstract Syntax Tree and then will evaluate it generating output.
Phase I : In this phase of the project, our goal will be to build the scanner and recursive descent parser of the interpreter which will scan and parse the input Micro program given to it and once found syntactically correct, convert it into an abstract syntax tree (AST). This can be accomplished through the following steps: Step I: First rewrite the grammar converting it into an appropriate form removing left recursion and to enforce correct operator precedence. Step II : Write a scanner which will generate tokens mentioned in the lexical classes as above. Along with ID token you should send the exact identifier as a string for holding it in AST and along with INTNUM token you should also send its value. Remember you have to write the scanner that reads the input Micro program character by character and thus, you will have to assemble the value of INTNUM token from its constituent characters (points will be deducted for solutions which skip/short-circuit such key steps). Apart from generating tokens, the scanner will also skip white spaces, tabs, newlines etc. Step III : Write a recursive descent parser which demands and matches tokens one by one generated by the scanner above and builds the sentence into an AST. You can use the format of AST as given on page 138 of Louden book. In general read section 3.6 and 3. for Tiny language implementation which should be helpful. In case a syntax error is detected, it should be notified to the user by generating the dump of the current partial AST that is built for the current sentence along with the token that has produced the error. The parser should be able to continue discarding the remainder of the current erroneous sentence and parse the remainder of Micro program. Thus, this phase should be able to