[antlr-interest] Fragment for Parserrules

Alexander Gängel alexander at gaengel.de
Mon Jan 15 08:09:32 PST 2007


Thank you

I will look for non-reserved keywords in the Mailing list.

But right now don't see why not just make it possible to make a parser
rule a fragment, cause it would solve the problem easy and would not
change the normal behavior of ANTLR when you just don't use fragment
before the rule. (but ok I'm not into the code from Antlr so maybe there
would be Problems I just don't know about right now)

Alexander

Shmuel Siegel schrieb:
>
> There are ways around it by controlling when idents are checked for
> matches against keyword. The actual order in ANTLR is for IDENT to
> match and then the IDENT is checked to see if it matches ‘test’. If it
> does, the token type is changed. This process can be interfered with
> and can be made dependent on the state of the parser, but lookahead
> will probably cause you problems.
>
>  
>
> You can read a lot more about the subject by doing searches on the
> mailing list. The generic subject is non-reserved keywords.
>
>  
>
> The type of solution that I wrote is probably the most viable. And
> yes, everybody who deals with languages that accept none reserved
> keywords hits it at some time or other.
>
>  
>
> ------------------------------------------------------------------------
>
> *From:* Alexander Gängel [mailto:alexander at gaengel.de]
> *Sent:* Monday, January 15, 2007 5:24 PM
> *To:* Shmuel Siegel
> *Subject:* Re: [antlr-interest] Fragment for Parserrules
>
>  
>
> And there is no way to get around this?
>
> cause I think that such a Problem could occur in may grammars when
> special keywords are only specil in certain positions.
>
> Alexander
>
>
> Shmuel Siegel schrieb:
>
> As I answered you before, keyword (or any parser rule) is effectively
> a fragment. It doesn’t matter that it has been extracted to a separate
> rule. The mistake that you are making is that you think that something
> is identified as “keyword”. The parser doesn’t produce a keyword
> token. Even if you were building a tree, the parser wouldn’t have
> created anything special just because keyword matched. “keyword” will
> only match because it was called from the “start” rule. You are
> probably bothered by the fact that the mere presence of ‘test’ in a
> parser rule causes test to not match Identifier in the lexer. This is
> a fact of life.
>
>  
>
> You can achieve what you stated by rewriting the keyword parser rule as:
>
>  
>
> keyword: (t=’test’ | t=’test2’) {$t.setType(Identifier);};
>
>  
>
> This will reset the ‘test’ and ‘test2’ tokens to be identifiers when
> they are accessed through keyword.
>
>  
>
> Hopefully, I was clear enough
>
> ------------------------------------------------------------------------
>
> *From:* antlr-interest-bounces at antlr.org
> <mailto:antlr-interest-bounces at antlr.org>
> [mailto:antlr-interest-bounces at antlr.org] *On Behalf Of *Alexander G?ngel
> *Sent:* Monday, January 15, 2007 11:17 AM
> *To:* antlr-interest at antlr.org <mailto:antlr-interest at antlr.org>
> *Subject:* Re: [antlr-interest] Fragment for Parserrules
>
>  
>
> Can anybody give me a Hint where I am wrong?
>
> Alexander
>
> Alexander Gängel schrieb:
>
> Here is a small Grammar to show my Problem:
>
> grammar small;
> start    :(Identifier |    '->' keyword    )+;
> keyword:'test'|'test2';
> Identifier:('a'..'z'|'A'..'Z'|'_') (Letter)*;
> fragment
> Letter    :'a'..'z'|'A'..'Z'|'0'..'9'|'_'|'$';
> WS    :(' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;};
>
> for this Grammar input like this is true:
>
> word word -> test word -> test2
>
> but I would like that if the Input is just test it would also be true
> but then be identified as an Identifier not a keyword.
>
> ok in this example I could put my keyword also into the start rule but
> in actual grammar I'm working on I would use it with a lot of keyword
> (about 20) inside  a Syntactic Predicates so I would prefer to make
> the rule keyword a fragment so it#s just recognized inside the start
> rule when a -> is found.
>
> Ok I hope you now understand what I want.
>
> maybe there is a better way to do such a thing with Antlr I just don't
> know about it.
>
> Alexander
>
>
> Kay Roepke schrieb:
>
>
> On 10. Jan 2007, at 12:27 , Alexander Gängel wrote:
>
>
>
> I have some parserrule which should only be applied at special points,
> the problem why I can't inline it because I use in an Syntactic
> Predicate.
>
> or is there some better way I just don't see
>
>
> could you post some sample code/grammar?
>
> I can't really see what you want to do here, that's easier to discuss
> if we have some sample stuff to
> talk about ;)
>
> cheers,
>
> -k
> --Kay Röpke
> http://classdump.org/
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070115/32704559/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: alexander.vcf
Type: text/x-vcard
Size: 289 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20070115/32704559/attachment-0001.vcf 


More information about the antlr-interest mailing list