Sunday, 16 November 2014

YACC PROGRAM to recognize valid arithmetic expression that use operators + - * /.



Program to recognize valid arithmetic expression that use operators + - * /.


4a.l file ->
%{
#include "y.tab.h"
%}
%%
[a-zA-Z] {return ID;}
[0-9]+ {return NUMBER;}
[ \t] {;}
\n {return 0;}
. { return yytext[0];}
%%       
 


4a.y file
%{
#include<stdio.h>
%}
%token ID NUMBER
%left '+' '-'
%left '*' '/'
%%
stmt:expr
    ;
expr: expr '+' expr
    | expr '-' expr
    | expr '*' expr
    | expr '/' expr
    | '(' expr ')'
    | NUMBER
    | ID
    ;
%%
void main()
{
printf("enter expr : \n");
yyparse();
printf("valid exp");
exit(0);
}
void yyerror()
{
  printf("invalid exp");
exit(0);
}


1 comment:

  1. give me sample output program for that above program...... its an urgent .....

    ReplyDelete

Total Pageviews