[antlr-interest] help - Parser problem
06virg at gmail.com
Wed Dec 3 02:05:31 PST 2008
Thanks for your very quick response. Now i have understood how lexer and
1. As i have set of commands to parse
if there is any unknown command found it breaks the parsing and returns with
the error. But how to continue the parsing even though it finds unknown
command after returning the error.
I have added the functions "getErrorMessage(RecognitionException e, String
tokenNames), getTokenErrorDisplay(Token t) to a grammar file.
2. Added package at the beginning of header to grammar code. I could see the
package name for parser file but not for the lexer. Why?
On Wed, Dec 3, 2008 at 12:13 PM, Gavin Lambert <antlr at mirality.co.nz> wrote:
> At 18:28 3/12/2008, virg g wrote:
>> ok. Is there anyway i can mention the commands and its paramters without
>> mentioning them in the quotes?
> Yes -- either declare a named token for it instead (as you did for TESTCMD)
> or define an explicit lexer rule:
> CMD : 'CMD';
> Then you can refer to it as CMD in all the parser rules instead of 'CMD',
> and it'll have a nicer name in the generated code to boot.
> Eliminating quoted constants from parser rules like this will also let you
> group all of your lexer rules together in one place, making it easier to
> spot surprises and harder to forget that ANTLR is not a scannerless parser.
> The literal rule is combination of "OR" of Number, ALPHA and STRING. So
>> for the command " TESTCMD CNT=2 VAL=TYPE:CNT" where for VAL paramters CNT
>> will match ALPHA lexer rule right? as it works if it is "TEST". Otherwise i
>> have to add expilcitely all the quoted values to "literal" rule like this.
>> sorry if understanding is wrong.
>> literal : (NUMBER|ALPHA|STRING |TESTCMD|'CNT'|'VAL'|'TYPE')
> Nope, that's exactly right (that you do have to explicitly mention them, I
> mean). As each of those are unique token types, if you want 'literal' to
> match all of them then you have to actually mention all of them.
> Despite the string "CNT" being *compatible* with the rule ALPHA, the simple
> fact is that when the lexer ran it didn't generate an ALPHA, it generated a
> CNT (or a T14, or whatever). The parser doesn't know that this is
> acceptable where an ALPHA is unless you tell it.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the antlr-interest