[antlr-interest] n00b Question

Johannes Luber jaluber at gmx.de
Fri Aug 22 06:58:13 PDT 2008


Anders Karlsson schrieb:
> Another n00b question :)
> 
> Given this grammar ( to handle ini-files with statements like A=1 and B="adds sadsa"):

There are still some problems left.
> 
> prog : stat+;
> 
> stat
> 	:	expr (NEWLINE | EOF)
> 	;

What happens, when the last character is a newline? Or if there are
blank lines?

stat
	:	expr (NEWLINE | EOF)+
	;

should take care of this problem. Furthermore, fragment rules shouldn't
be called from parser rules because parsers expect tokens. That it works
for you may be caused by the fact that NEWLNIE is an hidden channel.
Considering that you DO check for its presence this action is nonsensical.
> 
> expr
> 	:	IDENTIFIER '='^ VALUE 
> 	;
> 
> // lex rules
> 
> IDENTIFIER
>     :	LETTER (LETTER|DIGIT)*
>     ;
> 
> // here seems to be a problem
> 
> VALUE
>     : '"' (LETTER|DIGIT|WS)* '"'
>     | (LETTER|DIGIT)*                        
>     ;
> 	
> fragment
> LETTER
>     :	'$'  |	'A'..'Z' | 'a'..'z' | '_' ; 	
> 
> fragment
> DIGIT : '0'..'9';
> 
> fragment
> NEWLINE	: '\r'? | '\n' { $channel=HIDDEN; } ;

With the changes you should use the following definition:

NEWLINE	: '\r' | '\n';

Note that I removed the '?'. In conjunction with the start rule all '\r'
and '\n' are recognized. Your original version would have never
recognized '\r\n' as intended.

Johannes
> 
> fragment
> WS	: ' '|'\t' { $channel=HIDDEN; } ;
> 
> (removed some actions for clarity)
> 
> I think there seems to be some problem with the VALUE rule
> I wish is to accept both A=1 and A="1" in an ini-file
> When VALUE contains the 2nd subrule ANTLRWorks doesn't
> work in the interpreter, if I comment that rule it works fine but
> then I am restricted to values having quotation marks
> 
> I don't quite understand why it doesn't work.
> 
> Any ideas?
> TIA
> Anders.
> 



More information about the antlr-interest mailing list