Professional Documents
Culture Documents
on
Third Year
CS-321
Program:
#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;
printf("Enter the no of productions:\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 elements whose first & 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);
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]);
}
}
}
}
Experiment 1: Output
Experiment NO.-2
Program:
#include <stdio.h>
#include <conio.h>
main()
{
int
i=0,flag=0;
char keyw[10][10] ={"int", "float", "break", "long", "char", "for","if", "switch",
"else", "while"},a[10];
printf("Enter Identifier : ");
gets(a);
for(i=0;i<10;i++)
{
if((strcmp(keyw[i],a)==0))
{
flag=1;
}
}
if(flag==1)
{
printf("\n%s is Keyword.",a);
}
else
{
flag=0;
if((a[0]=='_')||(isalpha(a[0])!=0))
{
for(i=1;a[i]!='\0';i++)
{
if((isalnum(a[i])==0)&&(a[i]!='_'))
{
flag=1;
}
}
}
else
{
flag=1;
}
}
if(flag==0)
{
printf("\n%s is an Identifier.",a);
}
else
{
printf("\n%s is Not an Identifier.",a);
}
getch();
}
Experiment 2: Output
Experiment NO - 3
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
count[i][j]=NULL;
for(k=0;k<10;k++){
res[i][j][k]=NULL;}
}
}
printf("Enter the no of productions:");
scanf("%d",&npro);
printf("Enter the productions:");
for(i=0;i<npro;i++)
{
scanf("%s",pro[i]);
}
for(i=0;i<npro;i++)
{
flag=0;
for(j=0;j<nont;j++)
{
if(nt[j]==pro[i][0])
{
flag=1;
}
}
if(flag==0)
{
nt[nont]=pro[i][0];
nont++;
}
}
printf("\nEnter the first values:\n");
for(i=0;i<nont;i++)
{
printf("First value(%c):",nt[i]);
scanf("%s",first[i]);
}
printf("\nEnter the follow values:\n");
for(i=0;i<nont;i++)
{
printf("Follow value(%c):",nt[i]);
scanf("%s",follow[i]);
}
for(i=0;i<nont;i++)
{
flag=0;
for(j=0;j<strlen(first[i]);j++)
{
for(k=0;k<noter;k++)
{
if(ter[k]==first[i][j])
{
flag=1;
}
}
if(flag==0)
{
if(first[i][j]!='#')
{
ter[noter]=first[i][j];
noter++;
}
}
}
}
for(i=0;i<nont;i++)
{
flag=0;
for(j=0;j<strlen(follow[i]);j++)
{
for(k=0;k<noter;k++)
{
if(ter[k]==follow[i][j])
{
flag=1;
}
}
if(flag==0)
{
ter[noter]=follow[i][j];
noter++;
}
}
}
for(i=0;i<nont;i++)
{
for(j=0;j<strlen(first[i]);j++)
{
flag=0;
if(first[i][j]=='#')
{
col=i;
for(m=0;m<strlen(follow[col]);m++)
{
for(l=0;l<noter;l++)
{
if(ter[l]==follow[col][m])
{
row=l;
}
}
temp[0]=nt[col];
temp[1]='-' ;
temp[2]='>';
temp[3]='#';
temp[4]='\0';
printf("temp %s",temp);
strcpy(res[col][row],temp);
count[col][row]+=1;
for(k=0;k<10;k++){
temp[k]=NULL; }
}
}
else{
for(l=0;l<noter;l++)
{
if(ter[l]==first[i][j])
{
row=l;
}
}
for(k=0;k<npro;k++){
if(nt[i]==pro[k][0])
{
col=i;
if((pro[k][3]==first[i][j])&&(pro[k][0]==nt[col]))
{
strcpy(res[col][row],pro[k]);
count[col][row]+=1;
}
else
{
if((isupper(pro[k][3]))&&(pro[k][0]==nt[col]))
{
flag=0;
for(m=0;m<nont;m++)
{
if(nt[m]==pro[k][3]){index=m;flag=1;}
}
if(flag==1){
for(m=0;m<strlen(first[index]);m++)
{if(first[i][j]==first[index][m])
{strcpy(res[col][row],pro[k]);
count[col][row]+=1;}
}
}
}}}}}
}}
flag=0;
for(j=0;j<nont;j++)
{
for(k=0;k<noter;k++)
{
if(count[j][k]>1){flag=1;}
}
}
if(flag==1){printf("\nThe given grammar is not LL1");}
else{printf("\nThe given grammar is LL1");}
getch();
Experiment 3 : Output
Experiment NO. - 4
Program:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int i, flag = 0;
for(i = 0; i < 32; ++i)
{
if(strcmp(keywords[i], buffer) == 0)
{ 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);
}
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);
}
fclose(fp);
return 0;
}
Experiment 4: Output
Experiment NO.-5
Program:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
void Tprime();
void Eprime();
void E();
void check();
void T();
char expression[10];
int count, flag;
int main()
{
count = 0;
flag = 0;
printf("\nEnter an Algebraic Expression:\t");
scanf("%s", expression);
E();
if((strlen(expression) == count) && (flag == 0))
{
printf("\nThe Expression %s is Valid\n", expression);
}
else
{
printf("\nThe Expression %s is Invalid\n", expression);
}
}
void E()
{
T();
Eprime();
}
void T()
{
check();
Tprime();
}
void Tprime()
{
if(expression[count] == '*')
{
count++;
check();
Tprime();
}
}
void check()
{
if(isalnum(expression[count]))
{
count++;
}
else if(expression[count] == '(')
{
count++;
E();
if(expression[count] == ')')
{
count++;
}
else
{
flag = 1;
}
}
else
{
flag = 1;
}
}
void Eprime()
{
if(expression[count] == '+')
{
count++;
T();
Eprime();
}
}
Experiment 5: Output
Experiment NO.-6
Program:
#include<stdio.h>
#include<conio.h>
char stack[30];
int top=-1;
void push(char c)
{
top++;
stack[top]=c;
}
char pop()
{
char c;
if(top!=-1)
{
c=stack[top];
top--;
return c;
}
return'x';
}
void printstat()
{
int i;
printf("\n\t\t\t $");
for(i=0;i<=top;i++)
printf("%c",stack[i]);
}
void main()
{
int i,j,k,l;
char s1[20],s2[20],ch1,ch2,ch3;
printf("\n\n\t\t LR PARSING");
printf("\n\t\t ENTER THE EXPRESSION");
scanf("%s",s1);
l=strlen(s1);
j=0;
printf("\n\t\t $");
for(i=0;i<l;i++)
{
if(s1[i]=='i' && s1[i+1]=='d')
{
s1[i]=' ';
s1[i+1]='E';
printstat(); printf("id");
push('E');
printstat();
}
else if(s1[i]=='+'||s1[i]=='-'||s1[i]=='*' ||s1[i]=='/' ||s1[i]=='d')
{
push(s1[i]);
printstat();
}
}
printstat();
l=strlen(s2);
while(l)
{
ch1=pop();
if(ch1=='x')
{
printf("\n\t\t\t $");
break;
}
if(ch1=='+'||ch1=='/'||ch1=='*'||ch1=='-')
{
ch3=pop();
if(ch3!='E')
{
printf("errror");
}
else
{
push('E');
printstat();
}
}
ch2=ch1;
}
getch();
}
Experiment 6: Output
Experiment NO.-7
Experiment 7: Output
Experiment NO.-8
Program:
#include<stdio.h>
#include<stdlib.h>
#define Blank ' '
#define Tab '\t'
#define MAX 100
while(choice == 'y')
{
top = 0;
printf("Enter Expression : ");
fflush(stdin);
gets(expression);
expression_to_syntaxtree();
printf("Expression Tree Format : %s\n",syntaxtree);
printf("Want to continue(y/n) : ");
scanf("%c",&choice);
}
}
expression_to_syntaxtree()
{
int i,p=0,type,precedence,len;
char next ;
stack[top]='#';
len=strlen(expression);
expression[len]='#';
for(i=0; expression[i]!='#';i++)
{
if( !white_space(expression[i]))
{
switch(expression[i])
{
case '(':
push(expression[i]);
break;
case ')':
while((next = pop()) != '(')
syntaxtree[p++] = next;
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
precedence = prec(expression[i]);
while(stack[top]!='#' && precedence<= prec(stack[top]))
syntaxtree[p++] = pop();
push(expression[i]);
break;
default: /*if an operand comes */
syntaxtree[p++] = expression[i];
}/*End of switch */
}/*End of if */
}/*End of for */
while(stack[top]!='#')
syntaxtree[p++] = pop();
syntaxtree[p] = '\0' ; /*End syntaxtree with'\0' to make it a string*/
}
prec(char symbol )
{
switch(symbol)
{
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
case '^':
return 3;
}
}
white_space(char symbol)
{
if( symbol == Blank || symbol == Tab || symbol == '\0')
return 1;
else
return 0;
}
Experiment 8: Output
Experiment NO.-9
However, as we will see, it can be applied to almost any situation where text-
based input is being used.
Experiment NO.-10
Objective: Write a lex Program to convert lowercase to uppercase & reverse - lex
program
Program:
lower [a-z]
CAPS [A-Z]
space [ \t\n]
%%
{lower} {printf("%c",yytext[0]- 32);}
{CAPS} {printf("%c",yytext[0]+ 32);}
{space} ECHO;
. ECHO;
%%
main()
{
yylex();
}
Experiment 10: Output
Experiment NO.-11
Objective: Write a LEX program to find out total number of vowels from
the given input string.
Program:
%{
int count=0;
%}
%%
[aeiouAEIOU] {count++;ECHO;}
%%
main()
{
yylex();
printf("\nNumber of vowels= %d\n",count);
return 0;
}
Experiment 11: Output
Experiment NO: 12
%%
\n ++no_of_lines;
. ++no_of_chars;
end return 0;
%%
int yywrap(){}
int main(int argc, char **argv)
{
yylex();
printf("number of lines = %d, number of chars = %d\n",
no_of_lines, no_of_chars );
return 0;
}
Experiment 12: Output
Experiment NO : 13
\+?{DIGIT}*\.{DIGIT}+ positivefractions++;
-{DIGIT}*\.{DIGIT}+ negativefractions++;
.;
%%
main()
{
yylex();
printf("\nNo. of positive numbers: %d",postiveno);
printf("\nNo. of Negative numbers: %d",negtiveno);
printf("\nNo. of Positive fractions: %d",positivefractions);
printf("\nNo. of Negative fractions: %d\n",negativefractions);
}
Experiment 14: Output
Experiment NO : 15
// rule section
%%
int main()
yylex();