[antlr-interest] Attributes on AST nodes
Priya
priya.uky at gmail.com
Tue Mar 21 08:28:07 PST 2006
Hi Everyone,
I am parsing a C-like syntax with C++ grammar specified in ANTLR.
This is a small snippet from my program .
int:3 a[10]; // a 3 bit integer declaration
int:4 b[10];
int:2 c[5];
The grammar description looks like
type COLON Integer Name LSQB Integer RSQB SEMI;
I modify the default AST structure and now the AST for this
declaration looks like
a----------------b ----------------c
| | |
int---[ int---[ int---[
| | | | | |
3 10 4 10 2 5
for the declaration part
I make the AST nodes a ,b and c to be of type NAMENode , where i
override the Base
AST factory that I already have with the methods of NAMENode and i add
additional methods called get_width() and get_precision() that walk
down the tree to get the values using the getFirstChild() and
getNextSibling() methods . This width and precision information is
recorded in a symbol table ...
when I encounter an expression like the following in my source language ,
c[2]=a[5]+b[7];
I want to know the precision of the variables 'a' and 'b' and perform
a promotion operation in order to add the two variables.
I can go do a symbol table look up to find this out but ,
Is there a way to directly encode the precision and width information as
a.precision , b.precision a.width b.width etc directly in the node itself?
Hope i made my question clear ..
Thanks in advance for your help
Priya
More information about the antlr-interest
mailing list