Looks like you are trying to use keyword as identifier. AFAIK, this
cannot be resolved in the lexer. You have to use predicates in the
parser rule. Something like this:
rule : keyKEYWORD1 (keyKEYWORD2 enc=Name)? ';' ;
keyKEYWORD1
: {input.LT(1).getText().equals("keyword1")}? Name ;
keyKEYWORD2
: {input.LT(1).getText().equals("keyword2")}? Name ;
One more problem I see is the production "Name : Letter* ;". Lexer
production cannot define a zero length string.
Another problem is you are expecting 'keyword1' to be parsed as Name but
production for Name doesn't allow numbers.
- Indhu
Gabriel Petrovay wrote:
> Hi all,
>
> I have the following grammar file:
>
> //========================================
> grammar k;
> options {
> output=AST;
> }
>
> rule : KEYWORD1 (KEYWORD2 enc=Name)? ';' ;
>
> KEYWORD1 : 'keyword1';
> KEYWORD2 : 'keyword2';
>
> Name : Letter* ;
> fragment Letter : 'a'..'z' | 'A'..'Z' ;
>
> S : ('\t' | ' ' | '\n' | '\r')+ { $channel = HIDDEN; } ;
> //========================================
>
>
> The following text is not a valid one.
>
> INPUT:
> =====
> keyword1 keyword2 keyword1 ;
>
> OUTPUT:
> =======
> line 1:18 mismatched input 'keyword1' expecting Name
> <mismatched token: [...@4,18:25='keyword1',<4>,1:18], resync=keyword1
> keyword2 keyword1 ;>
>
>
> How can I make a parser to recognize this input? I want to be able to
> allow the keywords in the places where any char combination is
> allowed. How can I make this?
>
> Regards,
> Gabriel
> ------------------------------------------------------------------------
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address