A powerful C-based static code analysis tool that reads a C source file and performs advanced syntax checking, structural validation, and code analysis. This project goes beyond basic parsing and implements compiler-like logic, including comment removal, tokenization, scope tracking, and format validation.
This project is an improved version that highlights strong understanding of low-level programming in C, including memory management, pointer operations, and efficient data processing with modular design.
- ๐ Modular multi-file architecture (industry-style design)
- ๐ง Compiler-inspired architecture (Parser + Validator separation)
- ๐งฉ Scalable and extensible codebase
- ๐งช Includes unit testing for core components
- โก Efficient string parsing and memory handling
- ๐ Improved version with advanced parsing techniques
- ๐ง Implements mini-compiler level logic
- ๐ Detects real-world coding mistakes
- ๐ก Strong focus on clean code analysis
- Removes single-line (
//) and multi-line (/* */) comments - Eliminates blank and unnecessary lines
- Generates clean processed code
- Counts total valid lines of code
- Detects C keywords with line numbers
- Counts built-in functions (with frequency)
- Detects functions and validates prototypes
- Counts variables by data type:
int,char,float,double,long,short
- Supports arrays
- Calculates memory usage
- Detects:
- โ Undeclared variables
โ ๏ธ Duplicate variables (scope-based)
Advanced validation for:
printf,scanffprintf,fscanfgets,puts
Checks include:
- โ๏ธ Format specifier vs argument matching
- โ๏ธ Correct use of
&inscanf - โ๏ธ Invalid usage detection
- Stack-based bracket validation:
{ },( ),[ ]
- Detects:
- โ Unmatched brackets
- โ Mismatched pairs
- Ignores brackets inside strings โ
- Validates
forloop syntax:- Ensures correct number of
; - Checks balanced parentheses
- Ensures correct number of
- Validates
whileloop syntax
- Prints cleaned and analyzed code
- Writes formatted output to: output.txt
Basic unit tests are included for core modules:
test_parser.cโ Tests parsing logictest_validator.cโ Tests validation rulestest_runner.cโ Runs all test cases
Run tests by compiling:
gcc tests/*.c src/*.c -Iinclude -o tests
./tests๐ Each module follows separation of concerns with a clearly defined responsibility.
c-static-code-analyzer/
โ
โโโ main.c
โ
โโโ include/ # Header files (.h)
โ โโโ cleaner.h
โ โโโ file_handler.h
โ โโโ keyword.h
โ โโโ bracket.h
โ โโโ builtin.h
โ โโโ function.h
โ โโโ variables.h
โ โโโ parser.h
โ โโโ validator.h
โ โโโ syntax.h
โ โโโ undeclared.h
โ โโโ duplicate.h
โ
โโโ src/ # Implementation files (.c)
โ โโโ cleaner.c
โ โโโ file_handler.c
โ โโโ keyword.c
โ โโโ bracket.c
โ โโโ builtin.c
โ โโโ function.c
โ โโโ variables.c
โ โโโ parser.c
โ โโโ validator.c
โ โโโ syntax.c
โ โโโ undeclared.c
โ โโโ duplicate.c
โ
โโโ models/
โ โโโ fileline.h
โ
โโโ tests/
โ โโโ test_parser.c
โ โโโ test_validator.c
โ โโโ test_runner.c
โ
โโโ utils/
โ โโโ helpers.c
โ โโโ helpers.h
โ
โโโ data/
โ โโโ input.txt
โ โโโ output.txt
|
โโโ LICENSE
โโโ README.md
โโโ Makefile
| File | Description |
|---|---|
main.c |
Entry point; orchestrates all modules and controls execution flow |
file_handler.c |
Handles file reading and writing |
cleaner.c |
Removes comments and stores clean code |
keyword.c |
Identifies C keywords |
bracket.c |
Checks bracket balancing |
builtin.c |
Counts built-in functions |
function.c |
Counts functions and prototypes |
variables.c |
Counts variables and calculates memory |
parser.c |
Parses format strings and extracts components |
validator.c |
Validates I/O functions and syntax rules |
syntax.c |
Checks loop syntax (for, while) |
undeclared.c |
Finds undeclared variables |
duplicate.c |
Detects duplicate variable declarations |
input.txt |
Input C source file |
output.txt |
Output of analyzed code |
utils/ |
optional helper functions (currently unused, reserved for future common utilities) |
Makefile |
Build automation script |
This project follows a modular, layered design inspired by compiler architecture.
- Separation of Concerns
- Modular Components
- Reusable Parsing Logic
- Scalable Structure
- Parser โ Extracts structured data from raw code
- Validator โ Applies rules and detects errors
This separation improves:
- Maintainability
- Testability
- Reusability
Input C Code
โ
โผ
[ Cleaner ]
โ
โผ
[ Parser ] โโโโบ Extract Format / Variables
โ
โผ
[ Validator ] โโโโบ Check printf/scanf, syntax
โ
โผ
[ Analyzer Modules ]
โ
โผ
Output (Console + File)
- Reads C file line-by-line
- Removes comments and cleans code
- Stores lines using structured format
- Applies multiple analysis modules:
- Keyword detection
- Syntax validation
- Variable tracking
- Function analysis
- Outputs results to console + file
------ CLEAN CODE ------
Line 1 [18]: #include<stdio.h>
Line 2 [11]: int main()
------ BRACKET CHECK ------
All brackets are balanced correctly โ
------ KEYWORDS ------
Line 2: int
------ BUILTIN FUNCTIONS ------
printf: 2
------ UNDECLARED VARIABLE CHECK ------
Line 8: x is undeclared
------ DUPLICATE VARIABLE CHECK ------
Line 11: Duplicate variable 'a'
- C
- File Handling
- Structures
- String Parsing
- Tokenization (
strtok) - Pointers & Memory Management
- Stack Implementation
- Function Pointers
- Static Code Analysis
- Modular multi-file architecture
- Compiler-inspired design (parser + validator separation)
- Added comment removal (
//,/* */) - Implemented undeclared variable detection
- Added duplicate variable detection (scope-aware)
- Improved
printf/scanfvalidation using format specifiers - Stack-based bracket matching
- Advanced variable counting (arrays + memory)
- Token-based parsing (
strtok) - Function pointer-based validation system
- Basic syntax checker
- Keyword detection
- Simple bracket matching
- Basic variable and function counting
- Support nested scopes more accurately
- Detect unused variables
- Perform type checking across assignments
- Multi-file analysis support
- GUI or Web-based interface
This project simulates the front-end of a compiler, focusing on:
- Lexical-level parsing
- Syntax validation
- Static code analysis
It demonstrates strong understanding of:
- Parsing techniques
- Code analysis strategies
- Modular system design
- Low-level memory handling and pointer-based operations in C
โ ๏ธ Requiresgccinstalled (Linux / macOS / Windows with MinGW)
git clone https://github.com/KhushpreetKaur10/c-static-code-analyzer.git
cd c-static-code-analyzermakePlace your C source code inside:
input.txt
make run- Console โ Displays analysis results
- File โ Output saved in:
output.txt
make testmake cleangcc src/*.c main.c -Iinclude -o analyzer./analyzergcc tests/*.c src/*.c -Iinclude -o tests
./testsUsing the Makefile is recommended for easier builds and better project management as the project grows.
Khushpreet Kaur
Computer Science Student
๐ GitHub: https://github.com/KhushpreetKaur10
Give it a โญ on GitHub and feel free to contribute!