0% found this document useful (0 votes)
51 views3 pages

Java Variable Declaration Syntax Checker

The document describes a program that uses YACC specifications to implement a syntax analyzer for a compiler to validate variable declarations in Java. The program defines lexical rules in a .l file to tokenize input into tokens like data types, commas, semicolons, identifiers, and numbers. It defines grammar rules in a .y file to validate that a list of variables follows the format of a data type followed by identifiers and optional assignments of numbers, separated by commas. The program outputs "valid" if the input matches the grammar and exits successfully.

Uploaded by

Balbhim Mapare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views3 pages

Java Variable Declaration Syntax Checker

The document describes a program that uses YACC specifications to implement a syntax analyzer for a compiler to validate variable declarations in Java. The program defines lexical rules in a .l file to tokenize input into tokens like data types, commas, semicolons, identifiers, and numbers. It defines grammar rules in a .y file to validate that a list of variables follows the format of a data type followed by identifiers and optional assignments of numbers, separated by commas. The program outputs "valid" if the input matches the grammar and exits successfully.

Uploaded by

Balbhim Mapare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Name: Shubhangi Mapare Roll no: 17112

Div: TE A Subject: SPOS Lab

Assignment No: B4

Problem statement: Write a program using YACC specifications to implement syntax analysis
phase of compiler to validate type and syntax of variable declaration in Java

Input code:

validate.l
%{
#include "y.tab.h"
%}
%%
int|float|char|double return DT;
"," return COMMA;
";" return SC;
[a-zA-Z][a-zA-Z0-9_]* return ID;
[0-9]+ return NUM;
"=" return EQ;
%%
int yywrap()
{
return 1;
}

validate.y
%{
#include<stdio.h>
%}
%token ID DT SC COMMA NUM EQ
%%
start1:DT varlist SC {printf("valid\n");exit(0);};
varlist:ID EQ NUM
|ID
|varlist COMMA ID EQ NUM
|varlist COMMA ID
;
%%
main()
{
printf("Enter the exp: ");
yyparse();
}
int yyerror(char *s)
{
fprintf(stderr,"%s\n",s);
}

Output:

You might also like