Lorenzo Mirto Bertoldo @climadd
This project is a compiler that translates Ac, a simple language with integer and floating-point support, into Dc, a stack-based calculator language using reverse Polish notation. The compiler processes Ac code through lexical, syntactic, and semantic analysis before generating Dc-compatible output. Ac features basic arithmetic operations, variable declarations, and assignments, while Dc executes expressions using a stack-based approach. This compiler bridges the two, allowing Ac programs to be evaluated in Dc. More on Dc Manual.
Below is the installation guide to set up and run the project.
This compiler was developed using Java 17, but it should run on any modern Java version. Before running the project, ensure that Java is installed on your system. You can follow this installation guide to set it up.
- Verify Java Installation: Run
java -version
in the terminal to confirm Java is installed. - Clone the Repository (if applicable): Use
git clone <repo-link>
to download the project. - Compile the Source Code: Navigate to the project directory and use
javac *.java
to compile all Java files. - Run the Compiler: Execute the program through the various test classes and
.txt
files located in thesrc/test/data
directory for the compilation.
Below is a structured breakdown of the key stages involved in the compilation process.
The Lexical Analysis phase is performed by the Scanner
class. This stage tokenizes the input stream, converting raw text into a sequence of meaningful tokens that represent the basic elements of the source code.
Handled by the Parser
class, the Syntactic Analysis phase processes the tokens produced in the lexical analysis. It constructs an Abstract Syntax Tree (AST), which represents the hierarchical structure of the program.
The Semantic Analysis phase is executed by the TypeCheckingVisitor
and its related classes. This phase ensures that the program adheres to type correctness and semantic rules. It involves visiting the AST and verifying the types of variables involved, ensuring the consistency of operations and declarations, and validating them through the use of the Symbol Table.
The final phase of compilation involves the Code Generation process. This is another visit to the AST, where the CodeGenerationVisitor
class translates the AST's nodes contents into Dc code, producing the final executable output of the program.