About

Kannur University btech CSE study materials, question papers, syllabus . . .

Saturday, June 8, 2013

S6 CS COMPILER LAB : ARITHMETIC EXPRESSION VALIDATION

/*  ARITHMETIC EXPRESSION VALIDATION  */


Yacc program

%{
#include<stdio.h>
#include<stdlib.h>
%}
%token num alpha
%left '+''-'
%left '*''/'
%right '^'
%nonassoc UMINUS
%%
S:E NL  { printf("\nThe given expression is valid\n");exit(0);}
E:E'+'E
 |E'-'E
 |E'*'E
 |E'/'E
 |'-'E %prec UMINUS
 |'('E')'
 |E'^'E
 |num
 |alpha
 ;
%%
#include"lex.yy.c"
int main()
{
yyparse();
yylex();
return 0;
}
yyerror(char *s)
{
 printf("\nThe given expression is invalid\n");
}

Lex program

%{
#include"y.tab.h"
%}
%%
[0-9]+    {return num;}
[a-z]+    {return alpha;}
[ \t]      ;
[\n]      {return NL;}
.         {return yytext[0];}
%%






 OUTPUT

-8+4
The given expression is valid


(4*2

The given expression is invalid

0 comments:

Post a Comment