Example of Symbol table creation using Visitor With ANTLR4

2,410 views
Skip to first unread message

UJWAL KUMAR Singh

unread,
Nov 15, 2016, 5:30:44 AM11/15/16
to antlr-discussion
I am new to ANTLR4. Currently, I am working on a small translator project for C language. My aim is to translate a simple and small C program into other programming languages. I am able to parse the C grammar file using ANTLR4. Using Visitor I am able to print each and every part of input C file. Now, I need to create a Symbol table. For example : -  If my input C file is : -  
int main() {  
int a=1;  
if(a==1)  
printf(" a = %d",a);  
return 0; 
}
Using Visitor, I am able to print on screen like typespecifier -> int, Identifier -> a, Constant -> 1, etc.

Now, I want to create a symbol table to store all these symbols with information like their scope, type, value etc. 

Can anyone suggest me some examples. Or at least guide me how to create a Symbol table in ANTLR4 using Visitor.
 
Any help will be really appreciated.

Thanks in Advance.

Mike Lischke

unread,
Nov 15, 2016, 5:49:15 AM11/15/16
to antlr-di...@googlegroups.com

I am new to ANTLR4. Currently, I am working on a small translator project for C language. My aim is to translate a simple and small C program into other programming languages. I am able to parse the C grammar file using ANTLR4. Using Visitor I am able to print each and every part of input C file. Now, I need to create a Symbol table. For example : -  If my input C file is : -  
int main() {  
int a=1;  
if(a==1)  
printf(" a = %d",a);  
return 0; 
}
Using Visitor, I am able to print on screen like typespecifier -> int, Identifier -> a, Constant -> 1, etc.

Now that you have all the info you only need to settle on an infrastructure to store it. Important here is to keep the context (scope) where you found the symbol.

Terence also wrote a generic symbol table implementation: https://github.com/antlr/symtab, which might give you some ideas. There the scopes are handled by individual objects which are linked to each other. Another approach could be to use a rule index list (kinda call stack) which gives you a much more compact scope vector. Depends on your needs of course (e.g. if you just need a lookup or if you need additional information).


Reply all
Reply to author
Forward
0 new messages