Mihai Danila wrote:
Remember that this is not {f}lex. The lexer does not try each possible match in turn then go on to the next when one fails. ANTLR lexers are more programmatic in nature; they are both more flexible (no pun intended) and more prone to getting things wrong without more explicit instructions (though some things are earmarked for improvement). If you do this type of thing though: fragment TODAY : ; fragment TOMORROW : ; fragment D; Alpha : ('TODAY')=>'TODAY' { $type = TODAY; } | ('TOMORROW')=>'TOMMOROW' { $type = TOMORROW; } | {canBeD}?=> 'D' {$type = D; } | ('a'..'z'|'A'..'Z') ; ErrChar : . { Record illegal char err; skip(); } ; BTW - We all need to get past assuming that the longest match thing is causing problems. In most cases this isn't the issue per se, but lack of enough guidance to the rules to tell ANTLR what you want the outcomes to be. There are various opinions on what the analysis should do by default of course and some of that I believe Ter has already said he is going to try to make more 'intuitive'. But for now, if you remember that ANTLR will just look for enough 'stuff' to decide that this is the only rule that can work from here and it does not come back after going down that path and say "Oh, that was wrong, let me try a simpler/the next rule", unless you tell it that that is what you want a little more explicitly than you would in flex. In the end though, if you don't cover all the bases, you will just get a mismatched character, and you should not get such a thing from a lexer. Also, it is always better to accept any old rubbish in a token when you can and then verify the char sequence later. So, if you can't have '\u0567' in an ID, accept it anyway (assuming it isn't some other valid separator etc), then when you see the ID token in the parser, call isValidId() and print "Error - the identifier ... cannot contain char '\u0567' - line 45, offset 17". This makes much more sense the "Illegal char:" from the default lexer error. Similarly, do not try to match 'D' on its own. Just look for ID in the parser then have a check to see that it is a D, or separate the parsing of that type of construct into an island grammar or external grammar. Divide and conquer etc. Jim --~--~---------~--~----~------------~-------~--~----~ 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
