MiniJava-Compiler =================.
VerifiedAdded on 2023/04/06
|4
|450
|88
AI Summary
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
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
=================
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
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
### 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
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
```java
int fun(){
int sum; //declaration
sum=0; //initialization
return sum; //use
}
```
should compile, while
```java
int fun(){
int sum; //declaration
return sum; //error, variable x may not have been initialized.
}
```
### Bytecode Generation ###
This is where the [magic](CodeGenerator Documentation) happens. The parse tree is traversed a final time
in order to generate byte code.
[//]: # (The ObjectWeb ASM java libraries is used to generate class files.
[//]: # (Generating bytecode, even with ASM, is akin to [printing assembly]
3
int fun(){
int sum; //declaration
sum=0; //initialization
return sum; //use
}
```
should compile, while
```java
int fun(){
int sum; //declaration
return sum; //error, variable x may not have been initialized.
}
```
### Bytecode Generation ###
This is where the [magic](CodeGenerator Documentation) happens. The parse tree is traversed a final time
in order to generate byte code.
[//]: # (The ObjectWeb ASM java libraries is used to generate class files.
[//]: # (Generating bytecode, even with ASM, is akin to [printing assembly]
3
Build
-----
Building the tool from source requires the [Antlr4 java library] and the [ASM java library]
0. Copy the project in your folder
1. Download [Antlr4](http://www.antlr.org/download/antlr-4.4-complete.jar) and [ASM] asm-5.0.3-
bin.zip)
2. Unzip asm-5.0.3-bin.zip and add asm-5.0.3/lib/all/asm-all-5.03.jar and the antlr jar to your path.
3. Generate the lexer and parser by feeding Antlr4 the [minijava grammar]( Minijava.g4).
`java -jar ~/path/to/antlr-4.4-complete.jar -visitor Minijava.g4`
4. compile with the java 1.8 compiler
`javac *.java`
And you're done. If you want to package it as a jar file:
jar cf minijavac.jar *.class
4
-----
Building the tool from source requires the [Antlr4 java library] and the [ASM java library]
0. Copy the project in your folder
1. Download [Antlr4](http://www.antlr.org/download/antlr-4.4-complete.jar) and [ASM] asm-5.0.3-
bin.zip)
2. Unzip asm-5.0.3-bin.zip and add asm-5.0.3/lib/all/asm-all-5.03.jar and the antlr jar to your path.
3. Generate the lexer and parser by feeding Antlr4 the [minijava grammar]( Minijava.g4).
`java -jar ~/path/to/antlr-4.4-complete.jar -visitor Minijava.g4`
4. compile with the java 1.8 compiler
`javac *.java`
And you're done. If you want to package it as a jar file:
jar cf minijavac.jar *.class
4
1 out of 4
Related Documents
Your All-in-One AI-Powered Toolkit for Academic Success.
+13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
© 2024 | Zucol Services PVT LTD | All rights reserved.