Decade Moon
Decade Moon

Reputation: 34286

ANTLR: How to preserve data in tree between multiple tree grammars

I want to create a very simple translator that translates some source code defined by the language into assembly. I have already created the lexer and parser which produces an AST. I have created a tree grammar which traverses the tree and keeps track of things like declared variables using dynamic scopes.

The problem is I can't simply create a translator using one tree grammar because I need to traverse the tree in multiple passes. The first pass will be semantic analysis, and the second pass will be translation into assembly. Each pass will be a different tree grammar.

My question is, how do I preserve node-specific information from the semantic analysis pass in the tree so that I can access it for the translation pass? How do I annotate the tree? Is there a way I can assign information to the individual nodes in the tree? Do I need to create a custom tree node class for this?

Upvotes: 1

Views: 239

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170178

Do I need to create a custom tree node class for this?

Yes.

See:

Upvotes: 1

Related Questions