Skip to content

tuomasb/compiler

Repository files navigation

Compiler project

Compiler compiles grammar defined in easy/assignment to SLX language.

Compiler.java First class creates a new Scanner class. Then it created a new Parser class with the created Scanner class as parameter in constructor. Then it created SymbolTable and CodeGenerator classes and sets them to created parsers public variables to let it use them during compilation. Finally it calls parser.Parse() and return SlxProgram instance of the compiled program

SymbolTable.java This class is a symbol table implemented as a simple linked list. Symbol table implements FindObj and NewObj methods used to find and create symbols. SymbolTable created Obj classes for each symbol that contain memory address, type and name of that symbol. Each Obj also contains pointer to next Obj in the chain. SymbolTable class also contains pointer to the first created Obj. When FindObj is called, the chain of symbols is iterated starting from the first created Obj class. When NewObj is called, the chain of symbols is traversed to the final symbol in the chain and its pointer is pointed to the new symbol. SymbolTable also keeps track of memory addresses. I could have done that in the parser itself but this is a good way to reduce code.

CodeGenerator.java This class is mostly a proxy class for the supplief SlxProgram class. It passes emit commands into that class. However it keeps track of labels. Everytime a label is emitted, CodeGenerator increments its label counter by one. This is also mostly to reduce code in Compiler.atg and generated parser.

Scanner.java Scanner generated by Coco/R from Compiler.atg. Tokenises the input stream.

Parser.java Parser generated by Coco/R from Compiler.atg. Parses the token stream with a LL(1) parser.

Compiler.atg Here lies the meat of the project. All the productions and the grammar I left factored. Grammar has been attributed to bring out data such as types of variables and names of symbols. Semantic actions perform type checking, code generation and pretty much everyting important. The file is well commented and the comments should clarify my thinking while coding the project.

owntests/*.tst Some tests I created to test the functionality more.

Features: All features in the easy project should be functional and hopefully bug free. Tests: All supplied easy tests + tests coded by me in in owntests directory pass. Estimated 20 hours spent coding and reading Coco/R manuals/documentation.

About

Compiler project

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published