This project is a simple compiler for a subset of the Go programming language, designed to perform lexical analysis and parsing on Go source code. The project utilizes flex
for lexical analysis and bison
for parsing, allowing the compiler to recognize tokens, parse expressions, and execute basic syntax checking on the Go code provided as input.
- Lexical Analysis: Recognizes tokens like keywords, identifiers, numbers, operators, and symbols.
- Syntax Parsing: Parses statements, expressions, and functions.
- Error Handling: Basic syntax error reporting with line number indication.
- Loop Detection: Detects and processes simple
for
loops,if
statements, and variable declarations. - Arithmetic Expressions: Supports basic arithmetic operations.
lexer.l
: Contains rules for tokenizing the input source code. Written usingflex
.parser.y
: Contains grammar rules for parsing the Go language subset. Written usingbison
.main.c
: Entry point of the compiler. Coordinates the process between the lexical analyzer and parser.example.go
: Sample Go code to be used as input to test the compiler.
Ensure that flex
and bison
are installed on your system. Compile the code using the following steps:
-
Run
flex
to generate the lexical analyzer:flex lexer.l
-
Run
bison
to generate the parser with debug information:bison -d parser.y
-
Compile the generated files and main program with
gcc
:gcc -o my_parser main.c lex.yy.c parser.tab.c -ll
To use the compiler, run it with an input Go file:
./my_parser < example.go
This command will start parsing the input and display each token found along with syntax error information (if any).
Example output when parsing example.go
:
Starting parsing...
ID token found: package
ID token found: main
...
Syntax error at line 22: syntax error
PARSING FINISHED SUCCESSFULLY.
- The project currently has
shift/reduce
conflicts inparser.y
. These conflicts occur due to grammar ambiguities and may lead to parsing errors. - Further refinement of the grammar rules in
parser.y
can help reduce conflicts and improve parsing accuracy.