SlideShare a Scribd company logo
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");
}
}

More Related Content

What's hot

Itp practical file_1-year
Itp practical file_1-yearItp practical file_1-year
Itp practical file_1-year
AMIT SINGH
 
C programms
C programmsC programms
C programms
Mukund Gandrakota
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
Chris Ohk
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manualSANTOSH RATH
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
Abdullah Al Naser
 
C Programming
C ProgrammingC Programming
C Programming
Sumant Diwakar
 
C programs
C programsC programs
C programs
Vikram Nandini
 
some basic C programs with outputs
some basic C programs with outputssome basic C programs with outputs
some basic C programs with outputs
KULDEEPSING PATIL
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
Rahul Pandit
 
C programs
C programsC programs
C programsMinu S
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
JAYA
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
KavyaSharma65
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
Harjinder Singh
 
c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointers
Sushil Mishra
 
C programming pointer
C  programming pointerC  programming pointer
C programming pointer
argusacademy
 

What's hot (20)

week-12x
week-12xweek-12x
week-12x
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
Itp practical file_1-year
Itp practical file_1-yearItp practical file_1-year
Itp practical file_1-year
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
C programms
C programmsC programms
C programms
 
C program
C programC program
C program
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
 
C Programming
C ProgrammingC Programming
C Programming
 
C programs
C programsC programs
C programs
 
some basic C programs with outputs
some basic C programs with outputssome basic C programs with outputs
some basic C programs with outputs
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
C programs
C programsC programs
C programs
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointers
 
C programming pointer
C  programming pointerC  programming pointer
C programming pointer
 

Similar to Cd practical file (1) start se

C basics
C basicsC basics
C basicsMSc CST
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
Ashishchinu
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
PRATHAMESH DESHPANDE
 
String Manipulation Function and Header File Functions
String Manipulation Function and Header File FunctionsString Manipulation Function and Header File Functions
Pattern printing-in-c(Jaydip Kikani)
Pattern printing-in-c(Jaydip Kikani)Pattern printing-in-c(Jaydip Kikani)
Pattern printing-in-c(Jaydip Kikani)
Jaydip JK
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
Arkadeep Dey
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in cgkgaur1987
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
Saket Pathak
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
sandeep kumbhkar
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysisVishal Singh
 
Basic C Programming Lab Practice
Basic C Programming Lab PracticeBasic C Programming Lab Practice
Basic C Programming Lab Practice
Mahmud Hasan Tanvir
 
Programming fundamentals
Programming fundamentalsProgramming fundamentals
Programming fundamentals
Zaibi Gondal
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
Ashutoshprasad27
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
Ashutoshprasad27
 
C- Programming Assignment 3
C- Programming Assignment 3C- Programming Assignment 3
C- Programming Assignment 3
Animesh Chaturvedi
 
pattern-printing-in-c.pdf
pattern-printing-in-c.pdfpattern-printing-in-c.pdf
pattern-printing-in-c.pdf
RSathyaPriyaCSEKIOT
 
data structure and algorithm.pdf
data structure and algorithm.pdfdata structure and algorithm.pdf
data structure and algorithm.pdf
Asrinath1
 

Similar to Cd practical file (1) start se (20)

C basics
C basicsC basics
C basics
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
String Manipulation Function and Header File Functions
String Manipulation Function and Header File FunctionsString Manipulation Function and Header File Functions
String Manipulation Function and Header File Functions
 
Pattern printing-in-c(Jaydip Kikani)
Pattern printing-in-c(Jaydip Kikani)Pattern printing-in-c(Jaydip Kikani)
Pattern printing-in-c(Jaydip Kikani)
 
Data struture lab
Data struture labData struture lab
Data struture lab
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysis
 
Basic C Programming Lab Practice
Basic C Programming Lab PracticeBasic C Programming Lab Practice
Basic C Programming Lab Practice
 
Programming fundamentals
Programming fundamentalsProgramming fundamentals
Programming fundamentals
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
 
20DCE096_prac8.pdf
20DCE096_prac8.pdf20DCE096_prac8.pdf
20DCE096_prac8.pdf
 
C- Programming Assignment 3
C- Programming Assignment 3C- Programming Assignment 3
C- Programming Assignment 3
 
pattern-printing-in-c.pdf
pattern-printing-in-c.pdfpattern-printing-in-c.pdf
pattern-printing-in-c.pdf
 
data structure and algorithm.pdf
data structure and algorithm.pdfdata structure and algorithm.pdf
data structure and algorithm.pdf
 

Recently uploaded

How about Huawei mobile phone-www.cfye-commerce.shop
How about Huawei mobile phone-www.cfye-commerce.shopHow about Huawei mobile phone-www.cfye-commerce.shop
How about Huawei mobile phone-www.cfye-commerce.shop
laozhuseo02
 
Drip Irrigation technology with solar power
Drip Irrigation technology with solar powerDrip Irrigation technology with solar power
Drip Irrigation technology with solar power
anikchanda4
 
Alert-driven Community-based Forest monitoring: A case of the Peruvian Amazon
Alert-driven Community-based Forest monitoring: A case of the Peruvian AmazonAlert-driven Community-based Forest monitoring: A case of the Peruvian Amazon
Alert-driven Community-based Forest monitoring: A case of the Peruvian Amazon
CIFOR-ICRAF
 
"Understanding the Carbon Cycle: Processes, Human Impacts, and Strategies for...
"Understanding the Carbon Cycle: Processes, Human Impacts, and Strategies for..."Understanding the Carbon Cycle: Processes, Human Impacts, and Strategies for...
"Understanding the Carbon Cycle: Processes, Human Impacts, and Strategies for...
MMariSelvam4
 
Epcon is One of the World's leading Manufacturing Companies.
Epcon is One of the World's leading Manufacturing Companies.Epcon is One of the World's leading Manufacturing Companies.
Epcon is One of the World's leading Manufacturing Companies.
EpconLP
 
Daan Park Hydrangea flower season I like it
Daan Park Hydrangea flower season I like itDaan Park Hydrangea flower season I like it
Daan Park Hydrangea flower season I like it
a0966109726
 
Summary of the Climate and Energy Policy of Australia
Summary of the Climate and Energy Policy of AustraliaSummary of the Climate and Energy Policy of Australia
Summary of the Climate and Energy Policy of Australia
yasmindemoraes1
 
Willie Nelson Net Worth: A Journey Through Music, Movies, and Business Ventures
Willie Nelson Net Worth: A Journey Through Music, Movies, and Business VenturesWillie Nelson Net Worth: A Journey Through Music, Movies, and Business Ventures
Willie Nelson Net Worth: A Journey Through Music, Movies, and Business Ventures
greendigital
 
Q&A with the Experts: The Food Service Playbook
Q&A with the Experts: The Food Service PlaybookQ&A with the Experts: The Food Service Playbook
Q&A with the Experts: The Food Service Playbook
World Resources Institute (WRI)
 
Navigating the complex landscape of AI governance
Navigating the complex landscape of AI governanceNavigating the complex landscape of AI governance
Navigating the complex landscape of AI governance
Piermenotti Mauro
 
UNDERSTANDING WHAT GREEN WASHING IS!.pdf
UNDERSTANDING WHAT GREEN WASHING IS!.pdfUNDERSTANDING WHAT GREEN WASHING IS!.pdf
UNDERSTANDING WHAT GREEN WASHING IS!.pdf
JulietMogola
 
growbilliontrees.com-Trees for Granddaughter (1).pdf
growbilliontrees.com-Trees for Granddaughter (1).pdfgrowbilliontrees.com-Trees for Granddaughter (1).pdf
growbilliontrees.com-Trees for Granddaughter (1).pdf
yadavakashagra
 
Artificial Reefs by Kuddle Life Foundation - May 2024
Artificial Reefs by Kuddle Life Foundation - May 2024Artificial Reefs by Kuddle Life Foundation - May 2024
Artificial Reefs by Kuddle Life Foundation - May 2024
punit537210
 
Climate Change All over the World .pptx
Climate Change All over the World  .pptxClimate Change All over the World  .pptx
Climate Change All over the World .pptx
sairaanwer024
 
Sustainable farming practices in India .pptx
Sustainable farming  practices in India .pptxSustainable farming  practices in India .pptx
Sustainable farming practices in India .pptx
chaitaliambole
 
International+e-Commerce+Platform-www.cfye-commerce.shop
International+e-Commerce+Platform-www.cfye-commerce.shopInternational+e-Commerce+Platform-www.cfye-commerce.shop
International+e-Commerce+Platform-www.cfye-commerce.shop
laozhuseo02
 
Characterization and the Kinetics of drying at the drying oven and with micro...
Characterization and the Kinetics of drying at the drying oven and with micro...Characterization and the Kinetics of drying at the drying oven and with micro...
Characterization and the Kinetics of drying at the drying oven and with micro...
Open Access Research Paper
 
Celebrating World-environment-day-2024.pdf
Celebrating  World-environment-day-2024.pdfCelebrating  World-environment-day-2024.pdf
Celebrating World-environment-day-2024.pdf
rohankumarsinghrore1
 
AGRICULTURE Hydrophonic FERTILISER PPT.pptx
AGRICULTURE Hydrophonic FERTILISER PPT.pptxAGRICULTURE Hydrophonic FERTILISER PPT.pptx
AGRICULTURE Hydrophonic FERTILISER PPT.pptx
BanitaDsouza
 
一比一原版(UMTC毕业证书)明尼苏达大学双城分校毕业证如何办理
一比一原版(UMTC毕业证书)明尼苏达大学双城分校毕业证如何办理一比一原版(UMTC毕业证书)明尼苏达大学双城分校毕业证如何办理
一比一原版(UMTC毕业证书)明尼苏达大学双城分校毕业证如何办理
zm9ajxup
 

Recently uploaded (20)

How about Huawei mobile phone-www.cfye-commerce.shop
How about Huawei mobile phone-www.cfye-commerce.shopHow about Huawei mobile phone-www.cfye-commerce.shop
How about Huawei mobile phone-www.cfye-commerce.shop
 
Drip Irrigation technology with solar power
Drip Irrigation technology with solar powerDrip Irrigation technology with solar power
Drip Irrigation technology with solar power
 
Alert-driven Community-based Forest monitoring: A case of the Peruvian Amazon
Alert-driven Community-based Forest monitoring: A case of the Peruvian AmazonAlert-driven Community-based Forest monitoring: A case of the Peruvian Amazon
Alert-driven Community-based Forest monitoring: A case of the Peruvian Amazon
 
"Understanding the Carbon Cycle: Processes, Human Impacts, and Strategies for...
"Understanding the Carbon Cycle: Processes, Human Impacts, and Strategies for..."Understanding the Carbon Cycle: Processes, Human Impacts, and Strategies for...
"Understanding the Carbon Cycle: Processes, Human Impacts, and Strategies for...
 
Epcon is One of the World's leading Manufacturing Companies.
Epcon is One of the World's leading Manufacturing Companies.Epcon is One of the World's leading Manufacturing Companies.
Epcon is One of the World's leading Manufacturing Companies.
 
Daan Park Hydrangea flower season I like it
Daan Park Hydrangea flower season I like itDaan Park Hydrangea flower season I like it
Daan Park Hydrangea flower season I like it
 
Summary of the Climate and Energy Policy of Australia
Summary of the Climate and Energy Policy of AustraliaSummary of the Climate and Energy Policy of Australia
Summary of the Climate and Energy Policy of Australia
 
Willie Nelson Net Worth: A Journey Through Music, Movies, and Business Ventures
Willie Nelson Net Worth: A Journey Through Music, Movies, and Business VenturesWillie Nelson Net Worth: A Journey Through Music, Movies, and Business Ventures
Willie Nelson Net Worth: A Journey Through Music, Movies, and Business Ventures
 
Q&A with the Experts: The Food Service Playbook
Q&A with the Experts: The Food Service PlaybookQ&A with the Experts: The Food Service Playbook
Q&A with the Experts: The Food Service Playbook
 
Navigating the complex landscape of AI governance
Navigating the complex landscape of AI governanceNavigating the complex landscape of AI governance
Navigating the complex landscape of AI governance
 
UNDERSTANDING WHAT GREEN WASHING IS!.pdf
UNDERSTANDING WHAT GREEN WASHING IS!.pdfUNDERSTANDING WHAT GREEN WASHING IS!.pdf
UNDERSTANDING WHAT GREEN WASHING IS!.pdf
 
growbilliontrees.com-Trees for Granddaughter (1).pdf
growbilliontrees.com-Trees for Granddaughter (1).pdfgrowbilliontrees.com-Trees for Granddaughter (1).pdf
growbilliontrees.com-Trees for Granddaughter (1).pdf
 
Artificial Reefs by Kuddle Life Foundation - May 2024
Artificial Reefs by Kuddle Life Foundation - May 2024Artificial Reefs by Kuddle Life Foundation - May 2024
Artificial Reefs by Kuddle Life Foundation - May 2024
 
Climate Change All over the World .pptx
Climate Change All over the World  .pptxClimate Change All over the World  .pptx
Climate Change All over the World .pptx
 
Sustainable farming practices in India .pptx
Sustainable farming  practices in India .pptxSustainable farming  practices in India .pptx
Sustainable farming practices in India .pptx
 
International+e-Commerce+Platform-www.cfye-commerce.shop
International+e-Commerce+Platform-www.cfye-commerce.shopInternational+e-Commerce+Platform-www.cfye-commerce.shop
International+e-Commerce+Platform-www.cfye-commerce.shop
 
Characterization and the Kinetics of drying at the drying oven and with micro...
Characterization and the Kinetics of drying at the drying oven and with micro...Characterization and the Kinetics of drying at the drying oven and with micro...
Characterization and the Kinetics of drying at the drying oven and with micro...
 
Celebrating World-environment-day-2024.pdf
Celebrating  World-environment-day-2024.pdfCelebrating  World-environment-day-2024.pdf
Celebrating World-environment-day-2024.pdf
 
AGRICULTURE Hydrophonic FERTILISER PPT.pptx
AGRICULTURE Hydrophonic FERTILISER PPT.pptxAGRICULTURE Hydrophonic FERTILISER PPT.pptx
AGRICULTURE Hydrophonic FERTILISER PPT.pptx
 
一比一原版(UMTC毕业证书)明尼苏达大学双城分校毕业证如何办理
一比一原版(UMTC毕业证书)明尼苏达大学双城分校毕业证如何办理一比一原版(UMTC毕业证书)明尼苏达大学双城分校毕业证如何办理
一比一原版(UMTC毕业证书)明尼苏达大学双城分校毕业证如何办理
 

Cd practical file (1) start se

  • 1. 1 | P a g e
  • 2. 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. 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. 4 | P a g e } getch(); }
  • 5. 5 | P a g e
  • 6. 6 | P a g e PRACTICAL -2 AIM:-To write a program to implement the Lexical Analyzer using lex tool. OUTPUT:-
  • 7. 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. 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. 9 | P a g e
  • 10. 10 | P a g e PRACTICAL -3 AIM:-Design Predictive Parserof the RECURSIVE DESCENTPARSER OUTPUT:-
  • 11. 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. 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. 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. 14 | P a g e PRACTICAL -4 AIM:-To write a C program to implement a symbol table. OUTPUT:-
  • 15. 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. 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. 17 | P a g e
  • 18. 18 | P a g e PRACTICAL -5 AIM:-To design LALR bottom up parserfor the given language. OUTPUT:-
  • 19. 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. 20 | P a g e yyparse(); } yyerror(char *s) { printf("%s",s); }
  • 21. 21 | P a g e
  • 22. 22 | P a g e PRACTICAL -6 AIM:-To write a C program to find computation of First. OUTPUT:-
  • 23. 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. 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. 25 | P a g e
  • 26. 26 | P a g e PRACTICAL -7 AIM:-To find whether a given string is Keyword or not. OUTPUT:-
  • 27. 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. 28 | P a g e PRACTICAL -8 AIM:-To find whether a given string is Identifier or not. OUTPUT:-
  • 29. 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. 30 | P a g e } if(flag==1) { printf("ninvalid identifier"); } if(flag==0) { printf("nvalid identifier"); } } } p: getch(); }
  • 31. 31 | P a g e
  • 32. 32 | P a g e PRACTICAL -9 AIM:-To write a C program to implement data flow & control flow analysis OUTPUT:-
  • 33. 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. 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. 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. 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. 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. 38 | P a g e strcpy(arr[i].op2,arr[p].res); else strcpy(arr[i].op2,res); } }
  • 39. 39 | P a g e
  • 40. 40 | P a g e PRACTICAL -10 AIM:-To write a C program for Stack to use dynamic storage allocation. OUTPUT:-
  • 41. 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. 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. 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. 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. 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. 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"); } }