Professional Documents
Culture Documents
Nau Shik
Nau Shik
Page
No. Practical Title Date No. Sign
1. Write a Program for Lexical analyzer which
takes any text file as input and find
identifier, constant, operator from input
file.
2. Write a program to remove the left recursion
from any given grammar.
3. Write a program to remove the left factor from
any given grammar.
4. Write a program to find first and follow set
from any given grammar.
5. Write a program using C (or C++) for LL(1)
Parsing.
6. Write a program using C (or C++) for Operator
Precedence parsing.
7. Write a program using C (or C++) for SLR
parsing.
8. Write a program using C (or C++) for LALR
parsing.
9. write a program using C(or C++) for LALR
parsing.
INDEX
int main(){
char ch, buffer[15], operators[] = "+-*/%=";
ifstream fin("program.txt");
int i,j=0;
if(!fin.is_open()){
cout<<"error while opening the file\n";
exit(0);
}
while(!fin.eof()){
ch = fin.get();
if(isalnum(ch)){
buffer[j++] = ch;
}
if(isKeyword(buffer) == 1)
cout<<buffer<<" is keyword\n";
else
cout<<buffer<<" is indentifier\n";
}
fin.close();
return 0;
}
int main()
{
char nonterminal;
char beta,alpha;
int n;
char production[10][MAX];
int index=3; //Starting of String
int i;
printf("Enter no of production:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(i=0;i<n;i++)
{
printf("\nGrammer : : %s",production[i]);
nonterminal=production[i][0];
if(nonterminal==production[i][index])
{
alpha=production[i][index+1];
printf("\nIt's left recursive...");
while(production[i][index]!=0 &&
production[i][index]!='|')
index++;
if(production[i][index]!=0)
{
beta=production[i][index+1];
printf("\nGrammer without left recursion ..\n");
printf("\n%c-
>%c%c\'",nonterminal,beta,nonterminal);
printf("\n%c\'-
>%c%c\'|E\n",nonterminal,alpha,nonterminal);
}
else
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;
a3[k]='X';
a3[++k]='\0';
a4[l]='/';
a5[l]='\0';
a4[++l]='\0';
strcat(a4,a5);
printf("\n A->%s",a3);
printf("\n X->%s",a4);
getch();
}
int i,j,l,m,n=0,o,p,nv,z=0,x=0;
char str[10],temp,temp2[10],temp3[20],*ptr;
struct prod
{
char lhs[10],rhs[10][10],ft[10],fol[10];
int n;
}pro[10];
void findter()
{
int k,t;
for(k=0;k<n;k++)
{
if(temp==pro[k].lhs[0])
{
for(t=0;t<pro[k].n;t++)
{
void findfol()
{
int k,t,p1,o1,chk;
char *ptr1;
for(k=0;k<n;k++)
{
chk=0;
for(t=0;t<pro[k].n;t++)
{
int main()
{
FILE *f;
//clrscr();
for(i=0;i<10;i++)
pro[i].n=0;
f=fopen("tab5.txt","r");
while(!feof(f))
{
fscanf(f,"%s",pro[n].lhs);
if(n>0)
pro[0].ft[0]='#';
for(i=0;i<n;i++)
{
for(j=0;j<pro[i].n;j++)
{
printf("\n\nFIRST\n");
for(i=0;i<n;i++)
{
printf("\n%s -> ",pro[i].lhs);
for(j=0;j<strlen(pro[i].ft);j++)
{
for(l=j-1;l>=0;l--)
if(pro[i].ft[l]==pro[i].ft[j])
break;
if(l==-1)
for(i=0;i<n;i++)
temp2[i]=pro[i].lhs[0];
pro[0].fol[0]='$';
for(i=0;i<n;i++)
{
for(l=0;l<n;l++)
{
for(j=0;j<pro[i].n;j++)
{
ptr=strchr(pro[l].rhs[j],temp2[i]);
if( ptr )
{
p=ptr-pro[l].rhs[j];
if(pro[l].rhs[j][p+1]>=65 && pro[l].rhs[j][p+1]<=90)
{
for(o=0;o<n;o++)
if(pro[o].lhs[0]==pro[l].rhs[j][p+1])
strcat(pro[i].fol,pro[o].ft);
}
else if(pro[l].rhs[j][p+1]=='\0')
printf("\n\nFOLLOW\n");
for(i=0;i<n;i++)
{
printf("\n%s -> ",pro[i].lhs);
for(j=0;j<strlen(pro[i].fol);j++)
{
for(l=j-1;l>=0;l--)
if(pro[i].fol[l]==pro[i].fol[j])
break;
if(strcmp(array[n][l],'\0')==0)
{
if(i==(strlen(str)-1))
{
int len=strlen(str1);
str1[len-1]='\0';
cout << "\n\t"<<str1;
cout << "\n\n\tENTERED STRING IS VALID";
if(f==0)
{
strcpy(temp,'\0');
strcpy(temp1,'\0');
strcpy(temp2,'\0');
strcpy(t,'\0');
i++;
k=0;
goto again;
}
else
{
strcpy(temp1,'\0');
strcpy(temp2,'\0');
strcpy(t,'\0');
goto again1;
}
stack[top]='a';
printf("\nEnter the input string:");
scanf("%s",ip);
i=0;
printf("\n\nSTACK\t\tINPUT STRING\t\tACTION\n");
printf("\n%s\t\t%s\t\t",stack,ip);
while(i<=strlen(ip) )
{
for(j=0;j<st;j++)
{
if(stack[top]==st1[j])
a) Write a Lex program which takes input from text file and count no of
characters. no.of lines & no. of words.
Program :
PROCEDURE:
Step 1:- right click and open terminal.
Step 2:- write command gedit pr1.l
Step 3:- Now press the CNTR+S to save file.
Step 4:- Now write command lex pr1.l
Step 5:- Now write command cc lex.yy.c –ll for compile the file.
Step 6:- Now write command ./a.out to see the output.
%{
intnum_lines = 0, num_chars = 0 , num_words=0;
%}
%%
\n ++num_lines; ++num_chars ; ++num_words;
[ \t] ++num_words;
. ++num_chars;
%%
main()
{
yyin=fopen("input.txt","r");
yylex();
printf( "# of lines = %d, # of chars = %d ,# of words = %d\n", num_lines,
num_chars,num_words);
dig [0-9]+|([0-9]*)"."([0-9]+)
add "+"
sub "-"
mul "*"
div "/"
pow "^"
ln \n
%%
{dig} {digi();} /*** digi() is a user defined function ***/
{add} {op=1;}
{sub} {op=2;}
{mul} {op=3;}
{div} {op=4;}
%%
digi()
{
if(op==0)
a=atof(yytext); /*** atof() is used to convert the ASCII input to float***/
else
{
b=atof(yytext);
switch(op)
{
case 1:a=a+b;
break;
case 2:a=a-b;
break;
case 3:a=a*b;
break;
case 4:a=a/b;
break;
case 5:for(i=a;b>1;b--)
a=a*i;
break;