Ex. No: 4 B
Date:
4B Program
to recognize a valid variable which starts with a letterfollowed by any
number of letters or digits
By Using YACC
AIM:
To write a yacc program to check valid variable followed by letter or digits by using YACC
ALGORITHM:
Step-1:Reading an expression
Step-2: Checking the validating of the given expression according to the rule using yacc.
Step-3:Using expression rule print the result of the given values
PROGRAM
vid.l
%{
#include "y.tab.h"
%}
%%
[a-zA-z_] {return ALPHA;}
[0-9]+ {return NUMBER;}
"\n" { return ENTER;}
. {return ER;}
%%
yywrap()
{}
vid.y
%{
#include <stdio.h>
#include<stdlib.h>
%}
%token ALPHA NUMBER ENTER ER
%%
var:v ENTER {printf("Valid Variable\n");exit(0);}
v:ALPHA exp1
exp1:ALPHA exp1
|NUMBER exp1
| ;
%%
yyerror()
{
printf("Invalid Variable\n");
}
main()
{
printf("Enter the expression:");
yyparse();
}
OUTPUT:
D:\MOHANRAJ\CS6612 COMPILER LAB\lex_yacc\YACC\CD4B>lex vid.l
D: \MOHANRAJ \CS6612 COMPILER LAB\lex_yacc\YACC\CD4B>yacc -d vid.y
D: \MOHANRAJ \CS6612 COMPILER LAB\lex_yacc\YACC\CD4B>cc lex.yy.c y.tab.c
D: \MOHANRAJ \CS6612 COMPILER LAB\lex_yacc\YACC\CD4B>a.exe
Enter the expression:a1111
Valid Variable
D: \MOHANRAJ \CS6612 COMPILER LAB\lex_yacc\YACC\CD4B>a.exe
Enter the expression:12asda
Invalid Variable
D: \MOHANRAJ \CS6612 COMPILER LAB\lex_yacc\YACC\CD4B>a.exe
Enter the expression:abc1234df
Valid Variable
RESULT:
Thus the program for checking letter followed by letter or digits were done
No comments:
Post a Comment