1 | P a g e
2 | P a g e
PRACTICAL -1
AIM:-To Write a C program to develop a lexical analyzer to recognize a
few patterns in C.
OUTPUT:-
3 | P a g e
PRACTICAL -1
AIM:-To Write a C program to develop a lexical analyzer to recognize a
few patterns in C.
CODING:-
#include <stdio.h>
#include <conio.h>
#include <string.h>
char symTable[5][7] = { "int", "void", "float", "char", "string" };
void main() {
int i, j, k = 0;
char string[7];
char str[] = "int main(){printf("Hello");return 0;}";
char *ptr;
clrscr();
printf("Splitting string "%s" into tokens:n", str);
ptr = strtok(str, " (){};""");
printf("nn");
while (ptr != NULL) {
printf ("%sn", ptr);
for (i = k; i< 5; i++) {
memset(&string[0], 0, sizeof(string));
for (j = 0; j < 7; j++) {
string[j] = symTable[i][j];
}
if (strcmp(ptr, string) == 0) {
printf("Keywordnn");
break;
} else
if (string[j] == 0 || string[j] == 1 || string[j] == 2 ||
string[j] == 3 || string[j] == 4 || string[j] == 5 ||
string[j] == 6 || string[j] == 7 || string[j] == 8 ||
string[j] == 9) {
printf("Constantnn");
break;
} else {
printf("Identifiernn");
break;
}
}
ptr = strtok(NULL, " (){};""");
k++;
4 | P a g e
}
getch();
}
5 | P a g e
6 | P a g e
PRACTICAL -2
AIM:-To write a program to implement the Lexical Analyzer using lex tool.
OUTPUT:-
7 | P a g e
PRACTICAL -2
AIM:-To write a program to implement the Lexical Analyzer using lex tool.
CODING:-
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<string.h>
char vars[100][100];
int vcnt;
char input[1000],c;
char token[50],tlen;
int state=0,pos=0,i=0,id;
char *getAddress(char str[])
{
for(i=0;i<vcnt;i++)
if(strcmp(str,vars[i])==0)
return vars[i];
strcpy(vars[vcnt],str);
return vars[vcnt++];
}
int isrelop(char c)
{
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='%'||c=='^')
return 1;
else
return 0;
}
int main(void)
{
clrscr();
printf("Enter the Input String:");
gets(input);
do
{
c=input[pos];
putchar(c);
switch(state)
{
case 0:
if(isspace(c))
printf("b");
if(isalpha(c))
{
token[0]=c;
tlen=1;
state=1;
}
8 | P a g e
if(isdigit(c))
state=2;
if(isrelop(c))
state=3;
if(c==';')
printf("t<3,3>n");
if(c=='=')
printf("t<4,4>n");
break;
case 1:
if(!isalnum(c))
{
token[tlen]='o';
printf("bt<1,%p>n",getAddress(token));
state=0;
pos--;
}
else
token[tlen++]=c;
break;
case 2:
if(!isdigit(c))
{
printf("bt<2,%p>n",&input[pos]);
state=0;
pos--;
}
break;
case 3:
id=input[pos-1];
if(c=='=')
printf("t<%d,%d>n",id*10,id*10);
else{
printf("bt<%d,%d>n",id,id);
pos--;
}state=0;
break;
}
pos++;
}
while(c!=0);
getch();
return 0;
}
9 | P a g e
10 | P a g e
PRACTICAL -3
AIM:-Design Predictive Parserof the RECURSIVE DESCENTPARSER
OUTPUT:-
11 | P a g e
PRACTICAL -3
AIM:-Design Predictive Parserof the RECURSIVE DESCENTPARSER
LANGUAGE:- E-> TE’
E’-> +TE’ | -TE’ | null
T-> FT’
T’-> *FT’| /FT’ | null
F-> id/ (E)/ num
CODING:-
#include "stdio.h"
#include "conio.h"
char input[100];
char prod[100][100];
int pos=-1,l,st=-1;
char id,num;
void E();
void T();
void F();
void advance();
void Td();
void Ed();
void advance()
{
pos++;
if(pos<l)
{
if(input[pos]>='0'&& input[pos]<='9')
{
num=input[pos];
id='0';
}
if((input[pos]>='a' || input[pos]>='A')&&(input[pos]<='z' || input[pos]<='Z'))
{id=input[pos];
num='0';
}
}
}
void E()
{
strcpy(prod[++st],"E->TE'");
T();
Ed();
}
void Ed()
{
int p=1;
12 | P a g e
if(input[pos]=='+')
{
p=0;
strcpy(prod[++st],"E'->+TE'");
advance();
T();
Ed();
}
if(input[pos]=='-')
{ p=0;
strcpy(prod[++st],"E'->-TE'");
advance();
T();
Ed();
}
// Recursive Descent Parser
if(p==1)
{
strcpy(prod[++st],"E'->null");
}
}
void T()
{
strcpy(prod[++st],"T->FT'");
F();
Td();
}
void Td()
{
int p=1;
if(input[pos]=='*')
{
p=0;
strcpy(prod[++st],"T'->*FT'");
advance();
F();
Td();
}
if(input[pos]=='/')
{ p=0;
strcpy(prod[++st],"T'->/FT'");
advance();
F();
Td();
}
if(p==1)
strcpy(prod[++st],"T'->null");
}
13 | P a g e
void F()
{
if(input[pos]==id) {
strcpy(prod[++st],"F->id");
advance(); }
if(input[pos]=='(')
{
strcpy(prod[++st],"F->(E)");
advance();
E();
if(input[pos]==')') {
//strcpy(prod[++st],"F->(E)");
advance(); }
}
if(input[pos]==num)
{
strcpy(prod[++st],"F->num");
advance();
}
}
int main()
{
int i;
printf("Enter Input String ");
scanf("%s",input);
l=strlen(input);
input[l]='$';
advance();
E();
if(pos==l)
{
printf("String Acceptedn");
for(i=0;i<=st;i++)
{
printf("%sn",prod[i]);
}
}
else
{
printf("String rejectedn");
}
getch();
return 0;
}
14 | P a g e
PRACTICAL -4
AIM:-To write a C program to implement a symbol table.
OUTPUT:-
15 | P a g e
PRACTICAL -4
AIM:-To write a C program to implement a symbol table.
CODING:-
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
void main()
{
int i=0,j=0,x=0,n;
void *p,*add[5];
char ch,srch,b[15],d[15],c;
printf("Expression terminated by $:");
while((c=getchar())!='$')
{
b[i]=c;
i++;
}
n=i-1;
printf("Given Expression:");
i=0;
while(i<=n)
{
printf("%c",b[i]);
i++;
}
printf("n Symbol Tablen");
printf("Symbol t addr t type");
while(j<=n)
{
c=b[j];
if(isalpha(toascii(c)))
{
p=malloc(c);
add[x]=p;
d[x]=c;
printf("n%c t %d t identifiern",c,p);
x++;
j++;
}
else
{
ch=c;
if(ch=='+'||ch=='-'||ch=='*'||ch=='=')
16 | P a g e
{
p=malloc(ch);
add[x]=p;
d[x]=ch;
printf("n %c t %d t operatorn",ch,p);
x++;
j++;
}}}}
17 | P a g e
18 | P a g e
PRACTICAL -5
AIM:-To design LALR bottom up parserfor the given language.
OUTPUT:-
19 | P a g e
PRACTICAL -5
AIM:-To design LALR bottom up parserfor the given language.
CODING:-
<parser.l>
%{
#include<stdio.h>
#include "y.tab.h"
%}
%%
[0-9]+ {yylval.dval=atof(yytext);
return DIGIT;
}
n|. return yytext[0];
%%
<parser.y>
%{
/*This YACC specification file generates the LALR parser for the program
considered in experiment 4.*/
#include<stdio.h>
%}
%union
{
double dval;
}
%token <dval> DIGIT
%type <dval> expr
%type <dval> term
%type <dval> factor
%%
line: expr 'n' {
printf("%gn",$1);
}
;
expr: expr '+' term {$$=$1 + $3 ;}
| term
;
term: term '*' factor {$$=$1 * $3 ;}
| factor
;
factor: '(' expr ')' {$$=$2 ;}
| DIGIT
;
%%
int main()
{
20 | P a g e
yyparse();
}
yyerror(char *s)
{
printf("%s",s);
}
21 | P a g e
22 | P a g e
PRACTICAL -6
AIM:-To write a C program to find computation of First.
OUTPUT:-
23 | P a g e
PRACTICAL -6
AIM:-To write a C program to find computation of First.
CODING:-
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.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;
//clrscr();
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," ");
//flushall();
m=0;
follow(c);
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);
}
24 | P a g e
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]);
}
}
}
25 | P a g e
26 | P a g e
PRACTICAL -7
AIM:-To find whether a given string is Keyword or not.
OUTPUT:-
27 | P a g e
PRACTICAL -7
AIM:-To find whether a given string is Keyword or not.
CODING:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char keyword[32][10]={"auto","double","int","struct","break","else","long",
"switch","case","enum","register","typedef","char",
"extern","return","union","const","float","short",
"unsigned","continue","for","signed","void","default",
"goto","sizeof","voltile","do","if","static","while"} ;
char string[10];
int flag=0,i;
printf("ENTER ANY STRING:");
gets(string);
for(i=0;i<32;i++)
{
if(strcmp(string,keyword[i])==0)
{
flag=1;
}
}
if(flag==1)
printf("%s is a Keywordn",string);
else
printf("%s is not a Keywordn",string);
getch();
}
28 | P a g e
PRACTICAL -8
AIM:-To find whether a given string is Identifier or not.
OUTPUT:-
29 | P a g e
PRACTICAL -8
AIM:-To find whether a given string is Identifier or not.
CODING:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[100],str[20],str1[10]={"printf"};
int i,l,flag=0,s;
clrscr();
printf("nenter the string:");
gets(a);
if(strcmp(a,str1)==0)
{
printf("nnot allow");
}
else
{
l=strlen(a);
if(a[0]==' '||a[0]=='@')
{
printf("ninvalid identifier");
goto p;
}
else
{
s=0;
while(s!=l)
{
if(a[s]==' '||a[s]=='1'||a[s]=='2'||a[s]=='3'|| a[s]=='4'||a[s]=='5' ||a[s]=='6'
||a[s]=='7' ||a[s]=='8'||a[s]=='9'||a[s]=='0')
{
printf("ninvalid identifier");
goto p;
}
else
{
flag=0;
}
s++;
30 | P a g e
}
if(flag==1)
{
printf("ninvalid identifier");
}
if(flag==0)
{
printf("nvalid identifier");
}
}
}
p:
getch();
}
31 | P a g e
32 | P a g e
PRACTICAL -9
AIM:-To write a C program to implement data flow & control flow
analysis
OUTPUT:-
33 | P a g e
PRACTICAL -9
AIM:-To write a C program to implement data flow & control flow
analysis
CODING:-
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void input();
void output();
void change(int p,intq,char *res);
void constant();
void expression();
struct expr
{
char op[2],op1[5],op2[5],res[5];
int flag;
}arr[10];
int n;
int main()
{
int ch=0;
input();
constant();
expression();
34 | P a g e
output();
}
void input()
{
int i;
printf("nnEnter the maximum number of expressions:");
scanf("%d",&n);
printf("nEnter the input : n");
for(i=0;i<n;i++)
{
scanf("%s",arr[i].op);
scanf("%s",arr[i].op1);
scanf("%s",arr[i].op2);
scanf("%s",arr[i].res);
arr[i].flag=0;
}
}
void constant()
{
int i;
int op1,op2,res;
char op,res1[5];
for(i=0;i<n;i++)
{
if(isdigit(arr[i].op1[0]) &&isdigit(arr[i].op2[0]))
35 | P a g e
{
op1=atoi(arr[i].op1);
op2=atoi(arr[i].op2);
op=arr[i].op[0];
switch(op)
{
case '+':
res=op1+op2;
break;
case '-':
res=op1-op2;
break;
case '*':
res=op1*op2;
break;
case '/':
res=op1/op2;
break;
}
sprintf(res1,"%d",res);
arr[i].flag=1;
change(i,i,res1);
}
}
}
36 | P a g e
void expression()
{
int i,j;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(arr[i].op,arr[j].op)==0)
{
if(strcmp(arr[i].op,"+")==0||strcmp(arr[i].op,"*")==0)
{
if(strcmp(arr[i].op1,arr[j].op1)==0&&strcmp(arr[i].op2,arr[j].op2)==0 ||
strcmp(arr[i].op1,arr[j].op2)==0&&strcmp(arr[i].op2,arr[j].op1)==0)
{
arr[j].flag=1;
change(i,j,NULL);
}
}
else
{
if(strcmp(arr[i].op1,arr[j].op1)==0&&strcmp(arr[i].op2,arr[j].op2)==0)
{
arr[j].flag=1;
change(i,j,NULL);
} }
} }
37 | P a g e
} }
void output()
{
int i=0;
printf("nOptimized code is : ");
for(i=0;i<n;i++)
{
if(!arr[i].flag)
{
printf("n%s %s %s %sn",arr[i].op,arr[i].op1,arr[i].op2,arr[i].res);
}
}
}
void change(int p,intq,char *res)
{
int i;
for(i=q+1;i<n;i++)
{
if(strcmp(arr[q].res,arr[i].op1)==0)
if(res == NULL)
strcpy(arr[i].op1,arr[p].res);
else
strcpy(arr[i].op1,res);
else if(strcmp(arr[q].res,arr[i].op2)==0)
if(res == NULL)
38 | P a g e
strcpy(arr[i].op2,arr[p].res);
else
strcpy(arr[i].op2,res);
}
}
39 | P a g e
40 | P a g e
PRACTICAL -10
AIM:-To write a C program for Stack to use dynamic storage allocation.
OUTPUT:-
41 | P a g e
PRACTICAL -10
AIM:-To write a C program for Stack to use dynamic storage allocation.
CODING:-
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
typedef struct Heap
{
int data;
struct Heap *next;
}
node;
node *create();
void main()
{
int choice,val;
char ans;
node *head;
void display(node *);
node *search(node *,int);
node *insert(node *);
void dele(node **);
head=NULL;
do
{
printf("nProgram to perform various operations on Stack using dynamic memory
management");
printf("n1.create");
printf("n2.display");
printf("n3.insert an element in a list");
printf("n4.delete an element from list");
printf("n5.quit");
printf("nenter your chioce(1-5)");
scanf("%d",&choice);
switch(choice)
{
case 1:head=create();
break;
case 2:display(head);
break;
case 3:head=insert(head);
break;
case 4:dele(&head);
42 | P a g e
break;
case 5:exit(0);
default:
printf("invalid choice,try again");
}
}
while(choice!=5);
}
node* create()
{
node *temp,*New,*head;
int val,flag;
char ans='y';
node *get_node();
temp=NULL;
flag=TRUE;
do
{
printf("n enter the element:");
scanf("%d",&val);
New=get_node();
if(New==NULL)
printf("nmemory is not allocated");
New->data=val;
if(flag==TRUE)
{
head=New;
temp=head;
flag=FALSE;
}
else
{
temp->next=New;
temp=New;
}
printf("ndo you want to enter more elements?(y/n)");
}
while(ans=='y');
printf("nthe list is createdn");
return head;
}
node *get_node()
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->next=NULL;
return temp;
}
void display(node *head)
{
43 | P a g e
node *temp;
temp=head;
if(temp==NULL)
{
printf("nthe list is emptyn");
return;
}
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("NULL");
}
node *search(node *head,int key)
{
node *temp;
int found;
temp=head;
if(temp==NULL)
{
printf("the linked list is emptyn");
return NULL;
}
found=FALSE;
while(temp!=NULL && found==FALSE)
{
if(temp->data!=key)
temp=temp->next;
else
found=TRUE;
}
if(found==TRUE)
{
printf("nthe element is present in the listn");
return temp;
}
else
{
printf("the element is not present in the listn");
return NULL;
}
}
node *insert(node *head)
{
int choice;
node *insert_head(node *);
void insert_after(node *);
void insert_last(node *);
printf("n1.insert a node as a head node");
44 | P a g e
printf("n2.insert a node as a head node");
printf("n3.insert a node at intermediate position in t6he list");
printf("nenter your choice for insertion of node:");
scanf("%d",&choice);
switch(choice)
{
case 1:head=insert_head(head);
break;
case 2:insert_last(head);
break;
case 3:insert_after(head);
break;
}
return head;
}
node *insert_head(node *head)
{
node *New,*temp;
New=get_node();
printf("nEnter the element which you want to insert");
scanf("%d",&New->data);
if(head==NULL)
head=New;
else
{
temp=head;
New->next=temp;
head=New;
}
return head;
}
void insert_last(node *head)
{
node *New,*temp;
New=get_node();
printf("nenter the element which you want to insert");
scanf("%d",&New->data);
if(head==NULL)
head=New;
else
{
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=New;
New->next=NULL;
}
}
void insert_after(node *head)
{
45 | P a g e
int key;
node *New,*temp;
New=get_node();
printf("nenter the elements which you want to insert");
scanf("%d",&New->data);
if(head==NULL)
{
head=New;
}
else
{
printf("enter the element which you want to insert the node");
scanf("%d",&key);
temp=head;
do
{
if(temp->data==key)
{
New->next-temp->next;
temp->next=New;
return;
}
else
temp=temp->next;
}
while(temp!=NULL);
}
}
node *get_prev(node *head,intval)
{
node *temp,*prev;
int flag;
temp=head;
if(temp==NULL)
return NULL;
flag=FALSE;
prev=NULL;
while(temp!=NULL && ! flag)
{
if(temp->data!=val)
{
prev=temp;
temp=temp->next;
}
else
flag=TRUE;
}
if(flag)
return prev;
else
46 | P a g e
return NULL;
}
void dele(node **head)
{
node *temp,*prev;
int key;
temp=*head;
if(temp==NULL)
{
printf("nthe list is emptyn");
return;
}
printf("nenter the element you want to delete:");
scanf("%d",&key);
temp=search(*head,key);
if(temp!=NULL)
{
prev=get_prev(*head,key);
if(prev!=NULL)
{
prev->next=temp->next;
free(temp);
}
else
{
*head=temp->next;
free(temp);
}
printf("nthe element is deletedn");
}
}

Cd practical file (1) start se

  • 1.
    1 | Pa g e
  • 2.
    2 | Pa g e PRACTICAL -1 AIM:-To Write a C program to develop a lexical analyzer to recognize a few patterns in C. OUTPUT:-
  • 3.
    3 | Pa g e PRACTICAL -1 AIM:-To Write a C program to develop a lexical analyzer to recognize a few patterns in C. CODING:- #include <stdio.h> #include <conio.h> #include <string.h> char symTable[5][7] = { "int", "void", "float", "char", "string" }; void main() { int i, j, k = 0; char string[7]; char str[] = "int main(){printf("Hello");return 0;}"; char *ptr; clrscr(); printf("Splitting string "%s" into tokens:n", str); ptr = strtok(str, " (){};"""); printf("nn"); while (ptr != NULL) { printf ("%sn", ptr); for (i = k; i< 5; i++) { memset(&string[0], 0, sizeof(string)); for (j = 0; j < 7; j++) { string[j] = symTable[i][j]; } if (strcmp(ptr, string) == 0) { printf("Keywordnn"); break; } else if (string[j] == 0 || string[j] == 1 || string[j] == 2 || string[j] == 3 || string[j] == 4 || string[j] == 5 || string[j] == 6 || string[j] == 7 || string[j] == 8 || string[j] == 9) { printf("Constantnn"); break; } else { printf("Identifiernn"); break; } } ptr = strtok(NULL, " (){};"""); k++;
  • 4.
    4 | Pa g e } getch(); }
  • 5.
    5 | Pa g e
  • 6.
    6 | Pa g e PRACTICAL -2 AIM:-To write a program to implement the Lexical Analyzer using lex tool. OUTPUT:-
  • 7.
    7 | Pa g e PRACTICAL -2 AIM:-To write a program to implement the Lexical Analyzer using lex tool. CODING:- #include<stdio.h> #include<ctype.h> #include<conio.h> #include<string.h> char vars[100][100]; int vcnt; char input[1000],c; char token[50],tlen; int state=0,pos=0,i=0,id; char *getAddress(char str[]) { for(i=0;i<vcnt;i++) if(strcmp(str,vars[i])==0) return vars[i]; strcpy(vars[vcnt],str); return vars[vcnt++]; } int isrelop(char c) { if(c=='+'||c=='-'||c=='*'||c=='/'||c=='%'||c=='^') return 1; else return 0; } int main(void) { clrscr(); printf("Enter the Input String:"); gets(input); do { c=input[pos]; putchar(c); switch(state) { case 0: if(isspace(c)) printf("b"); if(isalpha(c)) { token[0]=c; tlen=1; state=1; }
  • 8.
    8 | Pa g e if(isdigit(c)) state=2; if(isrelop(c)) state=3; if(c==';') printf("t<3,3>n"); if(c=='=') printf("t<4,4>n"); break; case 1: if(!isalnum(c)) { token[tlen]='o'; printf("bt<1,%p>n",getAddress(token)); state=0; pos--; } else token[tlen++]=c; break; case 2: if(!isdigit(c)) { printf("bt<2,%p>n",&input[pos]); state=0; pos--; } break; case 3: id=input[pos-1]; if(c=='=') printf("t<%d,%d>n",id*10,id*10); else{ printf("bt<%d,%d>n",id,id); pos--; }state=0; break; } pos++; } while(c!=0); getch(); return 0; }
  • 9.
    9 | Pa g e
  • 10.
    10 | Pa g e PRACTICAL -3 AIM:-Design Predictive Parserof the RECURSIVE DESCENTPARSER OUTPUT:-
  • 11.
    11 | Pa g e PRACTICAL -3 AIM:-Design Predictive Parserof the RECURSIVE DESCENTPARSER LANGUAGE:- E-> TE’ E’-> +TE’ | -TE’ | null T-> FT’ T’-> *FT’| /FT’ | null F-> id/ (E)/ num CODING:- #include "stdio.h" #include "conio.h" char input[100]; char prod[100][100]; int pos=-1,l,st=-1; char id,num; void E(); void T(); void F(); void advance(); void Td(); void Ed(); void advance() { pos++; if(pos<l) { if(input[pos]>='0'&& input[pos]<='9') { num=input[pos]; id='0'; } if((input[pos]>='a' || input[pos]>='A')&&(input[pos]<='z' || input[pos]<='Z')) {id=input[pos]; num='0'; } } } void E() { strcpy(prod[++st],"E->TE'"); T(); Ed(); } void Ed() { int p=1;
  • 12.
    12 | Pa g e if(input[pos]=='+') { p=0; strcpy(prod[++st],"E'->+TE'"); advance(); T(); Ed(); } if(input[pos]=='-') { p=0; strcpy(prod[++st],"E'->-TE'"); advance(); T(); Ed(); } // Recursive Descent Parser if(p==1) { strcpy(prod[++st],"E'->null"); } } void T() { strcpy(prod[++st],"T->FT'"); F(); Td(); } void Td() { int p=1; if(input[pos]=='*') { p=0; strcpy(prod[++st],"T'->*FT'"); advance(); F(); Td(); } if(input[pos]=='/') { p=0; strcpy(prod[++st],"T'->/FT'"); advance(); F(); Td(); } if(p==1) strcpy(prod[++st],"T'->null"); }
  • 13.
    13 | Pa g e void F() { if(input[pos]==id) { strcpy(prod[++st],"F->id"); advance(); } if(input[pos]=='(') { strcpy(prod[++st],"F->(E)"); advance(); E(); if(input[pos]==')') { //strcpy(prod[++st],"F->(E)"); advance(); } } if(input[pos]==num) { strcpy(prod[++st],"F->num"); advance(); } } int main() { int i; printf("Enter Input String "); scanf("%s",input); l=strlen(input); input[l]='$'; advance(); E(); if(pos==l) { printf("String Acceptedn"); for(i=0;i<=st;i++) { printf("%sn",prod[i]); } } else { printf("String rejectedn"); } getch(); return 0; }
  • 14.
    14 | Pa g e PRACTICAL -4 AIM:-To write a C program to implement a symbol table. OUTPUT:-
  • 15.
    15 | Pa g e PRACTICAL -4 AIM:-To write a C program to implement a symbol table. CODING:- #include<stdio.h> #include<ctype.h> #include<stdlib.h> #include<string.h> #include<math.h> void main() { int i=0,j=0,x=0,n; void *p,*add[5]; char ch,srch,b[15],d[15],c; printf("Expression terminated by $:"); while((c=getchar())!='$') { b[i]=c; i++; } n=i-1; printf("Given Expression:"); i=0; while(i<=n) { printf("%c",b[i]); i++; } printf("n Symbol Tablen"); printf("Symbol t addr t type"); while(j<=n) { c=b[j]; if(isalpha(toascii(c))) { p=malloc(c); add[x]=p; d[x]=c; printf("n%c t %d t identifiern",c,p); x++; j++; } else { ch=c; if(ch=='+'||ch=='-'||ch=='*'||ch=='=')
  • 16.
    16 | Pa g e { p=malloc(ch); add[x]=p; d[x]=ch; printf("n %c t %d t operatorn",ch,p); x++; j++; }}}}
  • 17.
    17 | Pa g e
  • 18.
    18 | Pa g e PRACTICAL -5 AIM:-To design LALR bottom up parserfor the given language. OUTPUT:-
  • 19.
    19 | Pa g e PRACTICAL -5 AIM:-To design LALR bottom up parserfor the given language. CODING:- <parser.l> %{ #include<stdio.h> #include "y.tab.h" %} %% [0-9]+ {yylval.dval=atof(yytext); return DIGIT; } n|. return yytext[0]; %% <parser.y> %{ /*This YACC specification file generates the LALR parser for the program considered in experiment 4.*/ #include<stdio.h> %} %union { double dval; } %token <dval> DIGIT %type <dval> expr %type <dval> term %type <dval> factor %% line: expr 'n' { printf("%gn",$1); } ; expr: expr '+' term {$$=$1 + $3 ;} | term ; term: term '*' factor {$$=$1 * $3 ;} | factor ; factor: '(' expr ')' {$$=$2 ;} | DIGIT ; %% int main() {
  • 20.
    20 | Pa g e yyparse(); } yyerror(char *s) { printf("%s",s); }
  • 21.
    21 | Pa g e
  • 22.
    22 | Pa g e PRACTICAL -6 AIM:-To write a C program to find computation of First. OUTPUT:-
  • 23.
    23 | Pa g e PRACTICAL -6 AIM:-To write a C program to find computation of First. CODING:- #include<stdio.h> #include<math.h> #include<string.h> #include<ctype.h> #include<stdlib.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; //clrscr(); 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," "); //flushall(); m=0; follow(c); 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); }
  • 24.
    24 | Pa g e 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]); } } }
  • 25.
    25 | Pa g e
  • 26.
    26 | Pa g e PRACTICAL -7 AIM:-To find whether a given string is Keyword or not. OUTPUT:-
  • 27.
    27 | Pa g e PRACTICAL -7 AIM:-To find whether a given string is Keyword or not. CODING:- #include<stdio.h> #include<conio.h> #include<string.h> void main() { char keyword[32][10]={"auto","double","int","struct","break","else","long", "switch","case","enum","register","typedef","char", "extern","return","union","const","float","short", "unsigned","continue","for","signed","void","default", "goto","sizeof","voltile","do","if","static","while"} ; char string[10]; int flag=0,i; printf("ENTER ANY STRING:"); gets(string); for(i=0;i<32;i++) { if(strcmp(string,keyword[i])==0) { flag=1; } } if(flag==1) printf("%s is a Keywordn",string); else printf("%s is not a Keywordn",string); getch(); }
  • 28.
    28 | Pa g e PRACTICAL -8 AIM:-To find whether a given string is Identifier or not. OUTPUT:-
  • 29.
    29 | Pa g e PRACTICAL -8 AIM:-To find whether a given string is Identifier or not. CODING:- #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[100],str[20],str1[10]={"printf"}; int i,l,flag=0,s; clrscr(); printf("nenter the string:"); gets(a); if(strcmp(a,str1)==0) { printf("nnot allow"); } else { l=strlen(a); if(a[0]==' '||a[0]=='@') { printf("ninvalid identifier"); goto p; } else { s=0; while(s!=l) { if(a[s]==' '||a[s]=='1'||a[s]=='2'||a[s]=='3'|| a[s]=='4'||a[s]=='5' ||a[s]=='6' ||a[s]=='7' ||a[s]=='8'||a[s]=='9'||a[s]=='0') { printf("ninvalid identifier"); goto p; } else { flag=0; } s++;
  • 30.
    30 | Pa g e } if(flag==1) { printf("ninvalid identifier"); } if(flag==0) { printf("nvalid identifier"); } } } p: getch(); }
  • 31.
    31 | Pa g e
  • 32.
    32 | Pa g e PRACTICAL -9 AIM:-To write a C program to implement data flow & control flow analysis OUTPUT:-
  • 33.
    33 | Pa g e PRACTICAL -9 AIM:-To write a C program to implement data flow & control flow analysis CODING:- #include<stdio.h> #include<string.h> #include<ctype.h> void input(); void output(); void change(int p,intq,char *res); void constant(); void expression(); struct expr { char op[2],op1[5],op2[5],res[5]; int flag; }arr[10]; int n; int main() { int ch=0; input(); constant(); expression();
  • 34.
    34 | Pa g e output(); } void input() { int i; printf("nnEnter the maximum number of expressions:"); scanf("%d",&n); printf("nEnter the input : n"); for(i=0;i<n;i++) { scanf("%s",arr[i].op); scanf("%s",arr[i].op1); scanf("%s",arr[i].op2); scanf("%s",arr[i].res); arr[i].flag=0; } } void constant() { int i; int op1,op2,res; char op,res1[5]; for(i=0;i<n;i++) { if(isdigit(arr[i].op1[0]) &&isdigit(arr[i].op2[0]))
  • 35.
    35 | Pa g e { op1=atoi(arr[i].op1); op2=atoi(arr[i].op2); op=arr[i].op[0]; switch(op) { case '+': res=op1+op2; break; case '-': res=op1-op2; break; case '*': res=op1*op2; break; case '/': res=op1/op2; break; } sprintf(res1,"%d",res); arr[i].flag=1; change(i,i,res1); } } }
  • 36.
    36 | Pa g e void expression() { int i,j; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(strcmp(arr[i].op,arr[j].op)==0) { if(strcmp(arr[i].op,"+")==0||strcmp(arr[i].op,"*")==0) { if(strcmp(arr[i].op1,arr[j].op1)==0&&strcmp(arr[i].op2,arr[j].op2)==0 || strcmp(arr[i].op1,arr[j].op2)==0&&strcmp(arr[i].op2,arr[j].op1)==0) { arr[j].flag=1; change(i,j,NULL); } } else { if(strcmp(arr[i].op1,arr[j].op1)==0&&strcmp(arr[i].op2,arr[j].op2)==0) { arr[j].flag=1; change(i,j,NULL); } } } }
  • 37.
    37 | Pa g e } } void output() { int i=0; printf("nOptimized code is : "); for(i=0;i<n;i++) { if(!arr[i].flag) { printf("n%s %s %s %sn",arr[i].op,arr[i].op1,arr[i].op2,arr[i].res); } } } void change(int p,intq,char *res) { int i; for(i=q+1;i<n;i++) { if(strcmp(arr[q].res,arr[i].op1)==0) if(res == NULL) strcpy(arr[i].op1,arr[p].res); else strcpy(arr[i].op1,res); else if(strcmp(arr[q].res,arr[i].op2)==0) if(res == NULL)
  • 38.
    38 | Pa g e strcpy(arr[i].op2,arr[p].res); else strcpy(arr[i].op2,res); } }
  • 39.
    39 | Pa g e
  • 40.
    40 | Pa g e PRACTICAL -10 AIM:-To write a C program for Stack to use dynamic storage allocation. OUTPUT:-
  • 41.
    41 | Pa g e PRACTICAL -10 AIM:-To write a C program for Stack to use dynamic storage allocation. CODING:- #include<stdio.h> #include<stdlib.h> #define TRUE 1 #define FALSE 0 typedef struct Heap { int data; struct Heap *next; } node; node *create(); void main() { int choice,val; char ans; node *head; void display(node *); node *search(node *,int); node *insert(node *); void dele(node **); head=NULL; do { printf("nProgram to perform various operations on Stack using dynamic memory management"); printf("n1.create"); printf("n2.display"); printf("n3.insert an element in a list"); printf("n4.delete an element from list"); printf("n5.quit"); printf("nenter your chioce(1-5)"); scanf("%d",&choice); switch(choice) { case 1:head=create(); break; case 2:display(head); break; case 3:head=insert(head); break; case 4:dele(&head);
  • 42.
    42 | Pa g e break; case 5:exit(0); default: printf("invalid choice,try again"); } } while(choice!=5); } node* create() { node *temp,*New,*head; int val,flag; char ans='y'; node *get_node(); temp=NULL; flag=TRUE; do { printf("n enter the element:"); scanf("%d",&val); New=get_node(); if(New==NULL) printf("nmemory is not allocated"); New->data=val; if(flag==TRUE) { head=New; temp=head; flag=FALSE; } else { temp->next=New; temp=New; } printf("ndo you want to enter more elements?(y/n)"); } while(ans=='y'); printf("nthe list is createdn"); return head; } node *get_node() { node *temp; temp=(node*)malloc(sizeof(node)); temp->next=NULL; return temp; } void display(node *head) {
  • 43.
    43 | Pa g e node *temp; temp=head; if(temp==NULL) { printf("nthe list is emptyn"); return; } while(temp!=NULL) { printf("%d->",temp->data); temp=temp->next; } printf("NULL"); } node *search(node *head,int key) { node *temp; int found; temp=head; if(temp==NULL) { printf("the linked list is emptyn"); return NULL; } found=FALSE; while(temp!=NULL && found==FALSE) { if(temp->data!=key) temp=temp->next; else found=TRUE; } if(found==TRUE) { printf("nthe element is present in the listn"); return temp; } else { printf("the element is not present in the listn"); return NULL; } } node *insert(node *head) { int choice; node *insert_head(node *); void insert_after(node *); void insert_last(node *); printf("n1.insert a node as a head node");
  • 44.
    44 | Pa g e printf("n2.insert a node as a head node"); printf("n3.insert a node at intermediate position in t6he list"); printf("nenter your choice for insertion of node:"); scanf("%d",&choice); switch(choice) { case 1:head=insert_head(head); break; case 2:insert_last(head); break; case 3:insert_after(head); break; } return head; } node *insert_head(node *head) { node *New,*temp; New=get_node(); printf("nEnter the element which you want to insert"); scanf("%d",&New->data); if(head==NULL) head=New; else { temp=head; New->next=temp; head=New; } return head; } void insert_last(node *head) { node *New,*temp; New=get_node(); printf("nenter the element which you want to insert"); scanf("%d",&New->data); if(head==NULL) head=New; else { temp=head; while(temp->next!=NULL) temp=temp->next; temp->next=New; New->next=NULL; } } void insert_after(node *head) {
  • 45.
    45 | Pa g e int key; node *New,*temp; New=get_node(); printf("nenter the elements which you want to insert"); scanf("%d",&New->data); if(head==NULL) { head=New; } else { printf("enter the element which you want to insert the node"); scanf("%d",&key); temp=head; do { if(temp->data==key) { New->next-temp->next; temp->next=New; return; } else temp=temp->next; } while(temp!=NULL); } } node *get_prev(node *head,intval) { node *temp,*prev; int flag; temp=head; if(temp==NULL) return NULL; flag=FALSE; prev=NULL; while(temp!=NULL && ! flag) { if(temp->data!=val) { prev=temp; temp=temp->next; } else flag=TRUE; } if(flag) return prev; else
  • 46.
    46 | Pa g e return NULL; } void dele(node **head) { node *temp,*prev; int key; temp=*head; if(temp==NULL) { printf("nthe list is emptyn"); return; } printf("nenter the element you want to delete:"); scanf("%d",&key); temp=search(*head,key); if(temp!=NULL) { prev=get_prev(*head,key); if(prev!=NULL) { prev->next=temp->next; free(temp); } else { *head=temp->next; free(temp); } printf("nthe element is deletedn"); } }