Compiler Lab Experiments
Compiler Lab Experiments
PROGRAM NO:-1
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"
};
FacultyName:- Sign.WithDate
Experiment Date
FacultyName:- Sign.WithDate
Experiment Date
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;
}
}
}
FacultyName:- Sign.WithDate
Experiment Date
int main() {
char input[1000];
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
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;
}
Input:-
1; Output:-
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
PROGRAM N0:02
Object: Implementation of Lexical Analyzer using Lex Tool.
%{
#include <stdio.h>
#include <stdlib.h>
%}
%%
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
")" { printf("PUNCTUATION: )\n"); }
"{" { printf("PUNCTUATION: {\n"); }
"}" { printf("PUNCTUATION: }\n"); }
";" { printf("PUNCTUATION: ;\n"); }
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
[ \t\n]+ { /* Ignore whitespace */ }
%%
bash
CopyEdit
lex lexer.l
gcc lex.yy.c -o lexer
./lexer
int main()
{ int a =
5;
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
float b = 10.5;
if (a < b) {
a = a + 1;
}
}
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
PROGRAM N0: 03
digit [0-9]
%%
. { return yytext[0]; }
%%
expr.y (YACC file)
%{
#include <stdio.h>
#include <stdlib.h>
%}
%token NUMBER
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
%left '+' '-'
%left '*' '/'
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
%%
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;
}
Sample Input:-
Output:-
Name-Divya RollNo:2201320100064
%{
#include <stdio.h>
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
%}
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.
Command:-
Input:-
Output:-
Name-Divya RollNo:2201320100064
%%
%%
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; }
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
| '(' expression ')' { $$ = $2; }
| NUMBER
;
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
%%
int main() {
printf("Enter expression:\n");
yyparse();
return 0;
}
Output:-
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
Node* create(char *op, Node *l, Node *r) {
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
Node* t = (Node*)
malloc(sizeof(Node)); strcpy(t->val, op);
t->left = l;
t->right =
r; return t;
}
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; };
E : E '+' T { $$ = create("+", $1, $3); }
| E '-' T { $$ = create("-", $1, $3); }
|T { $$ = $1; };T : T '*' F { $$ = create("*", $1, $3); }
| T '/' F { $$ = create("/", $1, $3); }
|F { $$ = $1; };F : '(' E ')' { $$ = $2; }
| ID { $$ = create($1, NULL, NULL); };
%%
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
#include "lex.yy.c"
int main() {
printf("Enter expression: ");
yyparse();
return 0;
}
%%
%%
Input:-
Output:-
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
PROGRAM NO:04
#include <stdio.h>
#include <stdlib.h>
#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]);
}
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
}
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
visited[i] = 1;
eclosure[i][i] = 1;
findEclosure(i, i);
}
return 0;
}
eclosure.1(flex file)
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
Compilation:-
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
Input:-
Output:-
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
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]);
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
Name-Divya RollNo:2201320100064
}
return 0;
}
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
Input :-
Output :-
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
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);
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
for (int j = 0; j < n; j++) if (dfa_states[i][j]) printf("q%d ", j); printf("}\
n");
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
int main()
{ int i, j,
k;
printf("Enter number of NFA states: ");
scanf("%d", &n);
printf("Enter number of symbols: ");
scanf("%d", &symbol_count);
printf("Enter the transition table (for each symbol):\n");
for (k = 0; k < symbol_count; k++) {
printf("On symbol %c:\n", 'a' + k);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &nfa[i * symbol_count + k][j]);
}
}
}
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
if (dfa_states[i][j]) {
for (int m = 0; m < n; m++) {
if (nfa[j * symbol_count + k][m])
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
new_state[m] = 1;
}
}
}
if (!exists_flag) {
state_index = dfa_state_count;
add_state(new_state);
}
dfa[i][k] = state_index;
}
}
print_dfa();
return 0;
}
Input:-
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
Output:-
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
PROGRAM NO:07
void push(char c)
{ stack[++top] =
c;
}
void pop()
{ top--;
}
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
case '-': return
1; case '*':
case '/': return 2;
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
default: return 0;
}
}
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();
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
}
printf("\n");
}
int main2()
{ operatorPrecedenceParser();
return 0;
}
Input:-
Output:-
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
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() {
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
if (input[i] == '(')
{ i++; E();
if (input[i] == ')') i++;
else printf("Missing closing parenthesis\n");
} else if (isalnum(input[i]))
{ i++;
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
} 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;
}
Valid Input:-
Invalid input:-
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
// 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);
OUTPUT:
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
OUTPU
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
Output:
FacultyName:- Sign.WithDate
Experiment Date
Name-Divya RollNo:2201320100064
Output:
FacultyName:- Sign.WithDate