PROGRAM 4:
Program to recognize a valid variable which starts with a letter followed by any number of letters or digits.
YACC PART
Program to recognize a valid variable which starts with a letter followed by any number of letters or digits.
ALGORITHM:
1. Start the program.
2. Read the input.
3. Check whether the given input is valid according to the
rule for variable using yacc.
4. Print the result after the validation check
5. Stop the program.
PROGRAM:
LEX PART
%{
#include
"y.tab.h"
%}
%%
[a-zA-Z_][a-zA-Z_0-9]* return letter;
[0-9]
return digit;
.
return yytext[0];
\n return 0;
%%
int yywrap()
{
return 1;
}
%{
#include<stdio.h>
int valid=1;
%}
%token digit letter
%%
start : letter s
s : letter s
| digit s
|
;
%%
int yyerror()
{
printf("\n Its
not a variable !\n");
valid=0;
return 0;
}
int main()
{
printf("\n Enter
the input: ");
yyparse();
if(valid)
{
printf("\n It is a variable !\n");
}
}
0 Comments
Post a Comment