Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 43

Register Number: RA1611003010165

EX NO: 1(a)
Stimulation of DFA
DATE: 24-01-2019

AIM : To write a program for stimulation of DFA

ALGORITHM:
Step 1: Create a menu for getting input from user.
Step 2: Get the start state, final state, input symbols as input.
Step 3: Maintain a stack required for transfer.
Step 4: Finally the required output displayed has DFA valid or not.

PROGRAM :

#include<stdio.h>
#include<conio.h>

int n;

int check(char,int );
int dfa[10][10];
char c[10], string[10];
int main()
{
int states, finals;
int f[10];
int i,j,s=0,final=0;
printf("enter the number of states that your dfa consist of \n");
scanf("%d",&states);
printf("enter the number of input symbol that dfa have \n");
scanf("%d",&n);
printf("\nenter input symbols\t");
for(i=0; i<n; i++)
{
printf("\n\n %d input\t", i+1);
printf("%c",c[i]=getch());
}
printf("\n\nenter number of final states\t");
scanf("%d",&finals);

for(i=0;i<finals;i++)
{
printf("\n\nFinal state %d : q",i+1);
scanf("%d",&f[i]);
}

1
Register Number: RA1611003010165

printf("-----------------------------------------------------------------------");
printf("\n\ndefine transition rule as (initial state, input symbol ) = final state\n");
for(i=0; i<n; i++)
{
for(j=0; j<states; j++)
{
printf("\n(q%d , %c ) = q",j,c[i]);
scanf("%d",&dfa[i][j]);
}
}

do
{
i=0;
printf("\n\nEnter Input String.. ");
scanf("%s",string);

while(string[i]!='\0')
if((s=check(string[i++],s))<0)
break;
for(i=0 ;i<finals ;i++)
if(f[i] ==s )
final=1;
if(final==1)
printf("\n valid string");
else
printf("invalid string");
getch();

printf("\nDo you want to continue.? \n(y/n) ");


}
while(getch()=='y');

getch();
}

int check(char b,int d)


{
int j;
for(j=0; j<n; j++)
if(b==c[j])
return(dfa[d][j]);
return 0;
}

2
Register Number: RA1611003010165

OUTPUT

RESULT:

Thus the given program of stimulation of DFA is executed successfully.

3
Register Number: RA1611003010165

EX NO: 1(b)

DATE: 31-01-19
-NFA TO DFA CONVERSION

AIM : To write a program for construction of minimized DFA from a given regular expression
using C.

ALGORITHM:

Step 1: Get the start state, final state, input symbols as input and also give the edge value for
each state.

Step 2: Maintain a stack required for transition from one state to other state.

Step 3: Using Pop or push function perform the insertion and deletion of elements when
required.

Step 4: Finally conversion has been made to change from regular expression to minimized DFA
and the output is displayed as DFA transition table.

PROGRAM :

#include<stdio.h> int stackA[100],stackB[100],c[100],Cptr=-


#include<string.h> 1,Aptr=-1,Bptr=-1;
#define STATES 50 struct state States[10];
struct Dstate char temp[STATES+1],inp[10];
{ int nos,noi,nof,j,k,nods=-1;
char name; void pushA(int z)
char StateString[STATES+1]; {
char trans[10]; stackA[++Aptr]=z;
int is_final; }
}Dstates[50]; void pushB(int z)
struct tran {
{ stackB[++Bptr]=z;
char sym; }
int tostates[50]; int popA()
int notran; {
}; return stackA[Aptr--];
struct state }
{ void copy(int i)
int no; {
struct tran tranlist[50]; char temp[STATES+1]=" ";
}; int k=0;
Bptr=-1;

4
Register Number: RA1611003010165

strcpy(temp,Dstates[i].StateString); for(i=0;i<=Bptr;i++)
while(temp[k]!='\0') {
{ temp[i]=stackB[i]+'0';
pushB(temp[k]-'0'); }
k++; temp[i]='\0';
} }
} void display_DTran()
int popB() {
{ int i,j;
return stackB[Bptr--]; printf("\n\t\t DFA transition table");
} printf("\n\t\t ------------------------------------
int peekA() ---------- ");
{ printf("\n States \tString \tInputs\n");
return stackA[Aptr]; for(i=0;i<noi;i++)
} {
int peekB() printf("\t %c",inp[i]);
{ }
return stackA[Bptr]; printf("\n\t --------------------------------------
} ----------- ");
int seek(int arr[],int ptr,int s) for(i=0;i<nods;i++)
{ {
int i; if(Dstates[i].is_final==0)
for(i=0;i<=ptr;i++) printf("\n%c",Dstates[i].name);
{ else
printf("\n*%c",Dstates[i].name);
if(s==arr[i]) printf("\t%s",Dstates[i].StateString);
return 1; for(j=0;j<noi;j++)
} {
return 0; printf("\t%c",Dstates[i].trans[j]);
} }
void sort() }
{ printf("\n");
int i,j,temp; }
for(i=0;i<Bptr;i++) void move(int st,int j)
{ {
for(j=0;j<(Bptr-i);j++) int ctr=0;
{ while(ctr<States[st].tranlist[j].notran)
if(stackB[j]>stackB[j+1]) {
{ pushA(States[st].tranlist[j].tostates[ctr++])
temp=stackB[j]; ;
stackB[j]=stackB[j+1]; }
stackB[j+1]=temp; }
} void lambda_closure(int st)
} {
} int ctr=0,in_state=st,curst=st,chk;
} while(Aptr!=-1)
void tostring() {
{ curst=popA();
int i=0; ctr=0;
sort(); in_state=curst;

5
Register Number: RA1611003010165

while(ctr<=States[curst].tranlist[noi].notra {
n) for(j=0;j<=noi;j++)
{ {
chk=seek(stackB,Bptr,in_state); States[i].tranlist[j].sym=inp[j];
if(chk==0) k=0;
pushB(in_state); ans='y';
in_state=States[curst].tranlist[noi].tostates[ while(ans=='y')
ctr++]; {
chk=seek(stackA,Aptr,in_state); printf("move(%d,%c);",i,inp[j]);
if(chk==0 && scanf("%d",&States[i].tranlist[j].to
ctr<=States[curst].tranlist[noi].notran) states[k++]);
pushA(in_state); if((States[i].tranlist[j].tostates[k-
} 1]==-1))
} {
} k--;
int main() ans='n';
{ break;
char g1inp[100]; }
int i,final[20],start,fin=0; }
char c,ans,st[20]; States[i].tranlist[j].notran=k;
printf("\n Enter no of states in }
NFA:"); }
scanf("%d",&nos); i=0;nods=0,fin=0;
for(i=0;i<nos;i++) pushA(start);
{ lambda_closure(peekA());
States[i].no=i; tostring();
} Dstates[nods].name='A';
printf("\n Enter the start states:"); nods++;
scanf("%d",&start); strcpy(Dstates[0].StateString,temp);
printf("Enter the no of final while(i<nods)
states:"); {
scanf("%d",&nof); for(j=0;j<noi;j++)
printf("Enter the final states:\n"); {
for(i=0;i<nof;i++) fin=0;
scanf("%d",&final[i]); copy(i);
printf("\n Enter the no of input while(Bptr!=-1)
symbols:"); {
scanf("%d",&noi); move(popB(),j);
c=getchar(); }
printf("Enter the input
symbols:\n"); while(Aptr!=-1)
for(i=0;i<noi;i++) lambda_closure(peekA());
{ tostring();
scanf("%c",&inp[i]); for(k=0;k<nods;k++)
c=getchar(); {
} if((strcmp(temp,Dstates[k].StateString)==
g1inp[i]='e'; 0))
printf("\n Enter the transitions:(-1 {
to stop)\n"); Dstates[i].trans[j]=Dstates[k].name;
for(i=0;i<nos;i++) break;

6
Register Number: RA1611003010165

} }
} strcpy(Dstates[nods-1].StateString,temp);
if(k==nods) Dstates[nods-1].name='A'+nods-1;
{ Dstates[i].trans[j]=Dstates[nods-1].name;
nods++;
for(k=0;k<nof;k++) }
{ }
fin=seek(stackB,Bptr,final[k]); i++;
if(fin==1) }
{ display_DTran();
Dstates[nods-1].is_final=1; return 0;
break; }
}

OUTPUT

RESULT: The given program is successfully executed.

7
Register Number: RA1611003010165

EX NO: 2
Lexical Analysis
DATE: 07-02-2019

AIM : To implement a lexical analyzer for a simple C program.

ALGORITHM:

1. Save the program to be analyzed in a text file,


2. Create a keyword function, which simply checks if the string obtained from the text file is
a keyword or an identifier,
3. Open the file in the main function to be analyzed character by character,
4. If the character is an operator, print so,
5. If the string obtained after the white space or newline character matches the keyword,
print it’s a keyword,
6. If the string is identifier, print so.

PROGRAM :

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int isKeyword(char buffer[]){
char keywords[32][10] = {"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while"};
int i, flag = 0;
for(i = 0; i < 32; ++i){
if(strcmp(keywords[i], buffer) == 0){

8
Register Number: RA1611003010165

flag = 1;
break;
}
}
return flag;
}
int main(){
char ch, buffer[15], operators[] = "+-*/%=";
FILE *fp;
int i,j=0;
fp = fopen("program.txt","r");
if(fp == NULL){
printf("error while opening the file\n");
exit(0);
}
while((ch = fgetc(fp)) != EOF){
for(i = 0; i < 6; ++i){
if(ch == operators[i])
printf("%c is operator\n", ch);
}
if(isalnum(ch)){
buffer[j++] = ch;
}
else if((ch == ' ' || ch == '\n') && (j != 0)){
buffer[j] = '\0';
j = 0;
if(isKeyword(buffer) == 1)
printf("%s is keyword\n", buffer);
else
printf("%s is indentifier\n", buffer);
}
}

9
Register Number: RA1611003010165

fclose(fp);
return 0;
}

OUTPUT

RESULT:

The following program was successfully compiled and executed.

10
Register Number: RA1611003010165

EX NO: 3
Regular Expression to DFA
DATE: 13-02-2019 Conversion

AIM : To write a C program to convert the given RE to NFA.

ALGORITHM:

1. Accept the regular expression string from the user,


2. Using the Thompson’s Algorithm, implement operator precedence,
3. Handle the parentheses,
4. Handle the kleene stars,
5. Perform concatenation,
6. Followed by alternation.

PROGRAM :

#include<stdio.h> if(reg[i]=='a'& reg[i+1]!='/'&reg


[i+1]!='*'){
#include<string.h>
q[j][0]=j+1;
#include<conio.h>
j++;
void main(){
}
char reg[20];
if(reg[i]=='b'& reg[i+1]!='/'&
int q[20][3],i,j,len,a,b; reg[i+1]!='*'){
for(a=0;a<20;a++){ q[j][1]=j+1;
for(b=0;b<3;b++){ j++;
q[a][b]=0; }
} if(reg[i]=='e'& reg[i+1]!='/'&
} reg[i+1]!='*'){

printf("Regular expression: \n"); q[j][2]=j+1;

scanf("%s",reg); j++;

len=strlen(reg); }

i=0; if(reg[i]=='a'&reg[i+1]=='/'&reg[i+2]=='b')
{
j=1;
q[j][2]=((j+1)*10)+(j+3);
while(i<len){
j++;

11
Register Number: RA1611003010165

q[j][0]=j+1; if(reg[i]=='b'&reg[i+1]=='*'){
j++; q[j][2]=((j+1)*10)+(j+3);
q[j][2]=j+3; j++;
j++; q[j][1]=j+1;
q[j][1]=j+1; j++;
j++; q[j][2]=((j+1)*10)+(j-1);
q[j][2]=j+1; j++;
j++; }
i=i+2; if(reg[i]==')'&reg[i+1]=='*'){
} q[0][2]=((j+1)*10)+1;
if(reg[i]=='b'&reg[i+1]=='/'&reg[i+2]=='a') q[j][2]=((j+1)*10)+1;
{
j++;
q[j][2]=((j+1)*10)+(j+3);
}
j++;
i++;
q[j][1]=j+1;
}
j++;
printf("Transition function \n");
q[j][2]=j+3;
for(i=0;i<=j;i++){
j++;
if(q[i][0]!=0)
q[j][0]=j+1;
printf("\n q[%d,a]-->%d",i,q[i][0]);
j++;
if(q[i][1]!=0)
q[j][2]=j+1;
printf("\n q[%d,b]-->%d",i,q[i][1]);
j++;
if(q[i][2]!=0){
i=i+2;
if(q[i][2]<10)
}
printf("\n q[%d,e]-->%d",i,q[i][2]);
if(reg[i]=='a'&reg[i+1]=='*'){
else
q[j][2]=((j+1)*10)+(j+3);
printf("\n q[%d,e]-->%d &
j++; %d",i,q[i][2]/10,q[i][2]%10);
q[j][0]=j+1; }
j++; }
q[j][2]=((j+1)*10)+(j-1); getch();
j++;
}

12
Register Number: RA1611003010165

OUTPUT

RESULT:

The following program was successfully compiled and executed.

13
Register Number: RA1611003010165

EX NO: 4
Recursive Descent Parsing
DATE: 13-02-2019

AIM : To write a c program to convert the given Recursive Descent Parsing

ALGORITHM:

Step 1: Give the grammar

Step 2: E -> TE'

E' -> +TE' | ε

T -> FT'

T' -> *FT' | ε

F -> (E) | id

Step 3: Check the input string which accepts this grammar.

PROGRAM :

#include<stdio.h>
#include<string.h>
#include<ctype.h>
char input[10];
int i,error;
void E();
void T();
void Eprime();
void Tprime();
void F();
main()
{
i=0;
error=0;
printf("Recursive descent parsing for the following grammar\n");
printf("E->TE'\nE'->+TE'/@\nT->FT'\nT'->*FT'/@\nF->(E)/ID\n");
printf("Enter an arithmetic expression: ");
gets(input);

14
Register Number: RA1611003010165

E();
if(strlen(input)==i&&error==0)
printf("\nString is Accepted..!!!\n");
else printf("\nString is Rejected..!!!\n");
}
void E()
{
T();
Eprime();
}
void Eprime()
{
if(input[i]=='+')
{
i++;
T();
Eprime();
}
}
void T()
{
F();
Tprime();
}
void Tprime()
{
if(input[i]=='*')
{
i++;
F();
Tprime();
}
}
void F()
{
if(isalnum(input[i]))i++;
else if(input[i]=='(')
{
i++;
E();
if(input[i]==')')
i++;

else error=1;
}

else error=1;
}

OUTPUT

15
Register Number: RA1611003010165

RESULT:

The following program was successfully compiled and executed.

16
Register Number: RA1611003010165

EX NO: 5
FIRST & FOLLOW Set
DATE: 20-02-2019

AIM : To write a C program to find the FIRST and the FOLLOW of a grammar.

ALGORITHM:

1. Accept the number of productions,


2. Accept the production values,
3. Find the FIRST of the values entered,
4. Find the FOLLOW of the values entererd.

PROGRAM :

#include<stdio.h>
#include<string.h>
#include<ctype.h>
int n,m=0,p,i=0,j=0;
char a[10][10],f[10];
void follow(char c);
void first(char c);
int main(){
int i,z;
char c,ch;
printf("Enter the no of prooductions:\n");
scanf("%d",&n);
printf("Enter the productions:\n");
for(i=0;i<n;i++)
scanf("%s%c",a[i],&ch);
do{
m=0;
printf("Enter the elemets whose fisrt & follow is to be found:");
scanf("%c",&c);
first(c);
printf("First(%c)={",c);
for(i=0;i<m;i++)
printf("%c",f[i]);
printf("}\n");
strcpy(f," ");
m=0;
follow(c);

17
Register Number: RA1611003010165

printf("Follow(%c)={",c);
for(i=0;i<m;i++)
printf("%c",f[i]);
printf("}\n");
printf("Continue(0/1)?");
scanf("%d%c",&z,&ch);
}while(z==1);
return(0);
}
void first(char c){
int k;
if(!isupper(c))
f[m++]=c;
for(k=0;k<n;k++){
if(a[k][0]==c){
if(a[k][2]=='$')
follow(a[k][0]);
else if(islower(a[k][2]))
f[m++]=a[k][2];
else first(a[k][2]);
}
}
}
void follow(char c){
if(a[0][0]==c)
f[m++]='$';
for(i=0;i<n;i++){
for(j=2;j<strlen(a[i]);j++){
if(a[i][j]==c){
if(a[i][j+1]!='\0')
first(a[i][j+1]);
if(a[i][j+1]=='\0' && c!=a[i][0])
follow(a[i][0]);
}
}
}
}

OUTPUT

18
Register Number: RA1611003010165

RESULT:

The following program was successfully compiled and executed.

19
Register Number: RA1611003010165

EX NO: 6
Shift Reduce Parsing
DATE:

AIM : To write a C program to implement Shift Reduce Parser.

ALGORITHM:

1. Assume a grammar,
2. Accept the input string,
3. Print the actions.

PROGRAM :

#include<stdio.h> stk[i+1]=a[j+1];
#include<conio.h> stk[i+2]='\0';
#include<string.h> a[j]=' ';
int k=0,z=0,i=0,j=0,c=0; a[j+1]=' ';
char a[16],ac[20],stk[15],act[10];
printf("\n$%s\t%s$\t%sid",stk,a,act);
void check();
check();
int main()
}
{
else
puts("GRAMMAR is E->E+E \n E-
>E*E \n E->(E) \n E->id"); {
puts("enter input string "); stk[i]=a[j];
gets(a); stk[i+1]='\0';
c=strlen(a); a[j]=' ';
strcpy(act,"SHIFT->");
printf("\n$%s\t%s$\t%ssymbols",stk,a,act)
puts("stack \t input \t action"); ;
for(k=0,i=0; j<c; k++,i++,j++) check();
{ }
if(a[j]=='i' && a[j+1]=='d') }
{ getch();
stk[i]=a[j]; }

20
Register Number: RA1611003010165

void check() for(z=0; z<c; z++)


{ if(stk[z]=='E' && stk[z+1]=='*' &&
stk[z+2]=='E')
strcpy(ac,"REDUCE TO E");
{
for(z=0; z<c; z++)
stk[z]='E';
if(stk[z]=='i' && stk[z+1]=='d')
stk[z+1]='\0';
{
stk[z+1]='\0';
stk[z]='E';
printf("\n$%s\t%s$\t%s",stk,a,ac);
stk[z+1]='\0';
i=i-2;
printf("\n$%s\t%s$\t%s",stk,a,ac);
}
j++;
for(z=0; z<c; z++)
}
if(stk[z]=='(' && stk[z+1]=='E' &&
for(z=0; z<c; z++) stk[z+2]==')')
if(stk[z]=='E' && stk[z+1]=='+' && {
stk[z+2]=='E')
stk[z]='E';
{
stk[z+1]='\0';
stk[z]='E';
stk[z+1]='\0';
stk[z+1]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
stk[z+2]='\0';
i=i-2;
printf("\n$%s\t%s$\t%s",stk,a,ac);
}
i=i-2;
}
}
OUTPUT

21
Register Number: RA1611003010165

RESULT: The following program was successfully compiled and executed.

EX NO: 7
Leading and Trailing Sets
DATE:

AIM :

To write a C program to find the leading and trailing terminals of a given grammar.

ALGORITHM:

1. First include the necessary package,


2. Declare the array array and their functions,
3. Accept the number of productions,
4. Accept the different productions,
5. Find and print the leading terminals and the trailing terminals of the grammar.

PROGRAM :

#include<iostream> int searchter(char a){ return s[top];


#include<string.h> int count=-1,i; }
using namespace std; for(i=0;i<t;i++){ void installl(int a,int b){
int nt,t,top=0; if(T[i]==a) if(l[a][b]=='f'){
char l[a][b]='t';
s[50],NT[10],T[10],st[50] return i;
,l[10][10],tr[50][50];
} push(T[b]);
int searchnt(char a){
return count;
int count=-1,i; push(NT[a]);
}
for(i=0;i<nt;i++){ }
void push(char a){
}
if(NT[i]==a) s[top]=a;
void installt(int a,int b){
top++;
return i; if(tr[a][b]=='f'){
}
} tr[a][b]='t';
char pop(){
return count; top--; push(T[b]);
}

22
Register Number: RA1611003010165

if(searchnt(pr[j][k]
push(NT[a]); )==-1){
T[t++]=pr[i][j];
}
}
} installl(searchnt(pr
} [j][0]),searchter(pr[j][k]));
}
int main(){
for(i=0;i<nt;i++){
int i,s,k,j,n; break;
char for(j=0;j<t;j++)
pr[30][30],b,c; }
cout<<"Enter the l[i][j]='f';
no of productions:"; }
}
cin>>n;
for(i=0;i<nt;i++){ }
cout<<"Enter the
productions one by }
one\n"; for(j=0;j<t;j++)
}
for(i=0;i<n;i++)
tr[i][j]='f'; }
cin>>pr[i];
} while(top!=0){
nt=0;
for(i=0;i<nt;i++){ b=pop();
t=0;
c=pop();
for(i=0;i<n;i++){ for(j=0;j<n;j++){
for(s=0;s<n;s++){
if((searchnt(pr[i][0 if(NT[(searchnt(pr
]))==-1) [j][0]))]==NT[i]){ if(pr[s][3]==b)

NT[nt++]=pr[i][0] if(searchter(pr[j][3
; installl(searchnt(pr
])!=-1) [s][0]),searchter(c));
}
}
for(i=0;i<n;i++){
installl(searchnt(pr }
[j][0]),searchter(pr[j][3]));
for(j=3;j<strlen(pr for(i=0;i<nt;i++){
[i]);j++){
else{
cout<<"Leading["
if(searchnt(pr[i][j] <<NT[i]<<"]"<<"\t{";
)==-1){
for(k=3;k<strlen(p
r[j]);k++){ for(j=0;j<t;j++){
if(searchter(pr[i][j
])==-1) if(l[i][j]=='t')

23
Register Number: RA1611003010165

cout<<T[j]<<","; for(s=0;s<n;s++){
for(k=(strlen(pr[j])
} -1);k>=3;k--){
if(pr[s][3]==b)
cout<<"}\n";
if(searchnt(pr[j][k] installt(searchnt(pr
} [s][0]),searchter(c));
)==-1){
}
}
top=0; installt(searchnt(pr
[j][0]),searchter(pr[j][k])); for(i=0;i<nt;i++){
for(i=0;i<nt;i++){
cout<<"Trailing["
for(j=0;j<n;j++){ break; <<NT[i]<<"]"<<"\t{";

if(NT[searchnt(pr[ } for(j=0;j<t;j++){
j][0])]==NT[i]){
} if(tr[i][j]=='t')
if(searchter(pr[j][s
trlen(pr[j])-1])!=-1) cout<<T[j]<<",";
}
} }
installt(searchnt(pr }
[j][0]),searchter(pr[j][strle cout<<"}\n";
n(pr[j])-1])); }
}
while(top!=0){
else{ }
b=pop();
c=pop();

OUTPUT

24
Register Number: RA1611003010165

RESULT: The following program was successfully compiled and executed.

EX NO: 8(a)
Left Recursion
DATE:

AIM :

To a write a C program to eliminate left recursion from a grammar.

ALGORITHM:

1. Accept a grammar as input production,


2. For each non-terminal Ai,
3. Repeat until and iteration leaves the grammar unchanged.
4. For each rule, Ai ->αi, αi being a sequence of terminals and non-terminals,
5. If αi begins with a non-terminal Aj and j<1,
6. Let βi be αi without its leading Aj,
7. Remove the rule Ai-> αi,
8. For each rule Ai-> αj, Add the rule Ai-> αiβi,
9. Remove direct left recursion for Ai.

PROGRAM :

#include<stdio.h>
#include<string.h>
#define SIZE 10
int main () {
char non_terminal;
char beta,alpha;
int num;
int i;
char production[10][SIZE];
int index=3; /* starting of the string following "->" */
printf("Enter Number of Production : ");
scanf("%d",&num);
printf("Enter the grammar as E->E-A :\n");
for(i=0;i<num;i++){
scanf("%s",production[i]);
}
for(i=0;i<num;i++){
printf("\nGRAMMAR : : : %s",production[i]);
non_terminal=production[i][0];

25
Register Number: RA1611003010165

if(non_terminal==production[i][index]) {
alpha=production[i][index+1];
printf(" is left recursive.\n");
while(production[i][index]!=0 && production[i][index]!='|') {
index++; }
if(production[i][index]!=0) {
beta=production[i][index+1];
printf("Grammar without left recursion:\n");
printf("%c->%c%c\'",non_terminal,beta,non_terminal);
printf("\n%c\'->%c%c%c\'|e\n",non_terminal,alpha,beta,non_terminal);
}
else
printf(" can't be reduced\n");
}
else
printf(" is not left recursive.\n");
index=3;
}
return 0;
}

OUTPUT

RESULT:

The following program was successfully compiled and executed.

26
Register Number: RA1611003010165

EX NO: 8(b)
Left Factoring
DATE:

AIM :

To write a C program to perform LEFT FACTORING on a given grammar.

ALGORITHM:

1. Accept any production from the user,


2. Use another character array to store one part of the grammar and another character to
store the remaining.
3. Later compare the derived character with each other and copy the matching character to another

array,

4. Print the result

PROGRAM :

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char a[10],a1[10],a2[10],a3[10],a4[10],a5[10];
int i,j=0,k,l;
printf("enter any productions A->");
gets(a);
for(i=0;a[i]!='|';i++,j++)
a1[j]=a[i];
a1[j]='\0';
for(j=++i,i=0;a[j]!='\0';j++,i++)
a2[i]=a[j];
a2[i]='\0';
k=0;
l=0;
for(i=0;i<strlen(a1)||i<strlen(a2);i++)
{
if(a1[i]==a2[i])
{

27
Register Number: RA1611003010165

a3[k]=a1[i];
k++;
}
else
{
a4[l]=a1[i];
a5[l]=a2[i];
l++;
}}
a3[k]='A';
a3[++k]='\0';
a4[l]='|';
a5[l]='\0';
a4[++l]='\0';
strcat(a4,a5);
printf("\n A->%s'",a3);
printf("\n A'->e|%s",a4);
return 0;
}

OUTPUT

RESULT:

28
Register Number: RA1611003010165

The following program was successfully compiled and executed.

EX NO: 9
Predictive Parsing Table
DATE:

AIM :

To write a C program to construct a Predictive Parser for a grammar.

ALGORITHM:

1. Accept the number of coordinates,


2. Accept the productions,
3. Print the FIRST of the grammar,
4. Print the FOLLLOW of the grammar,
5. Finally print if the string is accepted or not.

PROGRAM :

#include<stdio.h> j=3; {
#include<conio.h> l=0; for(m=0;m<l;m++)
#include<string.h> a=0; {
int main() l1:if(!((st[i][j]>64)&&(st[ if(ft[i][m]==ft[a][b])
{ i][j]<91))) goto s2;
char { }
fin[10][20],st[10][20],ft[2 for(m=0;m<l;m++) ft[i][l]=ft[a][b];
0][20],fol[20][20]; { l=l+1;
int if(ft[i][m]==st[i][j]) s2:b=b+1;
a=0,e,i,t,b,c,n,k,l=0,j,s,m, goto s1; }
p; } }
printf("enter the no. of ft[i][l]=st[i][j]; }
coordinates\n"); l=l+1; while(st[i][j]!='\0')
scanf("%d",&n); s1:j=j+1; {
printf("enter the } if(st[i][j]=='|')
productions in a else {
grammar\n"); { j=j+1;
for(i=0;i<n;i++) if(s>0) goto l1;
scanf("%s",st[i]); { }
for(i=0;i<n;i++) while(st[i][j]!=st[a][0]) j=j+1;
fol[i][0]='\0'; { }
for(s=0;s<n;s++) a++; ft[i][l]='\0';
{ } }
for(i=0;i<n;i++) b=0; }
{ while(ft[a][b]!='\0') printf("first pos\n");

29
Register Number: RA1611003010165

for(i=0;i<n;i++) if(ft[a][p]!='@') printf("\n");


printf("FIRS[%c]=%s\n", { s=0;
st[i][0],ft[i]); for(m=0;m<l;m++) for(i=0;i<n;i++)
fol[0][0]='$'; { {
for(i=0;i<n;i++) if(fol[i][m]==ft[a][p]) j=3;
{ goto q2; while(st[i][j]!='\0')
k=0; } {
j=3; fol[i][l]=ft[a][p]; if((st[i][j-1]=='|')||(j==3))
if(i==0) l=l+1; {
l=1; } for(p=0;p<=2;p++)
else else {
l=0; e=1; fin[s][p]=st[i][p];
k1:while((st[i][0]!=st[k][j q2:p++; }
])&&(k<n)) } t=j;
{ if(e==1) for(p=3;((st[i][j]!='|')&&(
if(st[k][j]=='\0') { st[i][j]!='\0'));p++)
{ e=0; {
k++; goto a1; fin[s][p]=st[i][j];
j=2; } j++;
} } }
j++; } fin[s][p]='\0';
} else if(st[i][k]=='@')
j=j+1; { {
if(st[i][0]==st[k][j-1]) a1:c=0; b=0;
{ a=0; a=0;
if((st[k][j]!='|')&&(st[k][j] while(st[k][0]!=st[a][0]) while(st[a][0]!=st[i][0])
!='\0')) { {
{ a++; a++;
a=0; } }
if(!((st[k][j]>64)&&(st[k] while((fol[a][c]!='\0')&&( while(fol[a][b]!='\0')
[j]<91))) st[a][0]!=st[i][0])) {
{ { printf("M[%c,%c]=%s\n",
for(m=0;m<l;m++) for(m=0;m<l;m++) st[i][0],fol[a][b],fin[s]);
{ { b++;
if(fol[i][m]==st[k][j]) if(fol[i][m]==fol[a][c]) }
goto q3; goto q1; }
} } else
fol[i][l]=st[k][j]; fol[i][l]=fol[a][c]; if(!((st[i][t]>64)&&(st[i][t
l++; l++; ]<91)))
q3:; q1:c++; printf("M[%c,%c]=%s\n",
} } st[i][0],st[i][t],fin[s]);
else } else
{ goto k1; {
while(st[k][j]!=st[a][0]) } b=0;
{ fol[i][l]='\0'; a=0;
a++; } while(st[a][0]!=st[i][3])
} printf("follow pos\n"); {
p=0; for(i=0;i<n;i++) a++;
while(ft[a][p]!='\0') printf("FOLLOW[%c]=% }
{ s\n",st[i][0],fol[i]); while(ft[a][b]!='\0')

30
Register Number: RA1611003010165

{ } }
printf("M[%c,%c]=%s\n", s++; }
st[i][0],ft[a][b],fin[s]); } return 0;
b++; if(st[i][j]=='|') }
} j++;

OUTPUT

RESULT:

The following program was successfully compiled and executed.

31
Register Number: RA1611003010165

EX NO: 10
LR(0) Item Set
DATE:

AIM :

To write a C program for LR(0) parser for the given productions.

ALGORITHM:

1. Assume a grammar,
2. Accept the input string,
3. Print the actions.

PROGRAM :

#include<string.h> },{-1,- int gotot[12][3]={1,2,3,-


1},{101,6},{101,6}}, 1,-1,-1,-1,-1,-1,-1,-1,-
#include<conio.h> 1,8,2,3,-1,-1,-1,-1,
#include<stdio.h> {{100,5},{-1,-1},{-1,- 9,3,-1,-
int axn[][6][2]={ 1},{-1,-1},{-1,-1},{-1,- 1,10,-1,-1,-1,-1,-1,-1,-1,-
1}}, 1,-1,-1,-1,-1};

{{100,5},{-1,-1},{-1,- int a[10];


1},{100,4},{-1,-1},{-1,- {{100,5},{-1,-1},{-1,-
1},{100,4},{-1,-1},{-1,- char b[10];
1}},
1}}, int top=-1,btop=-1,i;
{{-1,-
1},{100,6},{-1,-1},{-1,- {{-1,- void push(int k)
1},{-1,-1},{102,102}}, 1},{100,6},{-1,-1},{-1,-
1},{100,11},{-1,-1}}, {
{{-1,-
1},{101,2},{100,7},{-1,- {{-1,- if(top<9)
1},{101,2},{101,2}}, 1},{101,1},{100,7},{-1,- a[++top]=k;
1},{101,1},{101,1}},
{{-1,- }
1},{101,4},{101,4},{-1,- {{-1,-
1},{101,4},{101,4}}, 1},{101,3},{101,3},{-1,- void pushb(char k)
1},{101,3},{101,3}},
{
{{100,5},{-1,-1},{-1,- {{-1,-
if(btop<9)
1},{100,4},{-1,-1},{-1,- 1},{101,5},{101,5},{-1,-
1}}, 1},{101,5},{101,5}} b[++btop]=k;
}; }
{{100,5},{101,6},{101,6

32
Register Number: RA1611003010165

char TOS()
printf("\n"); break;
{
} case
return a[top]; 5:dest="(E)";
}
void error() src='F';
void pop()
{
{
printf("\n\nSyntax break;
if(top>=0) Error"); case
top--; } 6:dest="i";
} void reduce(int p)
src='F';
{
void popb() int len,k,ad; break;
{ char src,*dest;
if(btop>=0) switch(p) default:dest="\0";
b[btop--]='\0'; {
src='\0';
} case
1:dest="E+T";
break;
void display()
src='E'; }
{

break; for(k=0;k<strlen(dest);k+
for(i=0;i<=top;i++) +)
case
2:dest="T"; {
printf("%d%c",a[i],b[i]);
pop();
} src='E'; popb();
}
void display1(char p[],int break;
pushb(src);
m) case
3:dest="T*F"; switch(src)
{
{
int l;
src='T'; case 'E':
printf("\t\t");
ad=0;
break;
for(l=m;p[l]!='\0';l++)
case break;
4:dest="F"; case 'T':
printf("%c",p[l]);
ad=1;
src='T';

33
Register Number: RA1611003010165

break; ic=0; push(axn[st][ic][1]);


case 'F': else
ad=2; if(an=='+') display();

break; ic=1; j++;


default: else
ad=-1; if(an=='*') display1(ip,j);
}
break; ic=2;
} else if(axn[st][ic][0]==101)
if(an=='(')
{
push(gotot[TOS()][ad]);
ic=3;
} reduce(axn[st][ic][1]);
else
int main() if(an==')')
display();
{
int j,st,ic; ic=4;
display1(ip,j);
char else
if(an=='$') }
ip[20]="\0",an;
clrscr(); if(axn[st][ic][1]==102)
ic=5;
printf("Enter any {
String :- "); else

gets(ip); {
printf("Given String is
push(0); Accepted");
error();
display();
break;
printf("\t%s\n",ip); break;
}
for(j=0;ip[j]!='\0';) }
}
{
if(axn[st][ic][0]==100) getch();
st=TOS();
{ return 0;
an=ip[j];
}

if(an>='a'&an<='z') pushb(an);

34
Register Number: RA1611003010165

OUTPUT

RESULT:

The following program was successfully compiled and executed.

35
Register Number: RA1611003010165

EX NO: 11
Intermediate code generation –
DATE: Three Address Codes

AIM :
To write a program for the generation of assembly language code of relational
operator.

ALGORITHM:

1. The three address code using the relational operator is get from the user.
2. Identifying the addressing mode of the given three address code.
3. Identify the relational operator used in the statement.
4. Generate and display the assembly language code.

PROGRAM :

#include<stdio.h> while(exp[i]!='=')
#include<string.h> {
void pm(); i++;
void plus(); }
void div(); strncat(exp2,exp,i);
int i,ch,j,l,addr=100; strrev(exp);
char ex[10], exp[10] exp1[0]='\0';
,exp1[10],exp2[10],id1[5],op[5],id2[5]; strncat(exp1,exp,l-(i+1));
int main() strrev(exp1);
{ printf("Three address
while(1) code:\ntemp=%s\n%s=temp\n",exp1,exp2)
{ ;
printf("\n1.assignment\n2.arithmetic\n3.rel break;
ational\n4.Exit\nEnter the choice:");
scanf("%d",&ch); case 2:
switch(ch) printf("\nEnter the expression with
{ arithmetic operator:");
case 1: scanf("%s",ex);
printf("\nEnter the expression with strcpy(exp,ex);
assignment operator:"); l=strlen(exp);
scanf("%s",exp); exp1[0]='\0';
l=strlen(exp);
exp2[0]='\0'; for(i=0;i<l;i++)
i=0; {

36
Register Number: RA1611003010165

if(exp[i]=='+'||exp[i]=='-') printf("\n%d\t T:=0",addr);


{ addr++;
if(exp[i+2]=='/'||exp[i+2]=='*') printf("\n%d\t goto %d",addr,addr+2);
{ addr++;
pm(); printf("\n%d\t T:=1",addr);
break; }
} break;
else }
{ }
plus(); return 0;
break; }
} void pm()
} {
else if(exp[i]=='/'||exp[i]=='*') strrev(exp);
{ j=l-i-1;
div(); strncat(exp1,exp,j);
break; strrev(exp1);
} printf("Three address
} code:\ntemp=%s\ntemp1=%c%ctemp\n",e
break; xp1,exp[j+1],exp[j]);
}
case 3: void div()
printf("Enter the expression with relational {
operator"); strncat(exp1,exp,i+2);
scanf("%s%s%s",&id1,&op,&id2); printf("Three address
if(((strcmp(op,"<")==0)||(strcmp(op,">")= code:\ntemp=%s\ntemp1=temp%c%c\n",e
=0)||(strcmp(op,"<=")==0)||(strcmp(op,">= xp1,exp[i+2],exp[i+3]);
")==0)||(strcmp(op,"==")==0)||(strcmp(op, }
"!=")==0))==0) void plus()
printf("Expression is error"); {
else strncat(exp1,exp,i+2);
{ printf("Three address
printf("\n%d\tif %s%s%s goto code:\ntemp=%s\ntemp1=temp%c%c\n",e
%d",addr,id1,op,id2,addr+3); xp1,exp[i+2],exp[i+3]);
addr++; }

OUTPUT

RESULT: Thus the program for generation of Machine Code for the given Intermediate code is
executed and verified.

37
Register Number: RA1611003010165

EX NO: 12
Intermediate code generation –
DATE: Postfix, Prefix

AIM :
To write a program for the Intermediate code generation – Postfix, Prefix.

ALGORITHM:

1. Given the input as a Infix expression.

2. Convert input expression into prefix or postfix.

3. Perform the intermediate code generation operation .

PROGRAM :
Infix to postfix

#define SIZE 400 }


#include<string.h> int main()
#include<iostream> {
#include<conio.h>
#include<ctype.h> char infx[400], pofx[400], ch;
using namespace std; int i = 0, k = 0;
char s[SIZE]; cout << "\n\nEnter the Infix Expression
int top = -1; ?";
void push(char elem) { cin >> infx;
s[++top] = elem; push('#');
} while ((ch = infx[i++]) != '\0') {
char pop() { if (ch == '(')
return(s[top--]); push(ch);
} else if (isalnum(ch))
int pr(char elem) { pofx[k++] = ch;
switch (elem) { else if (ch == ')') {
case '#': while (s[top] != '(')
return 0; pofx[k++] = pop();
case '(': top--;
return 1;
case '+': } else {
case '-': while (pr(s[top]) >= pr(ch))
return 2; pofx[k++] = pop();
case '*': push(ch);
case '/': }
return 3; }
} while (s[top] != '#')
return 0; pofx[k++] = pop();
38
Register Number: RA1611003010165

pofx[k] = '\0'; getch();


cout<< "Postfix Expression is:"<< return 0;
pofx<<endl; }

#define SIZE 50 push('#');


#include<string.h> strrev(infx);
#include <ctype.h> while( (ch=infx[i++]) != '\0')
#include <stdio.h> {
#include <conio.h> if( ch == ')') push(ch);
char s[SIZE]; else if(isalnum(ch)) prfx[k++]=ch;
int top=-1; else if( ch == '(')
int s1[SIZE]; {
int top1=-1; while( s[top] != ')')
void push1(int elem) prfx[k++]=pop();
{ top--;
s1[++top1]=elem; }
} else
int pop2() {
{ while( pr(s[top]) >= pr(ch) )
return(s1[top1--]); prfx[k++]=pop();
} push(ch);
void push(char elem) }
{ }
s[++top]=elem; while( s[top] != '#')
} prfx[k++]=pop();
char pop() prfx[k]='\0';
{ strrev(prfx);
return(s[top--]); strrev(infx);
} printf("\n\nPrefix Expression:
int pr(char elem) %s\n",prfx);
{ }
switch(elem) void caddress(char *prfx)
{ {
case '#': int i,j=0, k=0,l=0;
return 0; char op1,op2;
case ')': for(i=strlen(prfx)-1; i>=0; --i)
return 1; if(prfx[i]!='+' && prfx[i]!='-'&&
case '+': prfx[i]!='*' && prfx[i]!='/' &&
case '-': prfx[i]!='=')
return 2; push(prfx[i]);
case '*': else
case '/': {
return 3; k=0;
} l=0;
return 0;
} ++j;
void prefix(char *infx,char *prfx) { op1=pop();
int i=0,k=0; op2=pop();
char ch,elem;

39
Register Number: RA1611003010165

if(op1=='$')
k=pop2(); push('$');
if(op2=='$') push1(j);
l=pop2(); }
if(k==0 && l==0) }
printf("t%d=%c%c%c int main()
\n",j,op1,prfx[i],op2); {
if(k!=0 && l==0) char infx[50],prfx[50];
printf("t%d=t%d%c%c printf("\n\nEnter the Infix Expression ?
\n",j,k,prfx[i],op2); ");
if(l!=0 && k==0) scanf("%s",infx);
printf("t%d=%c%ct%d prefix(infx,prfx);
\n",j,op1,prfx[i],l); printf("\n3-address code is:\n");
if(l!=0 && k!=0) caddress(prfx);
printf("t%d=t%d%ct%d \n return 0;
",j,k,prfx[i],l); }

OUTPUT

RESULT:

Thus the given program is executed and verified.

40
Register Number: RA1611003010165

EX NO: 13
Construction of DAG
DATE:

AIM :

To write a C program to construct of DAG(Directed Acyclic Graph)

ALGORITHM:

1. Start the program


2. Include all the header files
3. Check for postfix expression and construct the in order DAG representation
4. Print the output
5. Stop the program

PROGRAM :

#include<iostream>
#include<stdlib.h>

// The maximum number of the vertex for the sample random graph.
#define NOV 20

using namespace std;

// A function to check for the cycle, on addition of a new edge in the random graph.
bool CheckAcyclic(int edge[][2], int ed, bool check[], int v)
{
int i;
bool value;
// If the current vertex is visited already, then the graph contains cycle.
if(check[v] == true)
{
return false;
}
else
{
check[v] = true;
// For each vertex, go for all the vertex connected to it.
for(i = ed; i >= 0; i--)
{
if(edge[i][0] == v)
{
return CheckAcyclic(edge, ed, check, edge[i][1]);

41
Register Number: RA1611003010165

}
}
}
// In case, if the path ends then reassign the vertexes visited in that path to false again.
check[v] = false;

if(i == 0)
return true;
}

// A function to generate random graph.


void GenerateRandGraphs(int e)
{
int i, j, edge[e][2], count;

bool check[21];

// Build a connection between two random vertex.


i = 0;
while(i < e)
{
edge[i][0] = rand()%NOV+1;
edge[i][1] = rand()%NOV+1;

for(j = 1; j <= 20; j++)check[j] = false;

if(CheckAcyclic(edge, i, check, edge[i][0]) == true)


i++;
}

// Print the random graph.


cout<<"\nThe generated random random graph is: ";
for(i = 0; i < NOV; i++)
{
count = 0;
cout<<"\n\t"<<i+1<<"-> { ";
for(j = 0; j < e; j++)
{
if(edge[j][0] == i+1)
{
cout<<edge[j][1]<<" ";
count++;
}
else if(edge[j][1] == i+1)
{
count++;
}
else if(j == e-1 && count == 0)
cout<<"Isolated Vertex!";
}

42
Register Number: RA1611003010165

cout<<" }";
}
}

int main()
{
int e;

cout<<"Enter the number of edges for the random graphs: ";


cin>>e;

// A function to generate a random undirected graph with e edges.


GenerateRandGraphs(e);
}

OUTPUT

RESULT:
Thus the program for Construction of DAG is executed and verified.

43

You might also like