This project is a compiler for a simple object-oriented programming (OOP) language, developed using Lex and Bison. The compiler handles lexical analysis, syntax parsing, and semantic checking for class declarations, method calls, variable assignments, and basic control flow constructs.
The main goal is to demonstrate the creation of a basic compiler that can process an OOP language, including parsing class declarations, methods, variables, and basic expressions.
- Lexical analysis using Flex for tokenizing input.
- Syntax analysis using Bison to construct the syntax tree.
- Semantic checks for type matching and scope validation.
- Support for basic control flow (
if,while,for), method calls, and object instantiation.
- project.l: Lex specification file for lexical analysis.
- project.y: Bison grammar file for syntax analysis.
- Makefile: Build script to compile the project.
- input.txt: Example input file containing a test OOP program.
- output.txt: The compiler's output file after processing the input.
- project.tab.c: Bison-generated C file for the parser.
- project.tab.h: Bison-generated header file.
- lex.yy.c: Flex-generated C file for the lexer.
Flex-Bison-Compiler/
|
├── Licence
├── Makefile
├── README.md
├── error.cpp
├── error.h
├── errors.txt
├── input.txt
├── lex.yy.c
├── myParser.exe
├── output.txt
├── parser_log.txt
├── project.l
├── project.tab.c
├── project.tab.h
├── project.y
├── Παραδοχές.txt
│
Ensure you have the following installed:
- Flex: Fast lexical analyzer generator
- Bison: Parser generator
- GCC: GNU Compiler Collection
-
Clone the repository:
git clone https://github.com/your-repository-name.git cd your-repository-name -
Compile the project using the Makefile:
make
This command will:
- Generate
project.tab.candproject.tab.hfrom the Bison grammar file. - Generate
lex.yy.cfrom the Lex file. - Compile all generated C files and link them into an executable
myParser.
To run the compiler on a test program:
./myParser input.txtThis will process the input.txt file and generate an output in output.txt.
public class Test {
public void main(int x, int y) {
x = 0;
}
}Collected Assignments:
1) Variable x assigned with value 0
Input Program:
1 | public class Test {
2 | public void main(int x, int y) {
3 | x = 0;
4 | }
5 | }
Program is syntactically correct.
- Created files error.cpp and error.h to handle errors from both lex and bison files
- The compiler will print error messages with the line number if it encounters any syntax or semantic errors.
- Example error message:
Error at line 6: Unexpected character '!'
This project is licensed under the MIT License. See the LICENSE file for more details.