




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!





Handed Out: August 31, 2022. Due: September 14, 2022, 5:00 pm
The machine problems for this semester will deal with implementing a compiler for the Classroom Object- Oriented Language (cool). Your compiler will need to cover the full set of cool instructions and detect and report errors in the input files if there are any. Correct programs will be translated into llvm.
This MP deals with the front end only. So your programs have to process input files in cool and build the abstract syntax tree (AST). The assignment is divided into three parts: (1) get familiar with cool, its support code, and the files we provide for this MP; (2) write a scanner or lexical analyser (lexer); and (3) write a parser.
The compiler consists of several phases. Each phase will be compiled into its own binary file. It will take its input from standard input (except the lexer which takes a commandline input filename) and write to standard output. So in the end to start your compiler you will call
lexer input file | parser | semant | cgen > output file.ll
For this assignment you are making the lexer and parser. Reference binaries for lexer, and parser are provided, but try to use them only after you are really done with your own testing. You should test your compiler passes extensively with your own tests. We believe that writing a comprehensive and well thought out set of tests is a critical part of good programming practice. Your goal should be that the reference binaries uncover no new bugs. You are not required to work on the semantic analyzer (semant) for this MP. We will provide you the binary for the same. The command shown above compiles the program into LLVM (output to the file output file.ll ) and this can be linked with our runtime for Cool, coolrt.o, using the command
clang output file.ll coolrt.o -o executable
To use Clang on EWS, see Section 2 of the Introductory LLVM Exercise in the Resources section of the course website.
2 Scanner
Write a lexical scanner for cool using flex. To do this, first read the documentation on flex, which you can download from the Resources section of the course web page.
The files that you will need to modify are:
There is an issue in deciding how to handle the special identifiers for the basic classes (Object, Int, Bool, String), SELF TYPE, and self. However, this issue doesn’t actually come up until later phases of the compiler—the scanner should treat the special identifiers exactly like any other identifier.
Your scanner should maintain the variable curr lineno that indicates which line in the source text is currently being scanned. This feature will aid the parser in printing useful error messages.
Finally, note that if the lexical specification is incomplete (some input has no regular expression that matches) then the scanner generated does undesirable things. Make sure your specification is complete.
3 Parser
Use bison to generate a parser for cool and build the AST.
First download the documentation on bison from the course web page and read it. Then take a look at the file cool.y and understand it. This is the skeleton for the second phase of your compiler — the parser.
The files that you will need to modify are:
Note that you will not hand in your modified bison test good.cl and bison test bad.cl files. However, it is important to make good tests to ensure that your parser is working properly.
To build the parser, type
make parser
in the directory mp1/src. This will link some more files of support code to your directory and compile your skeleton. Your parser needs as input the output of your completed lexer. So first complete the lexer, then you can start your parser. Use bison test good.cl to test your skeleton parser with a working cool program by typing
lexer bison test good.cl | parser
Your semantic actions should build an AST using the cool support code tree package, whose interface is defined in mp1/include/cool-tree.h. The Tour section of the cool manual contains an extensive discussion of the tree package for cool abstract syntax trees. You will need most of that information to write a working parser. Read the Tour section carefully: it contains explanations, caveats, and other details that will help you avoid a number of pitfalls in understanding and using the AST classes.
until it goes away). If you find yourself making up rules for many things other than operators in expressions and for let, you are probably doing something wrong. The cool let construct introduces an ambiguity into the language (try to construct an example if you are not convinced). The manual resolves the ambiguity by saying that a let expression extends as far to the right as possible. The ambiguity will show up in your parser as a shift-reduce conflict involving the productions for let. This problem has a simple, but slightly obscure, solution. We will not tell you exactly how to solve it, but we will give you a strong hint. We implemented the resolution of the let shift-reduce conflict by giving low precedence to the token that controls the precedence of the relevant production. Since your compiler uses pipes to communicate from one stage to the next, any extraneous characters produced by the parser can cause errors; in particular, the next stage may not be able to parse the AST your parser produces.
%type
This declaration says that the non-terminal program has type
%type <member_name> X Y Z ...
you instruct bison that the attributes of non-terminals (or terminals) X, Y, and Z have a type appropriate for the member member name of the union. All the union members and their types have similar names by design. It is a coincidence in the example above that the non-terminal program has the same name as a union member. It is critical that you declare the correct types for the attributes of grammar symbols; failure to do so virtually guarantees that your parser won’t work. You do not need to declare types for symbols of your grammar that do not have attributes. The g++ type checker complains if you use the tree constructors with the wrong type parameters. If you ignore the warnings, your program may crash when the constructor notices that it is being used incorrectly. Moreover, bison may complain if you make type errors. Heed any warnings. Don’t be surprised if your program crashes when bison or g++ give warning messages.
4 Further ways to use your Makefile
Documentation on make can also be downloaded from the course web page.
5 What and how to hand in
You have to hand in all files that you modify in this MP. That is
A few days before the deadline, the hand-in script /class/cs426/current/bins/mp1Handin will be made available on EWS. To hand in your MP, run the script with one argument—the directory that contains cool.flex and cool.y.
You can hand in the MP multiple times. The one we will grade is your last hand in.
When you hand in the MP, your implementation will be tested against the reference implementation for a few tests. This is only meant to ensure that this is a working implementation. It is by no means a complete testing, and it not meant to substitute your own testing.
Don’t copy and modify any part of the support code! The provided files are the ones that will be used in the grading process.
Important: We will use our version of the lexer to test and grade your parser. So make sure your parser also works with our lexer.
Grading: In addition to looking at the test results, we will manually look at your code. 5% of the grade is for commenting your code. Another 5% is for appropriate usage of flex and bison features.