[antlr-interest] Matching multiple occurrences of quoted text joined by 'and' (i.e. "a" and "b" and "c")

Colin Yates colin.yates at gmail.com
Wed Nov 3 12:16:50 PDT 2010


Hi,

I need to accept statements like:

  "a" and "b" and "c"

and retrieve the quoted strings (i.e. "a", "b", and "c").

When it didn't work as expected I decided to boil it down to its simplest
components.  I would expect the following to match, but it doesn't:

ruleA : QUOTED_TEXT 'and' QUOTED_TEXT 'and' QUOTED_TEXT;

QUOTED_TEXT : '\"' ( options {greedy=false;} : .)* '\"'
{ setText(getText().substring(1, getText().length() - 1)); } ;

With the text '"a" and "b" and "c"' (without the single quotes) it produces
the following output:

line 1:8 mismatched character '"' expecting 'w'
line 1:19 required (...)+ loop did not match anything at character '<EOF>'
line 1:9 mismatched input 'b' expecting 'and'

Changing ruleA so one of the quoted strings is replaced by the QUOTED_TEXT
lexical removes any errors.  For example, the following ruleA definition
works:

ruleA: '"a" and' QUOTED_TEXT 'and "c"';

So, my question is, what have I missed?  (This is using IDEA 8 with the
latest antlr works plugin by the way).

Many thanks,

Col


More information about the antlr-interest mailing list