This is an implementation of a Decaf Compiler using PLY (Python Lex-Yacc) tool for parsing and lexical analysis in Python. The compiler will print an Abstract Syntax Tree and output an assembly file after the input is successfully parsed. It will return an error should the input contain errors.
You need to have Python 3.x installed on your machine. PLY is a third-party tool that needs to be installed separately. You can install it using pip.
pip3 install -r requirements.txt
You can run the Decaf compiler by executing the decaf_compiler.py script. The script reads input code from a file and outputs the result to the console.
python decaf_compiler.py input_file.decaf
The input file should contain the Decaf code that you want to compile.
- Supports basic arithmetic operations (+, -, *, /)
- Supports comparison operators (<, <=, >, >=, ==, !=)
- Supports boolean operations (&&, ||, !)
- Supports if-else statements and while loops
- Supports integer and boolean literals
- Supports variable declaration and assignment
- Supports function declaration and calling
- Supports error handling for invalid input
- Supports the Decaf syntax and semantics
The Decaf compiler is implemented in Python using the PLY tool. PLY provides a lexer and parser generator that can be used to build compilers and interpreters. The lexer and parser are defined in separate files (decaf_lexer.py and decaf_parser.py) and are then imported into the main script (decaf_compiler.py). The lexer reads the input code and converts it into tokens, which are then passed to the parser. The parser uses the tokens to build an abstract syntax tree (AST) that represents the input code. The AST is then used to generate the output code in assembly.
This project was created by Daniel Kogan as a part of CSE 304 - Compiler Design at Stony Brook University.