Compiler Lab Experiments
Compiler Lab Experiments
OBJECT: Design and implement a lexical analyzer for given language using C
and the lexical analyzer should ignore redundant spaces, tabs, and new lines.
Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define MAX_KEYWORDS 10
// List of keywords
char *keywords[MAX_KEYWORDS] = {
"if", "else", "while", "do", "int", "float", "return", "char", "for", "break"
};
if (isKeyword(subStr))
printf("'%s' : Keyword\n", subStr);
else if (isNumber(subStr))
printf("'%s' : Number\n", subStr);
else
printf("'%s' : Identifier\n", subStr);
free(subStr);
}
if (isOperator(input[right]))
printf("'%c' : Operator\n", input[right]);
else if (input[right] != ' ' && input[right] != '\n' && input[right] != '\t')
printf("'%c' : Delimiter\n", input[right]);
right++;
left = right;
}
}
}
int main() {
char input[1000];
printf("Enter the source code (end input with EOF - Ctrl+D on Linux/Mac or Ctrl+Z on
Windows):\n");
fgets(input, sizeof(input), stdin);
lexicalAnalyzer(input);
return 0;
}
PROGRAM N0:02
Object: Implementation of Lexical Analyzer using Lex Tool.
%{
#include <stdio.h>
#include <stdlib.h>
%}
%%
%%
int main(int argc, char **argv) {
printf("Enter input code:\n");
yylex();
return 0;
}
int yywrap() {
return 1;
}
Output:
bash
CopyEdit
lex lexer.l
gcc lex.yy.c -o lexer
./lexer
int main() {
int a = 5;
float b = 10.5;
if (a < b) {
a = a + 1;
}
}
PROGRAM N0: 03
digit [0-9]
%%
. { return yytext[0]; }
%%
expr.y (YACC file)
%{
#include <stdio.h>
#include <stdlib.h>
%}
%token NUMBER
%left '+' '-'
%left '*' '/'
%%
input: expression '\n' { printf("Valid Expression\n"); return 0; }
;
expression:
expression '+' expression
| expression '-' expression
| expression '*' expression
| expression '/' expression
| NUMBER
;
%%
int main() {
printf("Enter arithmetic expression: ");
yyparse();
return 0;
}
letter [a-zA-Z]
digit [0-9]
%%
%%
int main() {
yylex();
return 0;
}
🧪 Compile and test this file using lex identifier.l && gcc lex.yy.c -o id && ./id.
🔹 c) Calculator using LEX & YACC
calc.l (LEX file)
%{
#include "y.tab.h"
%}
%%
%%
calc.y (YACC file)
%{
#include <stdio.h>
#include <stdlib.h>
%}
%token NUMBER
%left '+' '-'
%left '*' '/'
%right UMINUS
%%
input:
| input line
;
line:
'\n'
| expression '\n' { printf("Result = %d\n", $1); }
;
expression:
expression '+' expression { $$ = $1 + $3; }
| expression '-' expression { $$ = $1 - $3; }
| expression '*' expression { $$ = $1 * $3; }
| expression '/' expression { $$ = $1 / $3; }
| '-' expression %prec UMINUS { $$ = -$2; }
| '(' expression ')' { $$ = $2; }
| NUMBER
;
%%
int main() {
printf("Enter expression:\n");
yyparse();
return 0;
}
void preorder(Node* t) {
if(t) {
printf("%s ", t->val);
preorder(t->left);
preorder(t->right);
}
}
%}
%union {
char* str;
Node* ptr;
}
%token <str> ID
%left '+' '-'
%left '*' '/'
%type <ptr> E T F
%%
S : E { printf("AST (Prefix): "); preorder($1); printf("\n"); return 0; };
%%
#include "lex.yy.c"
int main() {
printf("Enter expression: ");
yyparse();
return 0;
}
%%
%%
PROGRAM NO:04
#define MAX 10
int epsilon[MAX][MAX];
int eclosure[MAX][MAX];
int visited[MAX];
int n;
int main() {
int i, j;
printf("Enter epsilon transitions (Enter 1 if there is ε-transition from state i to state j, else
0):\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &epsilon[i][j]);
}
}
visited[i] = 1;
eclosure[i][i] = 1;
findEclosure(i, i);
}
return 0;
}
PROGRAM NO:05
#define MAX 10
int main() {
int i, j, k;
// Input ε-transitions
printf("Enter epsilon transitions matrix:\n");
for (i = 0; i < states; i++)
for (j = 0; j < states; j++)
scanf("%d", &epsilon[i][j]);
// Remove ε-transitions
for (i = 0; i < states; i++) {
for (k = 0; k < symbols; k++) {
for (j = 0; j < states; j++) {
if (eclosure[i][j]) {
for (int m = 0; m < states; m++) {
if (trans[j][m][k])
result[i][m] = 1;
}
}
}
}
}
return 0;
}
PROGRAM NO:06
6. Convert NFA to DFA
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 20
void print_dfa() {
printf("\nDFA Transition Table:\n");
for (int i = 0; i < dfa_state_count; i++) {
printf("DFA State %d: { ", i);
for (int j = 0; j < n; j++) if (dfa_states[i][j]) printf("q%d ", j);
printf("}\n");
for (int k = 0; k < symbol_count; k++) {
printf(" on '%c' -> ", 'a' + k);
for (int j = 0; j < n; j++) {
if (dfa[i][k] == j) {
printf("DFA State %d\n", j);
break;
}
}
}
}
}
int main() {
int i, j, k;
if (!exists_flag) {
state_index = dfa_state_count;
add_state(new_state);
}
dfa[i][k] = state_index;
}
}
print_dfa();
return 0;
}
PROGRAM NO:07
void push(char c) {
stack[++top] = c;
}
void pop() {
top--;
}
void operatorPrecedenceParser() {
printf("Enter expression: ");
scanf("%s", input);
push('$');
char c;
c = input[i++];
while (c != '\0') {
if (c == '+' || c == '-' || c == '*' || c == '/') {
while (precedence(stack[top]) >= precedence(c)) {
printf("%c", stack[top]);
pop();
}
push(c);
} else {
printf("%c", c);
}
c = input[i++];
}
while (stack[top] != '$') {
printf("%c", stack[top]);
pop();
}
printf("\n");
}
int main2() {
operatorPrecedenceParser();
return 0;
}
PROGRAM NO:08
// PROGRAM 3: First and Follow Simulation (static example)
// Grammar: E -> TE'
// E' -> +TE' | ε
// T -> FT'
// T' -> *FT' | ε
// F -> (E) | id
// FIRST(E) = FIRST(T) = FIRST(F) = { '(', id }
// FOLLOW(E) = { $, ')' }
// FOLLOW(T) = { +, $, ')' }
// PROGRAM 8: Recursive Descent Parser
#include <stdio.h>
#include <string.h>
char input[10];
int i = 0;
void E();
void E1();
void T();
void T1();
void F();
void E() { T(); E1(); }
void E1() {
if (input[i] == '+') {
i++; T(); E1();
}
}
void T() { F(); T1(); }
void T1() {
if (input[i] == '*') {
i++; F(); T1();
}
}
void F() {
if (input[i] == '(') {
i++; E();
if (input[i] == ')') i++;
else printf("Missing closing parenthesis\n");
} else if (isalnum(input[i])) {
i++;
} else {
printf("Invalid character\n");
}
}
int main3() {
printf("Enter expression: ");
scanf("%s", input);
E();
if (input[i] == '\0') printf("Valid expression\n");
else printf("Invalid expression\n");
return 0;
}
// PROGRAM 9: Loop Unrolling
#include <stdio.h>
int main4() {
int i;
printf("Loop Unrolling Example (Original Loop):\n");
for (i = 0; i < 8; i++) printf("i = %d\n", i);