YACC Program to Implement Symbol Table [Compiler]

This is the symbol table implementation in yacc using regular expressions,it checks for redeclaration of variables and multiple declarations of variables

Lex Program:
[cc lang=”c”]
%{
#include “y.tab.h”
extern char * yylval;
int x=0;
%}
%%
“int” {x++;return INT;}
“float” {x++;return FLOAT;}
“char” {x++; return CHAR;}
“double” {x++;return DOUBLE;}
[a-z]+ {yylval=yytext;if(x>0) {return ID;} return 0;}
“,” return C;
[n] return NL;
“;” {x–;return SE;}
%%
[/cc]

YACC Program:
[cc lang=”c”]
%{
#include
#include
int err_no=0,fl=0,i=0,j=0,type[100];
char symbol[100][100],temp[100];
%}
%token ID NL SE C INT FLOAT CHAR DOUBLE
%%
START:S1 NL {return;}
;
S1:S NL S1
|
S NL
;
S: INT L1 E
|
FLOAT L2 E
|
CHAR L3 E
|
DOUBLE L4 E
|
INT L1 E S
|
FLOAT L2 E S
|
CHAR L3 E S
|
DOUBLE L4 E S
|
L1:L1 C ID{strcpy(temp,(char *)$3); insert(0);}
|ID{strcpy(temp,(char *)$1); insert(0);}
;
L2:L2 C ID{strcpy(temp,(char *)$3); insert(1);}
|ID{strcpy(temp,(char *)$1); insert(1);}
;
L3:L3 C ID{strcpy(temp,(char *)$3); insert(2);}
|ID{strcpy(temp,(char *)$1); insert(2);}
;
L4:L4 C ID{strcpy(temp,(char *)$3); insert(3);}
|ID{strcpy(temp,(char *)$1); insert(3);}
;
E:SE
;
%%
void yyerror(const char *str) {printf(“error”);}
int yywrap(){return 1;}
main()
{
yyparse();
if(err_no==0)
{
for(j=0;j

You may also like...

4 Responses

  1. raju says:

    display the output

  2. Akshay says:

    Such poor indentation.

  3. amrut says:

    how to run this program

  4. AISHWARYA PAWAR says:

    when yacc program is compiled then there will be an error like
    yacc: 5 reduce/reduce conflicts.

    plz reply.

Leave a Reply

Your email address will not be published. Required fields are marked *