Professional Documents
Culture Documents
Compiler Isha
Compiler Isha
INSTITUTIONS
B.TECH. COMPUTER
SCIENCE 5th SEMESTER
SUBMITTED TO:
PROF.NIHARIKA NAMDEV
NAME:Isha Rawat
CLASS:G1-A
ROLL.NO:16081
COMPILER DESIGN LAB (KCS 552)
List of Experiments
1. Students are advised to come to the laboratory at least 5 minutes before (to
starting time), those who come after 5 minutes will not be allowed into the
lab.
2. Plan your task properly much before to the commencement, come
prepared to the lab withthe synopsis / program / experiment details.
3. Student should enter into the laboratory with:
a. Laboratory observation notes with all the details (Problem statement,
Aim, Algorithm, Procedure, Program, Expected Output, etc.,) filled in
for the lab session.
b. Laboratory Record updated up to the last session experiments and
other utensils (if any)needed in the lab.
c. Proper Dress code and Identity card.
4. Sign in the laboratory login register, write the TIME-IN, and occupy the
computer systemallotted to you by the faculty.
5. Execute your task in the laboratory, and record the results / output in
the lab observationnote book, and get certified by the concerned faculty.
6. All the students should be polite and cooperative with the laboratory staff,
must maintain thediscipline and decency in the laboratory.
7. Computer labs are established with sophisticated and high end branded
systems, which shouldbe utilized properly.
8. Students / Faculty must keep their mobile phones in SWITCHED OFF
mode during the lab sessions. Misuse of the equipment, misbehaviors
with the staff and systems etc., will attract severe punishment.
9. Students must take the permission of the faculty in case of any urgency to go
out ; if anybody found loitering outside the lab / class without permission
during working hours willbe treated seriously and punished appropriately.
10.Students should LOG OFF/ SHUT DOWN the computer system before he/
she leaves the lab after completing the task (experiment) in all aspects. He/
she must ensure the system / seat is kept properly.
\
SOFTWARE REQUIREMENTS
SOFTWARE
#include<stdio.h>
#include<string.h
>
#include<stdlib.h
> void main()
{
char s[20],c;
int state=0,i=0;
printf("\n enter a
string:"); scanf("\n%s",s);
while(s[i]!='\0')
{
switch(state)
{
case 0:
c=s[i++];
if(c=='a'
) state
=1;
else
if(c=='b') state=2;
else
state=6
; break;
case 1:
c=s[i++];
if(c=='a'
) state=3;
else
if(c=='b') state=4;
els
e state=6;
break;
case 2:
c=s[i++];
if(c=='a'
) state=6;
else if(c=='b')
state=2;
els
e state=6;
break;
case 3:
c=s[i++];
if(c=='a'
) state=3;
else
if(c=='b') state=2;
els
e state=6;
break;
case 4:
c=s[i++];
if(c=='a'
) state=6;
else
if(c=='b') state=5;
els
e state=6;
break;
case 5:
c=s[i++];
if(c=='a'
) state=6;
else
if(c=='b') state=2;
els
e state=6;
break;
case 6:
printf("%s is not
recognised.",s); exit(0);
}
}
if((state==1)||(state==3)||(state==0))
printf("\n %s is accepted under rule
'a*'",s); else if((state==2)||(state==4))
printf("\n %s is accepted under rule 'a*b+'",s);
else if(state==5)
printf("\n %s is accepted under rule 'abb'",s);
PROGRAM N0:02
%%
%%
int main() {
yylex();
return 0;
}
lex lexer.l
gcc lex.yy.c -o lexer -ll
int x = 42;
float y = 3.14;
./lexer < input.txt
output
Type: int
Identifier: x
Unknown token: =
Number: 42
Type: float
Identifier: y
Unknown token: =
Number: 3.14
PROGRAM:03
%%
[\t]
+ is |
am
| are|
was|
were{printf(“%s: is a verb”,yytext);}
[a-zA-Z]+{printf(“%s: is not a verb,yytext);}
%%
main()
{
yylex();
}
OUTPUT: $ ./a.out
are
are:is a verb
PROGRAM NO:04
%{
#include<stdio.h>
int num_lines=0,num_chars=0;
%}
%%
\n {++num_lines; ++num_chars;}
. {++num_chars;}
%%
int main()
{
yylex();
printf("There are %d lines and %d characters. \n", num_lines,num_chars);
return 0;
}
Lex code:
%{
#include <stdio.h>
#include "y.tab.h"
%}
%%
[ \t]+ ; // ignore all whitespace
%%
Yacc:
%{
#include "lex.yy.c"
#include <stdio.h>
int yywrap() {
return 1;
}
int main(){
printf("Enter the expression:\n");
yyparse();
return 0;
}
%}
/* Debinitions */
%token NUMBER
%token ID
%% /* Rules */
E : T {
T:
T '+' T { $$ = $1 + $3; }
| T '-' T { $$ = $1 - $3; }
| T '*' T { $$ = $1 * $3; }
| T '/' T { $$ = $1 / $3; }
| T '**' T { $$ = $1 * $3; }
| SIN T { $$ = sin($2); }
| COS T { $$ = cos($2); }
| TAN T { $$ = tan($2); }
| '(' T ')' { $$ = $2; }
| NUMBER { $$ = $1; }
| ID { $$ = $1;
%%
PROGRAM NO:06
PROGRAM:Write program to find ε – closure of all states of any given NFA
with ε transition
#include <stdio.h>
#include <stdlib.h>
#define MAX_STATES 10
#define MAX_TRANSITIONS 10
// Structure to represent a state in the NFA
struct State {
int id;
int transitions[MAX_TRANSITIONS];
};
int main() {
int n; // Number of states in NFA
int epsilon[MAX_STATES][MAX_STATES]; // ε-transition matrix
return 0;
}
OUTPUT
gcc -o epsilon_closure epsilon_closure.c
./epsilon_closure
011
001
000
ε-Closure(q0) = { q0 q1 q2 }
ε-Closure(q1) = { q1 q2 }
ε-Closure(q2) = { q2 }
PROGRAM NO:07
#define MAX_STATES 10
#define MAX_ALPHABET 10
int main() {
int n; // Number of states in NFA
int alphabetSize; // Size of the input alphabet
int epsilon[MAX_STATES][MAX_STATES]; // ε-transition matrix
struct State nfaStates[MAX_STATES]; // NFA states
return 0;
}
OUTPUT
gcc -o nfa_conversion nfa_conversion.c
./nfa_conversion
011
001
000
State q0:
on input a: 1 -1
on input b: 2 -1
State q1:
on input a: 1 -1
on input b: 2 -1
State q2:
on input a: -1
on input b: -1
PROGRAM NO:08
PROGRAM:Write program to convert NFA to DFA.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STATES 10
#define MAX_ALPHABET 10
// Get the set of NFA states represented by the current DFA state
for (i = 0; i < MAX_STATES; i++) {
currentSet[i] = -1;
}
int setIndex = 0;
for (i = 0; i < n; i++) {
if (visited[i] == 1 && queue[front][setIndex] != -1) {
currentSet[setIndex] = i;
setIndex++;
}
}
// For each input symbol, compute the set of NFA states reachable from the
current set on that symbol
for (i = 0; i < alphabetSize; i++) {
move(n, currentSet, alphabetSize, nfaStates, epsilon, resultStates);
// Set the transition from the current DFA state to the newly computed DFA
state
dfaStates[currentState].transitions[i] = existingState;
}
}
}
int main() {
int n; // Number of states in NFA
int alphabetSize; // Size of the input alphabet
int epsilon[MAX_STATES][MAX_STATES]; // ε-transition matrix
struct NFAState nfaStates[MAX_STATES]; // NFA states
struct DFAState dfaStates[MAX_STATES]; // DFA states
// Convert to DFA
convertToDFA(n, alphabetSize, epsilon, nfaStates, dfaStates);
return 0;
}
OUTPUT
gcc -o nfa_to_dfa nfa_to_dfa.c
./nfa_to_dfa
011
001
000
DFA:
State q0:
on input a: q1
on input b: q2
State q1:
on input a: q1
on input b: q2
State q2:
on input a: q2
on input b: q2
PROGRAM NO:9
PROGRAM: Write program to minimize any given DFA.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_STATES 10
#define MAX_ALPHABET 10
struct Partition {
int state;
struct Partition* next;
};
struct State {
int id;
int transitions[MAX_ALPHABET];
};
struct PartitionSet {
struct Partition* partitions[MAX_STATES];
};
// Function to find the set of states that are distinguishable from each other
void findDistinguishableStates(int n, int alphabetSize, struct State states[MAX_STATES], int
distinguishable[MAX_STATES][MAX_STATES]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
distinguishable[i][j] = 1; // Initialize as distinguishable
}
}
initializePartitionSet(n, &partitionSet1);
initializePartitionSet(n, &partitionSet2);
int p;
for (int i = 0; i < n; i++) {
if (isInPartition(i, partitionSet1.partitions[0])) {
p = 0;
} else {
p = 1;
}
partitions[i] = p;
}
int previousPartitionCount = 0;
do {
previousPartitionCount = *partitionCount;
initializePartitionSet(n, &partitionSet2);
if (partition1 != partition2) {
if (isInPartition(i, partitionSet2.partitions[partition1])) {
addToPartition(&partitionSet2.partitions[partition1], j);
} else {
addToPartition(&partitionSet2.partitions[partition2], j);
}
}
}
}
}
*partitionCount = 0;
for (int i = 0; i < n; i++) {
if (isPartitionInSet(partitionSet2.partitions[i], &partitionSet1, n)) {
partitions[i] = *partitionCount;
(*partitionCount)++;
} else {
partitions[i] = *partitionCount - 1;
}
}
int main() {
int n; // Number of states in DFA
int alphabetSize; // Size of the input alphabet
struct State states[MAX_STATES]; // DFA states
int finalStates[MAX_STATES]; // Final states
int partitions[MAX_STATES]; // Partition for each state
int partitionCount = 0; // Number of partitions
return 0;
}
OUTPUT
Minimized DFA:
State q0:
on input a: q1
on input b: q2
State q1:
on input a: q1
on input b: q0
State q2:
on input a: q2
on input b: q0
Final States: q0
PROGRAM NO:10
PROGRAM: Develop an operator precedence parser for a given language .
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
void main()
{
char grm[20][20], c;
while (c != '\0') {
flag = 1;
else {
flag = 0;
f();
}
if (c == '$') {
flag = 0;
f();
}
c = grm[i][++j];
}
}
if (flag == 1)
printf("Operator grammar");
}
OUTPUT
Input :3
A=A*A
B=AA
A=$
Input :2
A=A/A
B=A+A