[antlr-interest] Unexpected text

Graham Mer gd.antlr at gmail.com
Wed Sep 21 21:41:33 PDT 2011


"Hi everybody!"
  - Dr. Nick.

I have a larger grammar, a tiny portion of which is attached below.
Everything works as expected, except for one thing. In the tree
grammar, note the following rule:

decl: ID typeSpec { System.out.println( "decl id=" + $ID + ";type=" +
$typeSpec.text );}

Given the following input:
  Dim foo as String
  Dim bar as Int

I expect it to print:
  decl id=foo;type=String
  decl id=bar;type=Int

But instead I get:
  decl id=foo;type=as String
  decl id=bar;type=as Int

The string trees all look as expected, like "(Dim foo String) (Dim bar
Int)", but I get the extra "as" in the type node, even though I
exclude the "AS" node when building the tree in the following rule:

asType: AS! type;


I get the same results when the parser is generated by ANTLR 3.3 and
3.4. What am I doing wrong?


Here are the grammars:

grammar test;

options{ output=AST; }

start	:	varDef+ EOF;

varDef	:	DIM^ ID asType;

asType	:	AS! type;

type	:	STRING_T | INT_T;


AS	:	('A'|'a')('S'|'s');

DIM	:	('D'|'d')('I'|'i')('M'|'m');

STRING_T:	('S'|'s')('T'|'t')('R'|'r')('I'|'i')('N'|'n')('G'|'g');

INT_T	:	('I'|'i')('N'|'n')('T'|'t');

ID  :	('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;



tree grammar testTree;

options
{
	tokenVocab=test;
	ASTLabelType=CommonTree;
	output=AST;
}

start	:	statement+
	;

statement
	:	^(DIM decl)
	;
	
decl	:	ID typeSpec { System.out.println( "decl id=" + $ID + ";type=" +
$typeSpec.text );}
	;
	
typeSpec:	STRING_T | INT_T
	;


More information about the antlr-interest mailing list