Skip to content

Latest commit

 

History

15 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C Lexical Analyzer - Compiler Design Course Project

Course: Compiler Design
Institution: Notre Dame University Bangladesh (NDUB)
Lecturer: Humayara Binte Rashid
Email: humayara@ndub.edu.bd


Team Members

  • Syed Nafish Shakir
  • Joynob Bint Jamal
  • Shanon Sheikh

Project Overview

This project is a lexical analyzer (tokenizer) for the C programming language, developed as part of the Compiler Design course. The analyzer is built using Flex/Lex and can identify various lexical elements in C source code including keywords, identifiers, operators, literals, and separators while providing detailed statistical analysis.

Status: WIP


Learning Objectives

Through this project, we aim to understand:

  • Phase 1 of Compilation: Lexical Analysis
  • Regular Expressions: Pattern matching for token recognition
  • Finite Automata: Theory behind lexical analyzers
  • Flex/Lex Tool: Practical implementation of lexical analyzers
  • Token Classification: Different types of lexical units in programming languages

Current Features

Token Recognition

  • C Keywords: All 32 C keywords (int, float, if, while, return, etc.)
  • Identifiers: Variable and function names with length information
  • Numeric Literals:
    • Integers (e.g., 42, 123)
    • Floating-point numbers with scientific notation (e.g., 3.14, 1.5e-10)
  • Relational Operators: <, <=, >, >=, ==, !=
  • Separators/Punctuation: ;, ,, ., :, #, (, ), {, }, [, ]
  • Arrow Operator: ->

Analysis Features

  • File-based Input: Reads C source files from command line
  • Statistical Counting: Words, spaces, tabs, and lines
  • Error Handling: File validation and user-friendly error messages
  • Formatted Output: Clear token classification and summary

Supported Token Categories

Category Examples Status
Keywords int, if, return ✅ Complete
Identifiers main, variable_name ✅ Complete
Integer Literals 42, 999 ✅ Complete
Float Literals 3.14, .5 ✅ Complete
Relational Operators <, <=, ==, != ✅ Complete
Separators (, ), {, }, ; ✅ Complete
Assignment Operators =, +=, -= ✅ Complete
Arithmetic Operators +, -, *, /, % ✅ Complete
String Literals "Hello World" ✅ Complete
Character Literals 'a', '\n' ✅ Complete
Comments //, /* */ ✅ Complete

Development Progress

Phase 1: Basic Lexical Analysis

  • Setup Flex environment
  • Define basic token patterns
  • Implement keyword recognition
  • Add identifier and literal support
  • File input/output handling
  • Statistical counting

Phase 2: Extended Token Support

  • Assignment operators (=, +=, -=, etc.)
  • Arithmetic operators (+, -, *, /, %)
  • Logical operators (&&, ||, !)
  • String and character literals
  • Preprocessor directives

Phase 3: Advanced Features

  • Comment handling (//, /* */)
  • Error recovery and reporting
  • Symbol table integration
  • Multi-file analysis
  • Token position tracking

Current Limitations

  • Assignment operators show as "Unknown character"
  • String literals are not properly tokenized
  • Comments are not handled
  • Arithmetic operators need implementation
  • Escape sequences in strings/characters not supported

Testing

Test Cases

  1. Basic C Program: Simple main function with variables
  2. Control Structures: if-else, loops, switch statements
  3. Functions: Function declarations and calls
  4. Data Types: All C primitive types
  5. Edge Cases: Empty files, syntax errors

Disclaimer: This is an educational project for Notre Dame University Bangladesh's Compiler Design course. The implementation is for learning purposes and may not be suitable for production use.

Last Updated: October 2025
Project Status: Work in Progress

About

Designing the C compiler

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages