ads

7. Write a Program to implement calculator using Lex and YACC.


Lex  Program:

%{

#include<stdio.h>

#include "y.tab.h"

extern int yylval;

%}

%%

[0-9]+ {

  yylval=atoi(yytext);

 return NUMBER;

}

[\t] ;

[\n] return 0;

. return yytext[0];

%%

int yywrap()

{

return 1;

}

YACC Program:

%{

#include<stdio.h>

 int flag=0;

%}

 

%token NUMBER

%left '+' '-'

%left '*' '/' '%'

%left '(' ')'

%%

ArithmeticExpression: E{

 printf("\nTHE ARTHMATIC RXPRESSiON Result=%d\n",$$);

 return 0;

 };

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

 |E'-'E {$$=$1-$3;}

 |E'*'E {$$=$1*$3;}

 |E'/'E {$$=$1/$3;}

|E'%'E {$$=$1%$3;}

 |'('E')' {$$=$2;}

 | NUMBER {$$=$1;}

;

%%

void main()

{

      printf("\nEnter Any Arithmetic Expression :\n");

yyparse();

}

void yyerror()

{

 printf("\nEntered arithmetic expression is Invalid\n\n");

 

   flag=1;

}

    OUTPUT:

    

No comments:

Post a Comment