[antlr-interest] need help on simple grammar

Christian Ulrich christianhofmann_de at yahoo.de
Sun Apr 22 15:12:25 PDT 2007


Hello,

I'm thinking about a simple problem and how to solve
it using ANTLR (I'm a beginner ....).
I'd like to parse an initialisation file e.g.
"win.ini". 
The file contains sections "[section_name]" and
key-value pairs "key=value".
Not all sections and key-value pairs are known and so
I'd like to find only some interesting of them.

I used ANTLRWorks interpreter with this sample input:
(KeyX could be any non-ambiguous identifier)
On 'Key1' or 'Key4' I'd like to call a special rule
and parse the value. Other key-values should be
skipped.

[MySection]
Key1=val1
Key2=val2
Key3=val3
:
KeyX=valX

At the first step I omitted the "=value"
Here are some definitions I've made:

BEGIN_SECTION : '[' ;
END_SECTION : ']' ;
MYSECTION : 'MySection' ;
KEY1 : 'Key1';
KEY4 : 'Key4';
KEYX : ('a'..'z'|'A'..'Z'|'_')+ ; // match other keys

ini_file :	
	my_section
	( section_keys )+;

my_section 	: 
	BEGIN_SECTION MYSECTION END_SECTION
	;

section_keys :
	  { KEY1 }? key_1
	| { KEY4 }? key_4
	| { KEYX }? key_x;

key_1 : ...;

key_4 : ...;

key_x : ...; // skip

It does not work!
Maybe anyone could give me a hint to solve such a
problem using ANTLR.

Thanks in advance

Regards
Christian


      __________________________________  Kennt man wirklich jeden über 3 Ecken? Die Antworten gibt's bei Yahoo! Clever. www.yahoo.de/clever


More information about the antlr-interest mailing list