You are on page 1of 2

2CEIT701: COMPILER DESIGN PRACTICAL-6

Practical-6

AIM: Write a program to validate the variable declaration statement using Lex and
YACC.

Code:
pr6.lex
%{
#include <stdio.h>
#include "y.tab.h"
%}
%%
"int"|"float"|"char"|"String" return BUILTIN;
"new" return NEW;
"[" return OPEN_SQ;
"]" return CLOSE_SQ;
"=" return EQ;
"," return COMMA;
";" return SC;
[_a-zA-Z][_a-zA-Z0-9]* return ID;
[0-9]+ return DIGIT;
\n return 0;
%%

pr6.yacc
%{
#include <stdio.h>
#include <ctype.h>
#include <string.h>

void yyerror(char *);


%}
%token BUILTIN ID OPEN_SQ CLOSE_SQ EQ NEW SC COMMA DIGIT
%%
start: BUILTIN varlist SC {printf("Valid Declaration\n");}
| BUILTIN OPEN_SQ CLOSE_SQ ID EQ NEW BUILTIN OPEN_SQ DIGIT
CLOSE_SQ SC {printf("Valid Declaration\n");}
| varlist
;
varlist: varlist COMMA ID | ID
;
%%
int yywrap()
{
return 1;
}
int main()
21012022019 Page 18 of 19 JOSHI KARAN: 7B-5
2CEIT701: COMPILER DESIGN PRACTICAL-6

{
printf("\nEnter Declaration Statement: ");
yyparse();
}
void yyerror(char *s) {
fprintf(stderr, "%s\n", s);
}

Output:

21012022019 Page 19 of 19 JOSHI KARAN: 7B-5

You might also like