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

向秦贤 fyaoxy at gmail.com
Mon Aug 27 23:39:47 PDT 2007


Hi,
I make a dicision, strongthen grammar rule, to this case, I changed:
grammar OnGreedy;
options{output=AST; ASTLabelType=CommonTree;}
tokens{PAIR;NAME;VALUE;ARRAY;STRING;STR;}
pairs	:	pair (((';'LINE?)|LINE) pair)* (';' | LINE)?->^(ARRAY pair+);
pair	:	name=stringArray ':' value=stringArray ->^(PAIR ^(NAME $name)
^(VALUE $value));
//stringArray:string (options{greedy=false;}:(',' LINE? string)*) (','
|LINE)?->^(ARRAY string+);
stringArray
	:	strings (',' LINE strings)*->^(ARRAY strings+);
strings	:	string (',' string)*->^(STRING string)+;
string: words->^(STR[$text]);

words: WORD+;
WORD:(~(' '| '\t'|',' |';' | ':' | '\r' | '\n'))+;
WS	:	(' ' | '\t')+ {$channel=HIDDEN;};
LINE	:	('\r'?'\n')+
//	{$channel=HIDDEN;}
	;
test case like this:

name:a,b,c,
a,b,c
a,b,c:value;
this would be 2 name/value pair.

But from deep heart, I still hope can handle this:
name:a,b,c,
a,b,c,
a,b,c:value;
I hope this can be read as 2 name/value pair as well.
so questions is still questions, :-)

在 07-8-28,向秦贤<fyaoxy at gmail.com> 写道:
> Hi,
> If this case just alike that this link stated
> http://www.antlr.org/wiki/display/ANTLR3/Action+execution+during+backtracking
>
> name:a,b,c,   \  ___ I want this 2 lines greedy make a combination.
> a,b,c,            /        because 3rd line match a pair rule.
> a,b,c:value;
> But a,b,c self is a stringArray.
>
> Any idea?
>
>
> 在 07-8-28,向秦贤<fyaoxy at gmail.com> 写道:
> > oops, soooorry again, this test grammar should be this,
> > grammar OnGreedy;
> > options{output=AST; ASTLabelType=CommonTree;}
> > tokens{ARRAY;STRING;STR;}
> > pairs   :       pair (';'LINE? 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;}
> >         ;
> >
> > Sooooorry:)
> > Questions is questions:)
> >
> > 2007/8/28, 向秦贤 <fyaoxy at gmail.com>:
> > > So Sooooorry, this grammar should be this,
> > >
> > > 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;}
> > >         ;
> > >
> > > Questions is yet that questions:)
> > > I mistake at extracting it from full grammar.
> > > Thanks
> > >
> > > 2007/8/28, 向秦贤 <fyaoxy at gmail.com>:
> > > > 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