[antlr-interest] Problem with literals

Peter Kronenberg PKronenberg at technicacorp.com
Tue Apr 5 07:06:10 PDT 2005


I'm obviously having a problem understanding how antlr handles literals.
Here is a small example.  This grammar accepts either
"abc = def" or "abc foo def"

If I uncomment the line at the end in the tree walker, it doesn't
compile because antlr puts 2 entries for LITERAL_foo in the token types
table.  How do I indicate the 'foo' token when walking the tree?

header {
}
class TestParser extends Parser;
	options { k=1; 
	          buildAST=true; 
	}
	
      	testExpr: IDENT (EQ^ | "foo"^) IDENT SEMI!;
        
class TestLexer extends Lexer;
	options { k=2;
     	      testLiterals=true;
     	      charVocabulary = '\3'..'\377';
	}

		EQ : '=';
        SEMI  : ';' ;
        
        IDENT : ('a'..'z' | 'A'..'Z') ('a'..'z' | 'A'..'Z' | '0'..'9' |
'_' | '$')*;

        WS    : (' ' | '\t' | '\n' | '\r') {$setType(Token.SKIP);} ;

class TestTreeWalker extends TreeParser;
	
	  expr returns [String result=""]
	  {String s1, s2;}
          : #(EQ  s1=expr s2=expr)   { result = s1 + " eq " + s2;
System.out.println(result); }
          /*
          | #(LITERAL_foo  s1=expr s2=expr)   { result = s1 + " foo " +
s2; System.out.println(result); }
          */
          | i:IDENT {result=i.getText();}
          ;     


Peter Kronenberg
Software Engineer
(703) 885-1222
pkronenberg at technicacorp.com


The information contained in this transmission may contain privileged and confidential information. It is intended only for the use of the person(s) named above. If you are not the intended recipient, you are hereby notified that any review, dissemination, distribution or duplication of this communication is strictly prohibited. If you are not the intended recipient, please contact the sender by reply e-mail and destroy all copies of the original message. Technica Corporation does not represent this e-mail to be free from any virus, fault or defect and it is therefore the responsibility of the recipient to first scan it for viruses, faults and defects. To reply to our e-mail administrator directly, please send an e-mail to postmaster at technicacorp.com. Thank you.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20050405/89d73e13/attachment.html


More information about the antlr-interest mailing list