[antlr-interest] Lexer and Parser using CSharp3

John B. Brodie jbb at acm.org
Sun Dec 19 06:34:57 PST 2010


Greetings!

On Sun, 2010-12-19 at 13:43 +0100, Gian Maria wrote:
> 
> program: start+;
> 
>  
> 
> start :      (let | num);
> 
>  
> 
> let    :      LET;
> 
> num    :      NUM;
> 
>  
> 
> LET    :      ('a'..'z')+;
> 
> NUM    :      ('0'..'9')+;
> 
>  
> 
> WHITESPACE   :      ( '\t' | ' ' | '\r' | '\n' )+ { $channel = HIDDEN; };
> 
>  
> 
> 
> 
> 1) Why inside Lexer and Parser all methods are declared Private (for example
> the above "program" parser rule)? If I want to use one method of my Parser I
> have to change it to public inside source code, is right to do this?
> 

I do not use C# so can not answer this question, sorry.

>  
> 
> 2) Is there a way to find if CommonTokenStream find some undefined chars in
> the string?
> 
>  
> 
> In the above definition I want as input only lower case letters and number,
> so if I write "123 aa 5 Bc" I want to detect the error at "B", How can I do
> that?
> 

By design ANTLR parsers stop parsing at the first erroneous input. I
think that the rationale is that you might want to process that
remaining input with some other software.

When you want ANTLR parsers to process all of the input, including
reporting and (trying to) recover from errors you must tell it to do
that by specifying the recognition of the special, built-in, EOF token.

so your top-level parsing rule should be:

program : start+ EOF ;

or better

program : start+ EOF! ;

where the ! suffix meta-operator causes the EOF token to not be included
in the generated AST.

Hope this helps...
   -jbb




More information about the antlr-interest mailing list