Skip to content

amrutha0001/C-Like-Language-Compiler-Implementation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 

Repository files navigation

🧠 C-Like Language Compiler Implementation Using C

This project is a simple C compiler that implements a lexical analyzer for a subset of the C programming language. It reads multi-line user input, categorizes tokens like keywords, identifiers, constants, operators, and punctuation, and outputs detailed token statistics.

πŸ“‚ Directory Structure

Compiler/
β”‚
β”œβ”€β”€ codes/                  # C source files for lexical analyzer, parser, and other logic
β”‚   β”œβ”€β”€ lex.c               # Lexical analyzer implementation
β”‚   β”œβ”€β”€ parse.c             # Parsing logic
β”‚   β”œβ”€β”€ run.c               # Code execution logic
β”‚   β”œβ”€β”€ main.c              # Main entry point
β”‚
β”œβ”€β”€ examples/               # Example input files to test the compiler
β”‚   β”œβ”€β”€ filename.txt
β”‚   β”œβ”€β”€ countdigits.txt
β”‚   β”œβ”€β”€ factorial.txt
β”‚   β”œβ”€β”€ fibonacci.txt
β”‚   β”œβ”€β”€ grades.txt
β”‚   β”œβ”€β”€ largestof3.txt
β”‚   β”œβ”€β”€ power.txt
β”‚   β”œβ”€β”€ printeven.txt
β”‚   β”œβ”€β”€ reverse.txt
β”‚   β”œβ”€β”€ smallestof3.txt
β”‚   β”œβ”€β”€ sum.txt
β”‚   β”œβ”€β”€ swap.txt
β”‚   β”œβ”€β”€ tables.txt
β”‚   β”œβ”€β”€ array1.txt
β”‚   β”œβ”€β”€ array2.txt
β”‚   β”œβ”€β”€ array3.txt
β”‚
β”œβ”€β”€ obj/                    # Object files directory (Created through commands)
β”‚
β”œβ”€β”€ outputs/                # Outputs directory (Created through commands)
β”‚
β”œβ”€β”€ project report.pdf

πŸ“Œ Features

  • Keyword recognition: Identifies keywords like int, float, if, else, switch
  • Identifier recognition: Supports variables starting with alphabetic characters or underscores
  • Constant recognition: Identifies numeric constants, including decimals
  • Operator recognition: Supports arithmetic and logical operators like +, -, *, /, ==, !=, &&, ||
  • Punctuation recognition: Recognizes common punctuation such as ;, ,, {, }, (, )
  • Finite State Machine (FSM): Used for efficient token detection
  • Token classification: Categorizes tokens and stores them in separate lists
  • Token statistics: Outputs the number of tokens for each category

πŸš€ Getting Started

βœ… Requirements

  • GCC or any standard C compiler
  • Basic familiarity with command-line tools

πŸ› οΈ Compilation Instructions

Open your terminal and run the following commands in the project root:

mkdir -p obj
gcc -std=gnu11 -Wall -Werror -c codes/lex.c -o obj/lex.o 
gcc -std=gnu11 -Wall -Werror -c codes/parse.c -o obj/parse.o
gcc -std=gnu11 -Wall -Werror -c codes/run.c -o obj/run.o
gcc -std=gnu11 -Wall -Werror -c codes/main.c -o obj/main.o
gcc -o interpret obj/lex.o obj/parse.o obj/run.o obj/main.o

▢️ Running the Compiler Test the compiler using sample inputs:

./interpret examples/filename.txt
./interpret examples/countdigits.txt
./interpret examples/factorial.txt  
./interpret examples/fibonacci.txt
./interpret examples/grades.txt
./interpret examples/largestof3.txt
./interpret examples/power.txt
./interpret examples/printeven.txt
./interpret examples/reverse.txt
./interpret examples/smallestof3.txt
./interpret examples/sum.txt
./interpret examples/swap.txt
./interpret examples/tables.txt
./interpret examples/array1.txt
./interpret examples/array2.txt
./interpret examples/array3.txt

πŸ“˜ Learning Outcomes

Basics of compiler design and lexical analysis

Practical implementation of tokenization and FSM in C

Understanding string manipulation, character classification, and categorizing tokens

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages