This is a C syntax checker built using Flex and Bison. It parses C code and reports any syntax errors found.
- Parses a subset of the C language, including:
- Variable declarations (including pointers)
- Function declarations and definitions
- Structs, unions, and typedefs
- Expressions and statements
- Control flow structures (
if,else,for,while,do-while,switch,case,default,break,continue,return)
- Detailed error reporting with line and column numbers, and a caret pointing to the error location.
- Reports multiple errors in a single pass.
- Color-coded output for better readability.
- Comprehensive test suite.
flexbisongccorclangmake
You can build the parser by following these steps:
-
Generate the parser and tokenizer C code from the Bison and Flex files:
bison -d parser.y flex -o tokenizer.c tokenizer.l
This will create
parser.c,parser.h, andtokenizer.c. -
Compile the generated C files and create the executable:
gcc -o parser parser.c tokenizer.c
This will create the
parserexecutable.
To check the syntax of a C file, run the parser executable with the file as an argument:
./parser path/to/your/file.cExample:
./parser tests/comprehensive_test.cIf the file has no syntax errors, the program will exit silently with a status code of 0. If there are syntax errors, they will be printed to standard error, and the program will exit with a non-zero status code.
This project is built using standard tools that are available on most Unix-like operating systems, including Linux and macOS.
Linux:
The build and run instructions above should work without any modifications. You may need to install the required tools first. On Debian-based distributions (like Ubuntu), you can do this with:
sudo apt-get update
sudo apt-get install flex bison build-essentialmacOS:
The build and run instructions above should work without any modifications. You may need to install the required tools first. You can do this using Homebrew:
brew install flex bisonXcode Command Line Tools are also required, which can be installed with:
xcode-select --installWindows:
To build and run this project on Windows, you will need to use a Unix-like environment such as Windows Subsystem for Linux (WSL) or MinGW/MSYS2. Once you have set up one of these environments, you can follow the Linux instructions to install the required tools and build the project.
The tests/ directory contains a suite of C files to test the syntax checker.
comprehensive_test.c: A large file that includes a variety of valid C constructs.error_test.c: A file with a variety of syntax errors to test the error reporting.expressions.c: Tests for various expressions.loops.c: Tests for loop structures.selection.c: Tests for selection statements.pointers.c: Tests for pointer declarations and usage.structs_unions.c: Tests for struct and union declarations.typedefs.c: Tests for typedef declarations.
You can run the tests by passing any of these files to the parser.
The syntax checker provides detailed error messages, including:
- The file name, line number, and column number of the error.
- The line of code where the error occurred.
- A caret (
^) pointing to the exact location of the error. - A descriptive message about the error.
Example of error output:
tests/error_test.c:4:2: error: expected ';' before 'int'
int y = 20
^
1 error(s) found.
- Support for more C language features (e.g., enums, preprocessor directives).
- Improved error recovery.
- Integration with a language server for real-time syntax checking in an IDE.
This project is licensed under the MIT License.