Thursday 30 April 2015

Filled Under:

How To Program Compiler Design Symbol Table Generator in C

Post By - Tanmay | 4/30/2015 10:32:00 am
Symbol table is a data structure used by a language translator such as a compiler or interpreter, where each identifier in a program's source code is associated with information relating to its declaration or appearance in the source, such as its type, scope level and sometimes its location.

--- Source : Wikipedia 

In this tutorial we will learn to build symbol table generator based on identifiers and keywords.

Test Case : Suppose in a program you encounter with some data types as

int a; float b; char c; double g,h;

Symbol table will be as follow :

INTEGER : a
FLOAT : b
CHAR : c
DOUBLE : g
DOUBLE : h

Problem :

YYSTYPE in y.tab.c and y.tab.h is by default INTEGER type, since we are using extern char *yylval it will say a warning and gives an error "error: conflicting types for ‘yylval’ In file included" along with it. To resolve it
just open your generated y.tab.c and y.tab.h and steps :

1: Search for typedef int YYSTYPE
2: Replace int with char*

Perform this in both of the file i.e. y.tab.c and y.tab.h
Program :

LEX File : Symbol.l
1:  %{  
2:  #include"y.tab.h"  
3:  extern char *yylval;  
4:  int x=0;  
5:  %}  
6:  %%  
7:  "int" {x++;return INT;}  
8:  "float" {x++;return FLOAT;}  
9:  "double" {x++;return DOUBLE;}  
10:  "char" {x++;return CHAR;;}  
11:  [a-z]+ {yylval=yytext; if(x>0)return ID; return O;}  
12:  [\n] {return NL;}  
13:  "," {return C;}  
14:  ";" {x--;return SE;}  
15:  . {}  
16:  %%  

YACC File : Symbol.y

1:  %{  
2:  #include<stdio.h>  
3:  #include<string.h>  
4:  int fl=0,i=0,type[100],j=0,error_flag=0;  
5:  char symbol[100][100],temp[100];  
6:  %}  
7:  %token INT FLOAT C DOUBLE CHAR ID NL SE O  
8:  %%  
9:  START:S1 NL {return;}  
10:  ;  
11:  S1:S NL S1  
12:  |S NL  
13:  ;  
14:  S:INT L1 E  
15:  |FLOAT L2 E  
16:  |DOUBLE L3 E  
17:  |CHAR L4 E  
18:  |INT L1 E S  
19:  |FLOAT L2 E S  
20:  |DOUBLE L3 E S  
21:  |CHAR L4 E S  
22:  |O  
23:  ;  
24:  L1:L1 C ID {strcpy(temp,(char *)$3);insert(0);}  
25:  |ID {strcpy(temp,(char *)$1);insert(0);}  
26:  ;  
27:  L2:L2 C ID {strcpy(temp,(char *)$3);insert(1);}  
28:  |ID {strcpy(temp,(char *)$1);insert(1);}  
29:  ;  
30:  L3:L3 C ID {strcpy(temp,(char *)$3);insert(2);}  
31:  |ID {strcpy(temp,(char *)$1);insert(2);}  
32:  ;  
33:  L4:L4 C ID {strcpy(temp,(char *)$3);insert(3);}  
34:  |ID {strcpy(temp,(char *)$1);insert(3);}  
35:  ;  
36:  E:SE  
37:  ;  
38:  %%  
39:  int main()  
40:  {  
41:       yyparse();  
42:       if(error_flag==0)  
43:       for(j=0;j<i;j++)  
44:       {  
45:            if(type[j]==0)  
46:                 printf(" INT - ");  
47:            if(type[j]==1)  
48:                 printf(" FLOAT - ");  
49:            if(type[j]==2)  
50:                 printf(" DOUBLE - ");  
51:            if(type[j]==3)  
52:                 printf(" CHAR - ");  
53:            printf(" %s\n",symbol[j]);  
54:       }  
55:  }  
56:  int yyerror(char *ch)  
57:  {  
58:       return 1;  
59:  }  
60:  int yywrap(){  
61:       return 1;  
62:  }  
63:  int insert(int type1)  
64:  {  
65:       fl=0;  
66:       for(j=0;j<fl;j++)  
67:       if(strcmp(temp,symbol[j])==0)  
68:       {  
69:            if(type[i]==type1)  
70:                 printf("REDECLARATION OF %s\n",temp);  
71:            else  
72:            {  
73:                 printf("MULTIPLE DECLARATION OF %s\n",temp);  
74:                 error_flag=1;  
75:            }  
76:            fl=1;  
77:       }  
78:       if(fl==0)  
79:       {  
80:            strcpy(symbol[i],temp);  
81:            type[i]=type1;  
82:            i++;  
83:       }  
84:  }  

Output :

0 comments:

Post a Comment