logo

MiniJava-Compiler =================.

   

Added on  2023-04-06

4 Pages450 Words88 Views
MiniJava-Compiler
=================
The compiler was written as the main project in an assignment
Run
---
minijavac requires java 1.8 to run.
Minijava source code can be compiled as follows:
java -jar minijavac.jar [source-file-name] that is you want for compiler purpose
The generated byte code is backwards compatible with java 1.1 and will run on any version of java.
java [main-class-name]
Project Description
______________________
Compilers operate in three main stages:
1. Parse source files to generate a parse tree. Report any syntax errors as another compiler working.
2. Traverse the parse tree, building a symbol table from the source code and reporting all semantic errors
3. Generate code in the target language
1

### Parsing and Syntax Analysis ###
To parse minijava source code, a [parser]() was generated with the Antlr4 parser generator tool. Antlr
generates a parser in java for the given minijava grammar.
The parser attempts to generates a parse tree given some minijava source code.
If any syntax errors are encountered during parsing, an error message is generated [underlining]() the
offending tokens.
If there are no syntax errors, the parser builds a parse tree and the compiler proceeds to semantic analysis.
### Static Semantics and the Symbol Table ###
Semantic analysis proceeds in the following fashion.
1. A symbol table is constructed be traversing the parse tree. During this phase, the program is checked for:
1. [Duplicate klasses]()
2. [Cyclic inheritance errors]()
3. [Declaration before use]()
2. The program is [type checked]() to grant as many static typing Type_system#Static_type-checking
guarantees
3. Variables are checked to ensure that they are [initialized before use]().
For a given method fun() taking no arguments and returning an integer:
2

End of preview

Want to access all the pages? Upload your documents or become a member.