You are on page 1of 6

/* Write a LEX program to recognize valid arithmetic expression.

Identifiers in the expression could


//be only integers and operators could be + and *. Count the identifiers & operators present and
print them separately.*/

%{
#include<stdio.h>
int v=0,op=0,id=0;
%}
%%
[0-9]+ {id++; printf("\nIdentifer: ");ECHO;}
[\+ \*] {op++; printf("\nOperator: ");ECHO;}
"(" {v++;}
")" {v--;}
.|\n {;}
%%
int main()
{
printf("Enter the expression: ");
yylex();
if((op+1)==id && v==0)
printf("\nExpression is valid.\n");
else
printf("\nExpression is invalid.\n");
printf("\nNo of identifers=%d",id);
printf("\nNo of operators=%d",op);
}

/*1b Write YACC program to evaluate arithmetic expression involving operators: +, -,*, and /
Lex part*/
%{
#include "y.tab.h"
extern yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext); return num;}
[\+\-\*\/] {return yytext[0];}
[)] {return yytext[0];}
[(] {return yytext[0];}
. {;}
\n {return 0;}
%%

%{
#include<stdio.h>
#include<stdlib.h>
%}
%token num
%left '+''-'
%left '*''/'
%%
input:exp {printf("%d \n",$$);exit(0);}
exp:exp '+' exp {$$=$1+$3;}
|exp '-' exp {$$=$1-$3;}
|exp '*' exp {$$=$1*$3;}
|exp '/' exp
{if($3==0) {printf("Divide by zero \n");exit(0);}
else $$=$1/$3;}
|'(' exp ')' {$$=$2;}
|num{$$=$1;}
;
%%
int yyerror()
{
printf("error");
exit(0);
}
int main()
{
printf("Enter an expression \n");
yyparse();
}

2.L

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

2.Y

%{
#include<stdio.h>
#include<stdlib.h>
%}
%token A B
%%
input:S'\n' {printf("Valid input for the given grammar.\n");exit(0);}
S:A S1 B| B;
S1:A S1 | ;
%%
int main()
{
printf("Enter input string: \n");
yyparse();
}
int yyerror()
{
printf("Invalid input for the defined grammar.\n");
exit(0);
}

%{
#include<stdlib.h>
int C_count=0;
%}
%%
"/*"[^*/]*"*/" {C_count++;}
"//".* {C_count++;}
%%
int main(int argc,char **argv)
{
FILE *f1,*f2;
if(argc>1)
{
f1=fopen(argv[1],"r");
if(!f1)
{
printf("File open error.");
exit(1);
}
yyin=f1;
f2=fopen(argv[2],"w");
if(!f2)
{
printf("File open error.");
exit(1);
}
yyout=f2;
yylex();
printf("number of comment line are: %d\n",C_count);
}
return 0;
}
/* Write a LEX program to recognize valid arithmetic expression. Identifiers in the expression
could //be only integers and operators could be + and *. Count the identifiers & operators
present and print them separately.*/

%{
#include<stdio.h>
int v=0,op=0,id=0;
%}
%%
[0-9]+ {id++; printf("\nIdentifer: ");ECHO;}
[\+ \*] {op++; printf("\nOperator: ");ECHO;}
"(" {v++;}
")" {v--;}
.|\n {;}
%%
int main()
{
printf("Enter the expression: ");
yylex();
if((op+1)==id && v==0)
printf("\nExpression is valid.\n");
else
printf("\nExpression is invalid.\n");
printf("\nNo of identifers=%d",id);
printf("\nNo of operators=%d",op);
}

/*1b.l Write YACC program to evaluate arithmetic expression involving operators: +, -,*,
and /
Lex part*/
%{
#include "y.tab.h"
extern yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext); return num;}
[\+\-\*\/] {return yytext[0];}
[)] {return yytext[0];}
[(] {return yytext[0];}
. {;}
\n {return 0;}
%%

1B.y

%{
#include<stdio.h>
#include<stdlib.h>
%}
%token num
%left '+''-'
%left '*''/'
%%
input:exp {printf("%d \n",$$);exit(0);}
exp:exp '+' exp {$$=$1+$3;}
|exp '-' exp {$$=$1-$3;}
|exp '*' exp {$$=$1*$3;}
|exp '/' exp
{if($3==0) {printf("Divide by zero \n");exit(0);}
else $$=$1/$3;}
|'(' exp ')' {$$=$2;}
|num{$$=$1;}
;
%%
int yyerror()
{
printf("error");
exit(0);
}
int main()
{
printf("Enter an expression \n");
yyparse();
}

Programme 2:
gedit 2.l

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

gedit 2.y

%{
#include<stdio.h>
#include<stdlib.h>
%}
%token A B
%%
input:S'\n' {printf("Valid input for the given grammar.\n");exit(0);}
S:A S1 B| B;
S1:A S1 | ;
%%
int main()
{
printf("Enter input string: \n");
yyparse();
}
int yyerror()
{
printf("Invalid input for the defined grammar.\n");
exit(0);
}

%{
#include<stdlib.h>
int C_count=0;
%}
%%
"/*"[^*/]*"*/" {C_count++;}
"//".* {C_count++;}
%%
int main(int argc,char **argv)
{
FILE *f1,*f2;
if(argc>1)
{
f1=fopen(argv[1],"r");
if(!f1)
{
printf("File open error.");
exit(1);
}
yyin=f1;
f2=fopen(argv[2],"w");
if(!f2)
{
printf("File open error.");
exit(1);
}
yyout=f2;
yylex();
printf("number of comment line are: %d\n",C_count);
}
return 0;
}

You might also like