[antlr-interest] Fragments for rewrite rules

Ben Corne ben.corne at gmail.com
Mon May 16 00:43:51 PDT 2011


Hi

Below an example of me trying to use fragment lexer rules to use in rewrite
rules. But when I try this out in the interpreter:

- 'test bar', with a fragment rule AGTEST conflicting with reading 'test',
doesn't parse
- 'foo bar', where no fragmented rule conflicts with reading input, parses

I thought fragment rules only tokenize input when used by other lexer rules?
Besides using literals as tree roots, what other alternatives do I have for
rewriting to abstract grammar elements?

=============================
grammar Foo;

options {
  language = Java;
  output = AST;
  ASTLabelType=CommonTree;
}

program
: 'test' t=test
-> ^(AGTEST $t)
 | 'foo' t=test
-> ^(AGTEST $t)
;

test
: NAM
;

NAM
: LETTER ( LETTER | DIGIT )*
 ;

fragment AGTEST : 'test';

fragment LETTER
: 'a'..'z' | 'A'..'Z'
 ;
fragment DIGIT
: '0'..'9'
 ;
=============================

Regards,
Ben


More information about the antlr-interest mailing list