0% found this document useful (0 votes)
121 views

Lex Program

This document contains 6 Lex/Yacc programs that perform various tasks: 1. The first two programs count characters, words, lines in a file and comments in a file. 2. The third program checks if an input is a valid arithmetic expression using a stack. 3. The fourth program checks if an input statement is simple or complex. 4. The remaining programs check for valid variables, evaluate arithmetic expressions, and check for specific regular language patterns.

Uploaded by

motwanishelly
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views

Lex Program

This document contains 6 Lex/Yacc programs that perform various tasks: 1. The first two programs count characters, words, lines in a file and comments in a file. 2. The third program checks if an input is a valid arithmetic expression using a stack. 3. The fourth program checks if an input statement is simple or complex. 4. The remaining programs check for valid variables, evaluate arithmetic expressions, and check for specific regular language patterns.

Uploaded by

motwanishelly
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Lex program to count the number of characters words lines

%{
int nchar=0,nword=0,nline=0;
#include<stdio.h>
%}
%%
\n {nline++;nchar++;}
[^ \t\n]+ {nword++;nchar+=yyleng;}
. {nchar++;}
%%
int main()
{
yylex();
printf("%d%d%d",nchar,nword,nline);
}

Lex program to co
%{
#include
int comments=0;
%]
%%
"//".* {comments++;}
"/*"[a-zA-Z0-9\n]*"*/" {comments++;}
%%
main()
{
char s[10],d[10];
printf("Enter the source file and destination file\n");
scanf("%s%s",s,d);
yyin=fopen(s,"r"); /*open input file in read mode*/
yyout=fopen(d,"w"); /*open output file in write mode*/
yylex();
printf("Number of comments = %d\n",comments);
fclose(yyin);
fclose(yyout);
}

1a.Count characters, lines and words

%{
int s=0,w=0,l=1,c=0;
%}
%%
[\n]*

{ l+=yyleng;}

[ \t]*
{ s+=yyleng;}
[^ \n\t]*[\t] { w++;c+=yyleng-1;s++;}
[^ \n\t]*[ ] { w++;c+=yyleng-1;s++;}
[^ \n\t]*[\n] { w++;c+=yyleng-1;l++;}
[^ \n\t]*
%%

{ w++;c+=yyleng;}

main(int argc,char **argv)


{
if(argc!=2)
{
printf("\nIncorrect usage!!\n");
return 1;
}
yyin=fopen(*(argv+1),"r");
if(!yyin)
{
printf("\nCannot open the file!\n");
return 1;
}
yylex();
printf("\nChar=%d\nSpaces=%d\nWords=%d\nLines=%d\n",c,s,w,l);
}

1b.Comment Lines
%{
int com=0;
%}
%%
\/\*[^*]*\*(\*|([^*/][^*]*\*))*\/ com++;
\/\/[^\n]*[\n] com++;
% fprintf(yyout,"%%");
.|[\n] fprintf(yyout,yytext);
%%
main(int argc,char **argv)
{
if(argc!=3)
{
printf("\nArguments passed in wrong manner\n");
return 1;
}
yyin=fopen(*(argv+1),"r");
yyout=fopen(*(argv+2),"w");
if(!(yyin&&yyout))

{
printf("\nSpecified file cannot be opened!");
return 1;
}
yylex();
printf("\nTotla number of comment lines=%d\n",com);
}

2a.Valid arithmetic expression


%{
#include<string.h>
int valid,i,j,k,temp,temp1,top=1,num[40];
char arr[40][10],st[80],op[40];
%}
%%
[a-zA-Z][a-zA-Z0-9]* {
strcpy(arr[j++],yytext);
if(st[top-1]=='i')
valid=1;
else if(st[top-1]=='o')
st[top-1]='i';
else
st[top++]='i';
}
[0-9]+

{
num[k++]=atoi(yytext);
if(st[top-1]=='i')
valid=1;
else if(st[top-1]=='o')
st[top-1]='i';
else
st[top++]='i';
}

[\-+/*%^&|]
{
op[temp++]=yytext[0];
if(st[top-1]=='i')
st[top-1]='o';
else
valid=1;
}
"("

{
if(st[top-1]=='('||st[top-1]=='$'||st[top-1]=='o')
st[top++]='(';
else
valid=1;
}

")"

{
if(st[top-1]=='(')
top--;
else if(st[top-1]=='i'&&st[top-2]=='(')

{
top-=2;
if(st[top-1]=='o')
st[top-1]='i';
else if(st[top-1]=='i')
valid=1;
else
st[top++]='i';
}
else
valid=1;
}
.

valid=1;

\n return ;
%%
check()
{
if(!(temp|k|j))
return 0;
if(valid)
return 0;
if(top==2&&st[top-1]=='i')
top--;
if(top==1&&st[top-1]=='$')
top--;
if(top==0)
return 1;
return 0;
}
main()
{
st[top-1]='$';
printf("\nEnter the expression\n");
yylex();
if(check())
{
printf("\nValid expression!\n");
if(j>0)
printf("\nIdentifiers present are ");
for(i=0;i<j;i++)
printf("\n%s",arr[i]);
if(k>0)
printf("\nNumbers used are ");
for(i=0;i<k;i++)
printf("\n%d",num[i]);
if(temp>0)
printf("\nOperators present are ");
for(i=0;i<temp;i++)
printf("\n%c",op[i]);
}
else

printf("\nInvalid expression!\n");
}

2b.Simple or complex statement


%{
int valid;
%}
%%
[a-zA-Z][ ](and|but|if|then|else|nevertheless)[ ][a-zA-Z] { valid=1; }
.|[\n] ;
%%
main()
{
printf("\nEnter the text ");
yylex();
if(valid)
{
printf("\nStatement is compound!\n");
}
else
{
printf("\nStatement is simple!\n");
}
}

3.Identifiers(method1)
%{
char ch;
int id;
%}
%%
^[ \t]*(int|float|double|char) {
ch=input();
while(1)
{
if(ch==',')
id++;
else if(ch==';')
{
id++;
break;
}
ch=input();
}
}
.|[\n] ;

%%
int main(int argc,char **argv)
{
if(argc!=2)
{
printf("\nImproper usage!\n");
return 1;
}
yyin=fopen(*(argv+1),"r");
if(!yyin)
{
printf("\nSpecified file cannot be opened!\n");
return 1;
}
yylex();
printf("\nTotal identifiers is %d\n",id);
}

3.Identifiers(method2)
%{
#include<ctype.h>
char ch,arr[20];
int id,i,j;
int test(char *);
%}
%%
^[ \t]*("int "|"float "|"double "|"char ")[ \t]* {
ch=input();
while(1)
{
if(ch=='\n'||ch==0)
break;
if(ch==','||ch==';')
{
arr[i]=0;
i=0;
j=test(arr);
if(j!=-1)
{
id+=j;
printf("\n%s is a %s",arr,j?"identifier":"nonidentifier");
}
if(ch==';')
break;
ch=input();
continue;
}
arr[i++]=ch;
ch=input();

}
}
.|[\n] ;
%%
yywrap()
{
return 1;
}
int rno(char *a,int state)
{
if(*a=='='&&state==0&&!(*a=0))
return 1;
if(isdigit(*a)&&state==1)
state=1;
if(*a==']'&&state==1)
state=0;
if(*a==0&&state==0)
return 1;
if(*a=='['&&state==0)
state=1;
a++;
return rno(a,state);
}
int test(char *a)
{
char *b=a;
int i;
while(*b==' ')
b++;
if(!isalpha(*b))
return 0;
while(*b!=0)
{
if(*b=='='&&!(*b=0))
return 1;
if(*b=='[')
{
i=rno(b++,1);
b--;
if(i==1)
*b=0;
return i;
}
if(!isalnum(*b))
return 0;
b++;
}
return 1;
}
int main(int argc,char **argv)
{

if(argc!=2)
{
printf("\nImproper usage!\n");
return 1;
}
yyin=fopen(*(argv+1),"r");
if(!yyin)
{
printf("\nSpecified file cannot be opened!\n");
return 1;
}
yylex();
printf("\nTotal identifiers is %d\n",id);
}

4a.Valid arithmetic expression(method 1)


LEX part
%{
#include "y.tab.h"
%}
%%
[a-zA-Z_][a-zA-Z_0-9]* return id;
[0-9]+(\.[0-9]*)?
return num;
[+/*]
.

return op;
return yytext[0];

\n
%%

return 0;

YACC part
%{
#include<stdio.h>
int valid=1;
%}
%token num id op
%%
start : id '=' s ';'
s:
id x
| num x
| '-' num x
| '(' s ')' x
;
x:

op s
| '-' s
|
;

%%
int yyerror()
{
valid=0;
printf("\nInvalid expression!\n");
return 0;
}
int main()
{
printf("\nEnter the expression:\n");
yyparse();
if(valid)
{
printf("\nValid expression!\n");
}
}

4a.Valid arithmetic expression(method 2)


LEX part
%{
#include "y.tab.h"
%}
%%
[a-zA-Z_][a-zA-Z_0-9]* return id;
[0-9]+(\.[0-9]*)?
return num;
[+/*]
return op;
.
\n

return yytext[0];
return 0;

%%

YACC part
%{
#include<stdio.h>
int valid=1;
%}
%token num id op
%left '-' op
%%
start : id y '=' s ';'
y : op
| '-'
|
;
s:

x op x
| x '-' x
| '(' s ')'

|x
;
x : id
| '-' num
| num
;
%%
int yyerror()
{
valid=0;
printf("\nInvalid expression!\n");
return 0;
}
int main()
{
printf("\nEnter the expression:\n");
yyparse();
if(valid)
{
printf("\nValid expression!\n");
}
}

4b.Valid variable
LEX part
%{
#include "y.tab.h"
%}
%%
[a-zA-Z] return letter;
[0-9] return digit;
.
return yytext[0];
\n
%%

return 0;

YACC part
%{
#include<stdio.h>
int valid=1;
%}
%token digit letter
%%
start : letter s
s:

letter s
| digit s
|
;

%%
int yyerror()
{
printf("\nIts not a identifier!\n");
valid=0;
return 0;
}
int main()
{
printf("\nEnter a name to tested for identifier ");
yyparse();
if(valid)
{
printf("\nIt is a identifier!\n");
}
}

5a.Evaluate valid arithmetic expression


LEX part
%{
#include "y.tab.h"
int extern yylval;
%}
%%
[0-9]+ { yylval=atoi(yytext); return num; }
. return yytext[0];
\n return 0;
%%

YACC part
%
#include<stdio.h>
int valid=0,temp;
%}
%token num
%left '+''-'
%left '*''/'
%nonassoc UMINUS
%%
expr1 : expr

{ temp=$1; }

expr: expr '+' expr { $$=$1+$3; }


| expr '-' expr { $$=$1-$3; }
| expr '*' expr { $$=$1*$3; }
| expr '/' expr { if($3==0) { valid=1; $$=0; } else { $$=$1/$3; } }
| '(' expr ')' { $$=$2; }
| '-' expr
{ $$=-1*$2; }

| num

{ $$=yylval; }

;
%%
int yyerror()
{
printf("\nInvalid expression!\n");
valid=2;
return 0;
}
int main()
{
printf("\nEnter the expression to be evaluated\n");
yyparse();
if(valid==1)
{
printf("\nDivision by 0!\n");
}
if(valid==0)
{
printf("\nValid expression!\n");
printf("The value evaluated is %d\n",temp);
}
}

5b.Language a^nb^n(n>=0)
LEX part
%{
#include "y.tab.h"
%}
%%
a return A;
b return B;
.|[\n] return 0;
%%
YACC part
%{
#include<stdio.h>
int valid=1;
%}
%token A B
%%
start : A start B
|
;
%%
int yyerror()

{
valid=0;
printf("\nPattern not matched!\n");
return 0;
}
int main()
{
printf("\nEnter the pattern ");
yyparse();
if(valid)
{
printf("\nValid pattern!\n");
}
}

6.Language of type a^nb(n>=10)


LEX part
%{
#include "y.tab.h"
%}
%%
a return A;
b return B;
.|[\n] return 0;
%%

YACC part
%{
#include<stdio.h>
int valid=1;
%}
%token A B
%%
start : A A A A A A A A A A s B
s:

As
|

;
%%
int yyerror()
{
valid=0;
printf("\nPattern not matched!\n");
return 0;
}
int main()
{

printf("\nEnter the pattern ");


yyparse();
if(valid)
{
printf("\nValid pattern!\n");
}
}

You might also like