Venkat,

One way to do what you've mentioned is to create a tree walker which
walks the AST.  Then, within the AST, you'll need to do a couple of
things.

First, you need to populate a symbol table.  That symbol table will
store the different objects/variables that are declared.  In your
example, it would store a and b, their values, if known, and their
types.

Second, you need to do an analysis of the structure while using the
symbol table.  Here's a fragment from one of my tree walking grammars:

plusMinusExpression returns [Type type]
        :       ^(PLUS lhs=baseExpression rhs=baseExpression)
                {
                        typeChecker.assertIsNumericType($lhs.type);
                        typeChecker.assertIsNumericType($rhs.type);
                        typeChecker.assertEqualTypes($lhs.type, $rhs.type);
                        $type = $lhs.type;
                }
                /* ... */
                ;

The above calls on my typeChecker to verify that both types are
numeric (I don't use + for concatenation) and I assert that the types
are equal (since I don't have any automatic casting from int, to
double, etc.). All of my type checking happens in a single pass of a
tree walker, so it's nicely componentized.

I hope that helps.

--
Kaleb Pederson

http://kalebpederson.com
http://twitter.com/kalebpederson


On Fri, Mar 19, 2010 at 9:46 AM, venkat medhaj <[email protected]> wrote:
> Hi,
>
> I am kinda new to antlr and I wonder how to perform symantic analysis using
> the tool. I was able to generate the AST output for a small grammar and now
> I want to check for the type check errors. For ex: if I have a statement
> that looks something like
>
> int a + b , I just have to make sure that both are of type int.
>
> or if I define a twice in my input, then thats a redefine error. Can some
> one give me some insight into this ?
>
> Thanks,
> -Venkat.
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: 
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.

Reply via email to