This project is a custom programming language compiler developed as part of a university course on "Processamento de Linguagens (PL)". The goal was to design a language with specific features and implement a compiler that translates the language into stack-based virtual machine code.
- Imperative Programming: The language supports an imperative programming paradigm.
- Variable Declaration: Variables must be declared before use and cannot be redeclared. The language supports:
- Integer variables
- Arrays of integers
- Control Structures:
whileloops with nested supportrepeatloops with a fixed number of iterationsif-elseconditional statements
- Input/Output Operations:
readfor inputprintfor integer outputprintSfor string output
- Error Handling:
- Lexical errors
- Syntax errors with suggestions
- Semantic errors for variable and conditional misuse
- Gain experience in language engineering and generative programming.
- Develop grammar rules for context-free and translator grammars.
- Implement a compiler using Python's PLY (Python Lex-Yacc).
- Generate code for a stack-based virtual machine (VM).
- Lexical Analysis:
- Recognizes tokens like keywords, identifiers, and operators.
- Implements error handling for illegal characters.
- Syntax Analysis:
- Uses grammar rules defined in YACC to parse valid programs.
- Provides detailed feedback for syntax errors.
- Semantic Analysis:
- Ensures proper use of variables and control structures.
- Code Generation:
- Translates source code into VM assembly instructions.
- Examples:
- Programs demonstrating use cases like finding the LCM of two numbers, drawing a triangle, and identifying square sides.
int NumOne, NumTwo, maxValue;
start
printS ("Please Enter two integer Values\n")
read(NumOne)
read(NumTwo)
if (NumOne > NumTwo) {
maxValue = NumOne;
} else {
maxValue = NumTwo;
}
while(! (maxValue % NumOne == 0 && maxValue % NumTwo == 0)) {
maxValue++;
}
printS ("LCM = " + maxValue);PUSHN 4
START
PUSHS "Please Enter two integer Values\n"
WRITES
READ
ATOI
STOREG 0
...
STOP- Install Python with the PLY library.
- Run the compiler with a source code file:
python compiler.py source.txt
- The compiler will generate VM code in a .vm file.