[antlr-interest] Simple way to get the text that matched

Rick Morgan r.morgan at verizonbusiness.com
Thu Nov 2 07:01:57 PST 2006


I'm getting closer in that I get declarations for my t1 and t2 element names
now, but only in certain cases; I don't get any declaration for something
like t1:parserRule, but I get one for t1:LEXER_RULE.  So that's one issue,
something I can probably work around if I focus on the lexer rules doing
more work.

A bigger issue for me right now is the generated code for the actions. When
I put the action {initEntity( #nm1.getText());} in my rule, that's exactly
what I get in the output, including the hash mark.  That's not valid C++ in
my book but I don't have ANTLR headers on the target machine yet so can't
compile it there, so I thought I'll switch to Java production and see what I
get.  Well, they're still there and the Java compiler doesn't like them one
bit.  I have to guess that (being new with ANTLR) I've totally overlooked
some step, like some post-processing step that turns those hash marks into
valid AST node references like the wiki says they'll be.  How do I get that
to happen?

Here's a portion of the generated Java (and the C++ looks about the same) in
case there's doubt about what I'm saying (generated by 2.7.7, released
yesterday):

	public final void classSpec() throws RecognitionException,
TokenStreamException {
		
		Token  nm1 = null;
		Token  nm2 = null;
		
		try {      // for error handling
			nm1 = LT(1);
			match(NAME);
			initEntity(#nm1.getText());
			{
			_loop37:

thanks Bryan for your reply, that got me closer,
Rick





More information about the antlr-interest mailing list