[antlr-interest] Question on aborting rule based on pattern

Thomas Brandon tbrandonau at gmail.com
Mon Jul 24 01:36:56 PDT 2006


You might like to have a look at some information on the difference
between LL and LR grammars. LR grammars make their decisions on the
right edge of productions, shifting tokens until they can reduce.
ANTLR on the other hand generates LL parsers. These decide which
alternative they will take on the left-edge instead, based on the
first k tokens of the input. To get around the limitations of a small
fixed lookahead you can use syntactic predicates and semantic
predicates. Check the docs.

Is "XXX" a keyword in your language? i.e. Is "XXX: x:y" a valid ddd? I
think you need to look at literal testing in the lexer. Given that
example either "XXX" will be matched as an ATOM in your lexer and ccc
will not work or "XXX" is a literal in your lexer and it will not
match as an ATOM in ddd. Or you should get an ambiguous lexer. Have
you got your token imports\exports set up correctly for literals in
the parser? Check the docs on vocabularies and importing\exporting.
If XXX is not a keyword so "XXX: x:y" is a valid ddd then you can
either use ("XXX"|ATOM) everywhere you want both which probably would
not be very nice, or you need to match "XXX" as an ATOM and use a
semantic predicate to check the token text. I don't have Antlr 2
running on this machine so I can't check but you might be able to use
something like:
aaa : "AAAA" SPACE ddd+ { LT(1).$getText == "XXX" }? ccc;
ccc : ATOM ":" SPACE ((cccentry)+ ;
I don't think you can put the predicate in rule ccc because Antlr 2
does not support predicate hoisting from called rules. And ANTLR may
not use the predicate for the exiting of ddd+. Then you might need to
use a semantic predicate within a syntactic predicate. Something like:
bbb:  ddd ( ( ccc )=> /*epsilon*/ | bbb );
ccc : { LT(1).$getText == "XXX" }? ATOM ":" SPACE ((cccentry)+ ;
The predicate will cause ANTLR to attempt to match ccc (with actions
disabled). If it succeeds it follows the first alternative and drops
out of the nested loop. If it fails it will go to the next alternative
and match a bbb. So it will keep matching nested bbb's until it sees a
ccc.

Tom.

On 7/22/06, Rajeev Bharadhwaj <rajeevb2 at yahoo.com> wrote:
> How do I stop matching so it can reduce the rule?
>
> aaa : "AAAA" SPACE bbb ccc ;
>
> bbb : ( ddd )+ ;
>
> ddd : ha:ATOM ":" SPACE eee SPACE ; // Rule 1
>
> eee : hva:ATOM (":" hvap:ATOM)* ;
>
> ccc : "XXX" ":" SPACE ((cccentry)+ ; // Rule 2
>
> I would like Rule 1 to stop matching when
> ha.getText()==XXX, so aaa can shift to Rule 2 (ccc).
>
> ATOM matches alphabets and numbers to create strings.
>
> R B
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>


More information about the antlr-interest mailing list