Your goal in this step is to generate the parser for the project's grammar. By the end of this step your compiler should be able to take a source file as the input and parse the content of that file returning "Accepted" if the file's content is correct according to the grammar or "Not Accepted" if it is not.
Now the scanner created in the first step will be modified to feed the parser. Instead of printing the tokens, the scanner has to return what token is recognized in each step. The following diagram shows the basic structure of a simple compiler and hightlights the steps that will be completed at the end of this step:
Similar to the scanner case, there are tools that automatically generate parsers based on context-free grammar descriptions. More precisely, there are parser generators for two main classes of context-free grammars: LR(1) and LL(k). Please, refer to the textbook or one of the references at the end of this handout for the theory of LL(k) and LR(k) parsing.
For this project we recommend two main parser generation tools:
At this step you have to create the code for the parser of your compiler. We recommend you to do that using one of the tools described above, although the whole parser could be written manually without the help of any tool.
The following basic steps should be followed:
Note: Your output should be exactly Accepted
or Not Accepted
Check the spellings and case of the letters and do not add any other characters including dots, exclamation marks, etc.
Testcases can be downloaded here.
· Java and ANTLR
ANTLR uses classes that implement the interface ANTLRErrorStrategy to handle errors during compilation.
When an error occurs during parsing, your parser will call reportError
on whatever ANTLRErrorStrategy is attached to the
parser (depending on how you write your grammar, it may also call recoverInline). DefaultErrorStrategy
is the class that implements the necessary methods; you can extend this class
to create your own error strategy (in the example below, called CustomErrorStrategy). To set a new error strategy, use the
ANTLRErrorStrategy es = new CustomErrorStrategy();
· C/C++ and Flex/Bison
When an error occurs in Bison/YACC, the error handler "yyerror" will be invoked. You should customize this handler to print the message and terminate the current program.
You will lose 20% of total credit if you failed to follow the following rules.
See this page for instructions regarding the directory structure of your project.
Assuming the above directory structure is used
· If Java and ANTLR are used, the following command will be run from the root of your project directory
· $ java -cp lib/antlr.jar:classes/ Micro <path to test cases>/test.micro· Accepted$
· If C/C++ and Flex/Bison are used,
· $ <build directory>./Micro path_to_your_testcases/test.micro· Accepted$
First, make sure that your directory structure is correct and you are submitting only the required files. Then, follow the instructions about the turnin command and note that the step name for this step is step2 (use -p step2). Also, if you are working in a pair, submit from the same login (also called your group account) every time.