[antlr-interest] greedy options on parser rule or look ahead sematics predicate

向秦贤 fyaoxy at gmail.com
Mon Aug 27 20:01:53 PDT 2007


Hi,
I have a grammar with conflict semantics, I cannot handle this right.
here is grammar.

grammar OnGreedy;
options{output=AST; ASTLabelType=CommonTree;}
tokens{ARRAY;STRING;STR;}
pairs	:	pair (':' pair)* (';' | LINE);
pair	:	stringArray ':' stringArray;
stringArray:string (options{greedy=false;}:',' LINE? string)* ','
LINE?->^(ARRAY string+);

string: words->^(STR[$text]);
words: WORD+;
WORD:(~(' '| '\t'|',' |';' | ':' | '\r' | '\n'))+;
WS	:	(' ' | '\t')+ {$channel=HIDDEN;};
LINE	:	('\r'?'\n')+
//	{$channel=HIDDEN;}
	;

Supported input:
name:a,b,c,
a,b,c,
a,b,c:value;

As you see, I hope this parsed result is :
name:a,b,c,a,b,c;
a,b,c:value;
Yes, I hope pair rule's value part (stringArray) greedy, and make
closest rule match next to a new pair.

My questions are:
1, I hope greedy options can work fine like in lexer. seems I cannot
get right result.
2, I wanna use semantics predicate like some syntactics predicate rewrite rule,
(stringArray !pair)=>stringArray
3, maybe in code do this work, but there need handle newline, and
match follow rule, I not sure if it is acceptable or effective.

There are some good ways to handle this case?
Thanks

-- 
致敬
向秦贤


More information about the antlr-interest mailing list