4.4 KiB
4.4 KiB
"Nicht Haskel 2.0" Java Compiler
Realisation of a subset of the Java Standard Compiler in the course Compiler Construction of the 4th semester Computer Science at the Duale Hochschule Stuttgart (Horb).
This project aims to provide a simplified version of the Java compiler, focusing on key language features and demonstrating the principles of compiler construction.
Realised Java syntax
- Data types:
int
,boolean
,char
- Access modifier:
public
,protected
,private
- Operators:
=
+
-
*
%
/
>
<
>=
<=
==
!=
!
&&
||
++
--
- Keywords:
class
,this
,while
,do
,if
,else
,for
,return
,new
,switch
,case
,break
,default
,:
- Statements:
if
...if else
...else
;while
... ;do
...while
;for
;
- Comments:
- Single line:
// comment
- Multi-line:
/* comment */
- Single line:
- Further functions:
- All methods are overloadable
- High maintainability and expandability through implementation of the visitor pattern
- Logging Input and Outputs
- Error Handling in the Semantic Check
Project Structure
src/
└── main/
├── java/
│ ├── ast/ -> Defining the structure of the AST
│ ├── bytecode/ -> Generate Java bytecode
│ ├── main/ -> Running the compiler
│ ├── parser/
│ │ ├── astBuilder/ -> Builder creating the AST
│ │ ├── generated/ -> Antlr generated grammar
│ │ └── grammar/ -> Antlr grammar
│ ├── semantic/ -> Running the semantic check
│ └── visitor/ -> Visitor interface
└── resources/
test/
└── java/
│ ├── main/ -> Running E2E tests
│ ├── parser/ -> Performs tests on the parser
│ ├── semantic/ -> Performs tests on the semantic check
└── resources/ -> Ressources for running the Tests
Class-Diagramm AST
Distribution of the realisation
i22030 & i22035
Ausführliche Beschreibung der Parser Umsetzung:
Parser:
- Grammar -> (src/main/java/parser/grammar)
- Scanner
- Parser
- Abstract Syntax Tree (AST) -> (src/main/java/ast)
- AstBuilder -> (src/main/java/parser/astBuilder)
Parser tests:
- ParserTests -> (src/test/java/parser)
- TestCases -> (src/test/resources/input/singeFeatureTests)
Other:
- Documentation -> (README.md)
- Ast Class-Diagramm -> (ast.png)
- PowerPoint
i22005
Semantic check:
- Set all types and check whether types have been used correctly
- Contexts -> (src/main/java/semantic/context)
- Exceptions Handling -> (src/main/java/semantic/exceptions)
Semantic Tests:
- Typing and Type checking -> (src/test/java/semantic/EndToTypedAstTest)
- Exception and feature test -> (src/test/resources/input/typedAstExceptionsTests)
i22007
Bytecode generation:
- Complete bytecode generation -> (src/mein/java/bytecode)
i22011
Tests and execution:
- Makefile
- Running Compiler -> (src/main/main)
- Running E2E tests -> (src/test/main)
- Typing and Type checking -> (src/test/java/semantic/EndToTypedAstTest)
Used Tools
- Maven 4.0
- Used for automating the build process and managing dependencies.
- ANTLR4 v.13.1
- Used to parse the input Java code into the Abstract Syntax Tree.
How to run the compiler
Possibilities
1. Start miniCompiler using make:
Make needs to be installed
cd .\src\test\ ; make clean compile-miniCompiler
2. Start miniCompiler using jar:
If you do not have the .jar, download it here or compile it using mvn package or make first
java.exe -DgenJar=bool -DgenClass=bool -jar path_to_jar\jarName.jar 'path_to_input_file.java' 'path_to_output_directory'
Example (jar needs to be in the target directory)
java.exe -DgenJar=true -DgenClass=true -jar .\target\JavaCompiler-1.0-jar-with-dependencies.jar 'src/main/resources/input/CompilerInput.java' 'src/main/resources/output'
- set DgenJar true, to generate the jar, false for no jar
DgenJar=true
- set DgenClass true, to generate class files, false for no class files
DgenClass=true