dcsimg
Runtime token assigning - URGENT
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Przemyslaw_Jaskierski
Posted On:   Thursday, June 19, 2003 06:47 AM

Hi, I have two problems with my ANTLR - generated Parser/Lexer (I'm new in this things). Please, help me... I'm stuck. (At the end there is a part of grammar file that this questions are about) 1. How can I set "SEPARATOR" token value from my grammar file in produced CSVLexer/CSVParser? I mean, I wrote class that uses this Parser/Lexer but I want the class' user to have ability to set SEPARATOR to ';' instead of ",". How is it possible to define variable SEPARATOR in Parser/Lexer that will be used to process input without running antlr.Tool again? I wonder why ANTLR hard-code tokens instead of exposing them in easy-to-modify variables... 2. In my grammar I tried to put    More>>

Hi, I have two problems with my ANTLR - generated Parser/Lexer (I'm new in this things). Please, help me... I'm stuck.

(At the end there is a part of grammar file that this questions are about)



1. How can I set "SEPARATOR" token value from my grammar file in produced CSVLexer/CSVParser? I mean, I wrote class that uses this Parser/Lexer but I want the class' user to have ability to set SEPARATOR to ';' instead of ",". How is it possible to define variable SEPARATOR in Parser/Lexer that will be used to process input without running antlr.Tool again?
I wonder why ANTLR hard-code tokens instead of exposing them in easy-to-modify variables...



2. In my grammar I tried to put

RECORD : (~('"'|SEPARATOR|'
'|'
'))+;

instead of

RECORD : (~('"'|','|'
'|'
'))+;



but antlr.Tool throws error:

csv.g:119:13: This subrule cannot be inverted. Only
subrules of the form:

(T1|T2|T3...) or

('c1'|'c2'|'c3'...)

may be inverted (ranges are also allowed).

Exiting due to errors.



How can I do that?



******************************************************


My grammar file:

---------------------------------------

class CSVLexer extends Lexer;

options

{

defaultErrorHandler=false;

k=2;

charVocabulary = '3'..'377';

}


RECORD : (~('"'|','|'
'|'
'))+;

SEPARATOR : ',';

QRECORD : '"'!('"''"'|~('"'))+'"'!;

NEWLINE : '
''
' //DOS

| '
' //MAC

| '
' //UNIX

{ newline(); };

--------------------------------------

   <<Less

Re: Runtime token assigning - URGENT

Posted By:   Monty_Zukowski  
Posted On:   Friday, June 20, 2003 09:27 AM

ANTLR isn't that dynamic. It has to know what the separator is before it generates the code.


ANTLR can invert subrules that only contain alternatives of single tokens. It won't follow through rule references.


If you need to specify the separator at runtime you might want to use a regular expression library. Or you could define an otherwise unused character and make it the separator. Then do a string replacement to change the real separator to that unused character.

About | Sitemap | Contact