Skip to content

About

A modular C preprocessor for source code analysis: supports recursive #include expansion, comment removal, identifier validation, and detailed logging. Robust error handling and fully tested.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

39 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

myPreCompiler

myPreCompiler is an educational preprocessor written in C. It expands local includes, removes comments without corrupting strings and character literals, validates names in supported declarations, and produces optional statistics.

Building and testing

It requires a C11 compiler and make.

make
make test
make sanitize

make test runs automated regression tests; make sanitize rebuilds the project with AddressSanitizer and UndefinedBehaviorSanitizer. GitHub CI runs both on every push and pull request.

Releases

Every push to main creates a GitHub Release after all checks pass. The workflow automatically creates a version tag in the v0.0.<run-number> format and attaches the Linux x86-64 executable and the PDF report generated from report/report.tex.

Usage

./myPreCompiler.out [-v] [-o output.c] -i input.c
./myPreCompiler.out [-v] [-o output.c] input.c
  • -i file: input C file;
  • -o file: output destination, or stdout if omitted;
  • -v: statistics and detailed diagnostics on stderr.

The program rejects identical input and output paths, preventing accidental truncation of the source file.

Behavior

Includes

Quoted includes are expanded recursively. Relative paths are resolved against the directory of the file containing the directive, as is normally done for #include "file.h". System includes enclosed in angle brackets are preserved.

A stack of canonical paths detects cycles and prints the chain that caused them. A defensive limit of 64 levels is also enforced.

Comments

A state machine distinguishes code, // and /* ... */ comments, strings, character literals, and escape sequences. Newlines within comments are preserved to keep line numbers unchanged. An unterminated block comment is treated as an error.

Lines are read using getline, so there is no fixed 4096-character limit.

Identifiers

The validator checks simple declarations, qualifiers, pointers, arrays, initializers, and multiple comma-separated declarators. A name must comply with C lexical syntax and cannot be a reserved word.

This project is not a replacement for a complete C parser: particularly complex declarators, macros that generate declarations, and user-defined types may require a real frontend, such as Clang.

Project structure

include/myPreCompiler.h  interface and statistics structures
src/main.c                CLI and stream management
src/preprocessor.c        includes, comment lexer, and declarations
src/utils.c               statistics, memory, and lexical validation
tests/                     automated suite and demonstration sources
report/                    LaTeX source and report Makefile

Test coverage

The suite checks strings containing comment-like sequences, character literals and escape sequences, block comments, identifiers and initializers, lines longer than 4096 bytes, a slash at the end of a file, nested includes independent of the working directory, cyclic includes, system includes, unterminated comments, and input/output protection.

Report

make report

This command generates report.pdf in the repository root from report/report.tex.

About

A modular C preprocessor for source code analysis: supports recursive #include expansion, comment removal, identifier validation, and detailed logging. Robust error handling and fully tested.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages