Skip to content

XO9A8/syntax-checker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C Syntax Checker

This is a C syntax checker built using Flex and Bison. It parses C code and reports any syntax errors found.

Features

  • 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.

Requirements

  • flex
  • bison
  • gcc or clang
  • make

Building and Running

Building

You can build the parser by following these steps:

  1. 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, and tokenizer.c.

  2. Compile the generated C files and create the executable:

    gcc -o parser parser.c tokenizer.c

    This will create the parser executable.

Running

To check the syntax of a C file, run the parser executable with the file as an argument:

./parser path/to/your/file.c

Example:

./parser tests/comprehensive_test.c

If 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.

Running on Different Operating Systems

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-essential

macOS:

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 bison

Xcode Command Line Tools are also required, which can be installed with:

xcode-select --install

Windows:

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.

Test Suite

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.

Error Reporting

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.

Future Improvements

  • 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.

License

This project is licensed under the MIT License.

About

This is a syntax checker cli for C language made using flex and bison

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published