Skip to content

Latest commit

ย 

History

38 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ C Static Code Analyzer (Syntax Checker) v2.0

Language Status Version License

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.


โœจ Highlights

  • ๐Ÿš€ 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

๐Ÿง  Features

๐Ÿ”น Code Preprocessing

  • Removes single-line (//) and multi-line (/* */) comments
  • Eliminates blank and unnecessary lines
  • Generates clean processed code

๐Ÿ”น Code Analysis

  • Counts total valid lines of code
  • Detects C keywords with line numbers
  • Counts built-in functions (with frequency)
  • Detects functions and validates prototypes

๐Ÿ”น Variable Analysis

  • Counts variables by data type:
    • int, char, float, double, long, short
  • Supports arrays
  • Calculates memory usage
  • Detects:
    • โŒ Undeclared variables
    • โš ๏ธ Duplicate variables (scope-based)

๐Ÿ”น Syntax Validation

Advanced validation for:

  • printf, scanf
  • fprintf, fscanf
  • gets, puts

Checks include:

  • โœ”๏ธ Format specifier vs argument matching
  • โœ”๏ธ Correct use of & in scanf
  • โœ”๏ธ Invalid usage detection

๐Ÿ”น Structural Checks

  • Stack-based bracket validation:
    • { }, ( ), [ ]
  • Detects:
    • โŒ Unmatched brackets
    • โŒ Mismatched pairs
  • Ignores brackets inside strings โœ…

๐Ÿ”น Control Structure Validation

  • Validates for loop syntax:
    • Ensures correct number of ;
    • Checks balanced parentheses
  • Validates while loop syntax

๐Ÿ”น Output

  • Prints cleaned and analyzed code
  • Writes formatted output to: output.txt

๐Ÿงช Testing

Basic unit tests are included for core modules:

  • test_parser.c โ†’ Tests parsing logic
  • test_validator.c โ†’ Tests validation rules
  • test_runner.c โ†’ Runs all test cases

Run tests by compiling:

gcc tests/*.c src/*.c -Iinclude -o tests
./tests

๐Ÿ—๏ธ Project Structure

๐Ÿ“Œ 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

๐Ÿ“Š Key Modules Overview

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

๐Ÿงฉ Architecture

This project follows a modular, layered design inspired by compiler architecture.

๐Ÿ”น Key Design Principles

  • Separation of Concerns
  • Modular Components
  • Reusable Parsing Logic
  • Scalable Structure

๐Ÿ”น Parser vs Validator (Important Design Choice)

  • Parser โ†’ Extracts structured data from raw code
  • Validator โ†’ Applies rules and detects errors

This separation improves:

  • Maintainability
  • Testability
  • Reusability

๐Ÿงญ Architecture Flow

Input C Code
โ”‚
โ–ผ
[ Cleaner ]
โ”‚
โ–ผ
[ Parser ] โ”€โ”€โ”€โ–บ Extract Format / Variables
โ”‚
โ–ผ
[ Validator ] โ”€โ”€โ”€โ–บ Check printf/scanf, syntax
โ”‚
โ–ผ
[ Analyzer Modules ]
โ”‚
โ–ผ
Output (Console + File)

โš™๏ธ How It Works

  1. Reads C file line-by-line
  2. Removes comments and cleans code
  3. Stores lines using structured format
  4. Applies multiple analysis modules:
    • Keyword detection
    • Syntax validation
    • Variable tracking
    • Function analysis
  5. Outputs results to console + file

๐Ÿงช Example Output

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

๐Ÿ› ๏ธ Technologies Used

๐Ÿ’ป Language

  • C

๐Ÿ“š Concepts

  • File Handling
  • Structures
  • String Parsing
  • Tokenization (strtok)
  • Pointers & Memory Management
  • Stack Implementation
  • Function Pointers
  • Static Code Analysis

๐Ÿ”„ Changelog

๐Ÿš€ Version 2.0 (Latest)

  • 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/scanf validation using format specifiers
  • Stack-based bracket matching
  • Advanced variable counting (arrays + memory)
  • Token-based parsing (strtok)
  • Function pointer-based validation system

๐ŸŸข Version 1.0

  • Basic syntax checker
  • Keyword detection
  • Simple bracket matching
  • Basic variable and function counting

๐Ÿ”ฎ Future Improvements

  • Support nested scopes more accurately
  • Detect unused variables
  • Perform type checking across assignments
  • Multi-file analysis support
  • GUI or Web-based interface

๐Ÿ’ก Why This Project?

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

โ–ถ๏ธ How to Run

โš ๏ธ Requires gcc installed (Linux / macOS / Windows with MinGW)


๐Ÿ”น Method 1: Using Makefile (Recommended)

1. Clone the Repository

git clone https://github.com/KhushpreetKaur10/c-static-code-analyzer.git
cd c-static-code-analyzer

2. Build the Project

make

3. Add Input File

Place your C source code inside:

input.txt

4. Run the Analyzer

make run

5. View Output

  • Console โ†’ Displays analysis results
  • File โ†’ Output saved in:
output.txt

6. Run Tests (Optional)

make test

7. Clean Build Files

make clean

๐Ÿ”น Method 2: Manual Compilation

1. Compile the Project

gcc src/*.c main.c -Iinclude -o analyzer

2. Run the Program

./analyzer

3. Run Tests (Optional)

gcc tests/*.c src/*.c -Iinclude -o tests
./tests

๐Ÿ’ก Tip

Using the Makefile is recommended for easier builds and better project management as the project grows.


๐Ÿ‘จโ€๐Ÿ’ป Author

Khushpreet Kaur
Computer Science Student

๐Ÿ”— GitHub: https://github.com/KhushpreetKaur10


โญ If you like this project

Give it a โญ on GitHub and feel free to contribute!

About

A C-based static code analyzer that performs advanced syntax validation, comment removal, variable analysis (including undeclared and duplicate detection), function tracking, and format specifier checking using compiler-like parsing techniques.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages