[antlr-interest] sematic analysis ?
Kaleb Pederson
kaleb.pederson at gmail.com
Fri Mar 19 10:51:50 PDT 2010
On Fri, Mar 19, 2010 at 10:37 AM, Kaleb Pederson
<kaleb.pederson at gmail.com> wrote:
> 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;
> }
> /* ... */
> ;
I realized that doesn't fully explain everything. In the various
expressions, I resolve any necessary symbols and then resolve their
types by:
* using the symbol table if it's a variable, OR
* using the type of the literal
I then return the type of the expression so I can verify that the
types match throughout the entire expression.
--
Kaleb Pederson
http://kalebpederson.com
http://twitter.com/kalebpederson
More information about the antlr-interest
mailing list