3

I have 2 problems:

In my ANTLR parser, I have this rewrite rule:

msg: msg_content (COMMA msg_content)* -> ^(MSG_CTS msg_content+);

In my tree grammar, how can I make use of the collected msg_content tokens? $msg_content.text is returning a null exception.


More generally, can you please provide some guide to me, as to how I can use my generated AST tree? I basically want to walk over the nodes and create Java classes for the different things for e.g.

I have this simple tree that gets printed:

(MSG (AGENTS A B) (MSG_CTS x y))

I'd like to have some Java class "Message" with fields for "Agents" containing A, B and some Content field that will hold X, Y.

I've gone through the ANTLR definitive guide, but haven't sen references on how to use the combined tokens, or even, how to navigate down the tree like I want. It's as if every ANTLR tutorial out there is about expression evaluators!

I've seen: "Interfacing AST with Java" and "Expression evaluator" from the ANTLR online manual, but I don't quite get how to apply those to my problem. If you can provide a simple example, it'd be very helpful!

Please help... Thank you!

Inf.S
  • 471
  • 1
  • 5
  • 14

1 Answers1

1

$msg_content.text is returning a null exception

That is impossible to comment on without seeing all the involved rules and code. Can you edit your question and include a self-contained example I, or someone else, can run that reproduces the error/exception?

(MSG (AGENTS A B) (MSG_CTS x y))

I'd like to have some Java class "Message" with fields for "Agents" containing A, B and some Content field that will hold X, Y.

Have a look at this list of tutorials: https://stackoverflow.com/questions/278480/antlr-tutorials, not all are about expression evaluators. My tutorial demonstrates how to use custom Node classes in the tree walker.

Also see this Q&A that also shows how to use custom node-classes in the tree walker.

Community
  • 1
  • 1
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • I read through your tutorial Bart Kiers, but it's still an expression evaluator. I'd like to know how to solve my particular problem. E.g., I want to create an "Agent" object, append it to a "Message" object. Then, move down and create a "Content" object, again append it to that same Message etc... How do I do this? – Inf.S Jul 12 '12 at 18:16
  • @Inf.S, no, it is not an expression evaluator: it is a small language with `for`-, `while` and `if` statements, functions, etc. Like I explained: in the tree walker, I show exactly how to create custom node classes, which is what you seem to be after. – Bart Kiers Jul 12 '12 at 18:20
  • let me go back and read through the tutorial again, then read on Scopes. I'll get back to you in this Q&A if I'm still stuck. Thanks! – Inf.S Jul 12 '12 at 19:02