Mini Compiler in C (Lexical Analysis & Syntax Checker Example)


This project demonstrates a Mini Compiler in C that performs basic lexical analysis and syntax checking for a simplified subset of C programs. It identifies keywords, identifiers, constants, operators, and delimiters. This helps beginners understand compiler fundamentals.

1. Features

  1. Identify keywords
  2. Detect identifiers
  3. Detect constants (numbers)
  4. Detect operators (+, -, *, /, =)
  5. Detect delimiters (;, {}, ())
  6. Basic syntax checking for simple statements

2. Structure


#include <stdio.h>
#include <string.h>
#include <ctype.h>

char keywords[32][10] = {
"int","float","char","double","if","else","for","while",
"do","return","break","continue","void","switch","case","default",
"struct","typedef","union","enum","const","sizeof","long","short",
"unsigned","signed","extern","register","volatile","static","auto","goto"
};

3. Helper Functions


// Check if a word is a keyword
int isKeyword(char str[]) {
for(int i = 0; i < 32; i++) {
if(strcmp(str, keywords[i]) == 0)
return 1;
}
return 0;
}

// Check if character is an operator
int isOperator(char ch) {
return (ch=='+' || ch=='-' || ch=='*' || ch=='/' || ch=='=');
}

// Check if character is a delimiter
int isDelimiter(char ch) {
return (ch==' '|| ch==',' || ch==';' || ch=='(' || ch==')' || ch=='{' || ch=='}');
}

4. Lexical Analysis Example


void lexicalAnalyzer(char code[]) {
int i = 0;
char temp[50];
int j = 0;

while(code[i] != '\0') {
if(isOperator(code[i])) {
printf("%c --> Operator\n", code[i]);
} else if(isDelimiter(code[i])) {
if(j != 0) {
temp[j] = '\0';
if(isKeyword(temp))
printf("%s --> Keyword\n", temp);
else if(isdigit(temp[0]))
printf("%s --> Constant\n", temp);
else
printf("%s --> Identifier\n", temp);
j = 0;
}
} else {
temp[j++] = code[i];
}
i++;
}
}

5. Main Function with Sample Code


int main() {
char code[200];

printf("Enter C code (single line): ");
fgets(code, sizeof(code), stdin);

printf("\nLexical Analysis:\n");
lexicalAnalyzer(code);

return 0;
}

6. Sample Input/Output

Input:


int a = 5;

Output:


int --> Keyword
a --> Identifier
= --> Operator
5 --> Constant
; --> Delimiter

7. Key Points to Remember

  1. This mini compiler performs lexical analysis only
  2. It identifies keywords, identifiers, constants, operators, and delimiters
  3. Can be extended to include syntax checking, expression evaluation, and simple code interpretation
  4. Great beginner project to understand compiler design concepts

This completes your Phase-9 projects roadmap including:

  1. Student Management System
  2. Library Management System
  3. Banking System
  4. Address Book using File Handling
  5. Mini Compiler Project