Experiment No:1 Aim: Lex Analyzer
Experiment No:1 Aim: Lex Analyzer
keywords.push_back("#include");
keywords.push_back("stdio.h");
keywords.push_back("conio.h");
keywords.push_back("int");
keywords.push_back("main");
keywords.push_back("float");
keywords.push_back("char");
keywords.push_back("return");
keywords.push_back("printf");
keywords.push_back("scanf");
keywords.push_back("if");
keywords.push_back("else");
cout<<"Enter the Program:\n";
getline(cin,str);
vector<string>::iterator it;
for(it=keywords.begin();it!=keywords.end();it++){
string temp=*it;
cout<<"Keywords::\n";
for(it=rkeywords.begin();it!=rkeywords.end();it++)
cout<<*it<<"\n";
cout<<"Operator::\n";
for(it=roperators.begin();it!=roperators.end();it++)
cout<<*it<<"\n";
return 0;
}
cout<<"Keywords::\n";
for(it=rkeywords.begin();it!=rkeywords.end();it++)
cout<<*it<<"\n";
cout<<"Operator::\n";
for(it=roperators.begin();it!=roperators.end();it++)
cout<<*it<<"\n";
return 0;
}
Input:
#include <stdio.h>int main(){ int a,b,cat; a++;
at=a+b; if(cat>a) printf("%d",cat); return 0;}
Output:
Enter the Program:
Keywords::
#include
stdio.h
int
main
return
printf
scanf
if
Operator::
++
"
>
,
;
<
=
scanf("%d,%d",&a,&b);
&
{
}
#
(
)
Experiment No:2
Aim: Flex Program
Input.txt
#include<iostream.h>
void main(){
int a=10,b=20;
sum=a+b;
cout<<sum;
getch();
}
1.Line Number
%{
int yylineno;
%}
%%
^(.*)\n printf("%4d\t%s", yylineno++, yytext);
%%
int main(int argc, char *argv[]) {
yyin = fopen(argv[1], "r");
yylex();
fclose(yyin);
}
Output:
Output:
yylex();
total=s_letters+c_letters+num+spl_char;
printf(" This File contains ...");
printf("\n\t%d lines", lines);
printf("\n\t%d words",words);
printf("\n\t%d small letters", s_letters);
printf("\n\t%d capital letters",c_letters);
printf("\n\t%d digits", num);
printf("\n\t%d special characters",spl_char);
printf("\n\tIn total %d characters.\n",total);
}
int yywrap()
{
return(1);
}
Output:
Experiment No:3
c2=*(++it);
dit=find(re.begin(),re.end(),'*');
if(dit!=re.end()){
out[0][2]=2;
out[2][2]=0;
}
d=1;
}
it=find(re.begin(),re.end(),'+');
if(it!=re.end()){
out[0][0]=1;
out[0][1]=1;
c1=*(--it);
++it;
c2=*(++it);
dit=find(re.begin(),re.end(),'*');
if(dit!=re.end()){
out[0][2]=1;
out[1][2]=0;
}
d=1;
}
if(d==0){
it=find(re.begin(),re.end(),'*');
if(it!=re.end()){
out[0][0]=0;
c1=*(--it);
}}
cout<<"s\ta\tb\tE\n";
for(i=0;i<3;i++){
cout<<"q"<<i<<"\t";
for(j=0;j<3;j++){
if(out[i][j]!=-1)
cout<<"q"<<out[i][j]<<"\t";
else
cout<<" \t";
}
cout<<endl;
}
return 0;
}
it=find(re.begin(),re.end(),'.');
if(it!=re.end()){
out[0][0]=1;
out[1][1]=2;
c1=*(--it);
++it;
c2=*(++it);
}
it=find(re.begin(),re.end(),'+');
if(it!=re.end()){
out[0][0]=1;
out[0][1]=1;
c1=*(--it);
++it;
c2=*(++it);
in=2;
s=2;
}
it=find(re.begin(),re.end(),'*');
if(it!=re.end()){
out[0][0]=0;
c1=*(--it);
in=1;
s=1;
}
cout<<"s\ta\tb\tE\n";
for(i=0;i<3;i++){
cout<<"q"<<i<<"\t";
for(j=0;j<3;j++){
if(out[i][j]!=-1)
cout<<"q"<<out[i][j]<<"\t";
else
cout<<" \t";
}
cout<<endl;
}
return 0;
}
Output: