Implement Syntax analyzer to validate type and syntax of variable declaration using YACC
Problem Statement:-
Write a program using YACC specifications to implement the syntax analysis phase of the compiler to validate type and syntax of variable declaration in Java
Input:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int a,b,c,d=10; |
Source Code:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#include <stdio.h> | |
#include "y.tab.h" | |
%} | |
DIGIT [0-9] | |
REAL {DIGIT}+[.]{DIGIT}* | |
LETTER [A-Za-z] | |
ASSIGN = | |
%% | |
[\t ] ; | |
int {printf("%s\t==> DataType\n",yytext);return (INT);} | |
float {printf("%s\t==> DataType\n",yytext);return (FLOAT);} | |
char {printf("%s\t==> DataType\n",yytext);return (CHAR);} | |
boolean {printf("%s\t==> DataType\n",yytext);return (BL);} | |
true|false { printf("%s\t==> BOOLEAN VAL\n",yytext);return BLVAL;} | |
['][^\t\n]['] { printf("%s\t==> CHAR VALUE\n",yytext);return CHVAL;} | |
[a-zA-z]+[a-zA-z0-9_]* {printf("%s\t==> ID\n",yytext);return ID;} | |
{REAL} { printf("%s\t==> REAL NUMBER\n",yytext);return REAL;} | |
{DIGIT}+ { printf("%s\t==> INT NUMBER\n",yytext);return NUM;} | |
"," {printf("%s\t==> COMMA\n",yytext);return COMMA;} | |
";" {printf("%s\t==> SC\n",yytext);;return SC;} | |
{ASSIGN} {printf("%s\t==> ASSIGN\n",yytext);return AS;} | |
\n return NL; | |
. ; | |
%% | |
int yywrap() | |
{ | |
return 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#include<stdio.h> | |
void yyerror(char*); | |
int yylex(); | |
//FILE* yyin; | |
%} | |
%token ID SC INT CHAR FLOAT BL BLVAL CHVAL REAL AS NUM COMMA NL | |
%% | |
s: type1|type2|type3|type4 | |
; | |
type1:INT varlist SC NL { printf("valid INT Variable declaration"); return 0;} | |
/// for "int a" Test case(without SC ;) NL is added at end otherwise it waits for input | |
; | |
type2:FLOAT varlist2 SC NL{ printf("valid FLOAT Variable declaration");return 0;} | |
; | |
type3:CHAR varlist3 SC NL{ printf("valid CHAR Variable declaration");return 0;} | |
; | |
type4:BL varlist4 SC NL{ printf("valid BOOLEAN Variable declaration");return 0;} | |
; | |
varlist: ID | ID COMMA varlist | ID AS NUM |ID AS NUM COMMA varlist | //THIS IS FOR EPSILON CASE (EMPTY) | |
; | |
varlist2: ID | ID COMMA varlist2 | ID AS REAL |ID AS REAL COMMA varlist2 | | |
; | |
varlist3: ID | ID COMMA varlist3 | ID AS CHVAL | ID AS CHVAL COMMA varlist3 | | |
; | |
varlist4: ID | ID COMMA varlist4 | ID AS BLVAL | AS BLVAL COMMA varlist4 | | |
; | |
%% | |
void yyerror(char *s ) | |
{ | |
fprintf(stderr, "ERROR: %s\n",s); | |
} | |
int main() | |
{ | |
//yyin=fopen("input.txt","r"); | |
yyparse(); | |
//fclose(yyin); | |
return 0; | |
} |
No comments: