[antlr-interest] iniFile grammer

Романов Артем arteminus at yandex.ru
Wed Aug 17 05:02:06 PDT 2011


I try define iniFile grammer(keys contains few subvalues).
I defined it from C# Regex:
Regex commentLine = new Regex(@"^\s*#(?<comment>.*)", RegexOptions.Compiled);
Regex sectionLine = new Regex(@"^\s*\[(?<section>.*)\].*", RegexOptions.Compiled);
//Regex recordLine = new Regex(@"^\s*(?<key>[^#[=\s]+)\s*=?\s*(?<values>[^#]*)(#(?<comment>.*))?", RegexOptions.Compiled);
Regex recordLine2 = new Regex(@"^\s*(?<key>[^#[=\s]+)\s*=?\s*((?<value>[^;\#]*);)*(?<endValue>[^;\#]*[^;\s\#]+)?\s*(#(?<comment>.*))?", RegexOptions.Compiled);
foreach(var c in recordLine2.Math(string).Groups["value"].Captures)
	//access to each value of key
Sample ini struct:
#comment
[section]
key1
key2=
key3= # this and earlier lines contains 0 values
key4=a# 1 values
key5=;# 1 empty values
key6=a;f # 2 values
key7= a ; f;; # 3 values
[section2]
..
But I don't know how implement endValue(without semicolon) and get lot of warnings from my grammer. 
This grammer return wrong parse tree([section2] as keyLine).
I testet grammer in ANTLRWork 1.4.3

grammar test;
WS	:	(' '|'\t')	{$channel=HIDDEN;};
EOL	:	('\r\n'|'\n'|'\r') ;
SHARP	:	'#' {System.out.println("#");};
EQUAL	:	'=' {System.out.println("=");};
SEMICOLON	:	';' {System.out.println(";");};		
COMMENT	:	SHARP .* EOL ;//{System.out.println("COM");};
SECTION	:	'[' .* ']' {System.out.println("SEC");};
ANY	:	. {System.out.println("ANY");};
iniFile	
	:	section* EOF;
section 
	: commentLine* sectionLine COMMENT* (keyLine COMMENT?)*;
commentLine 	
	: COMMENT;
sectionLine
	: SECTION (EOL|COMMENT);
keyLine	
	:	key keyValues* (EOL|COMMENT);
key	
	:	~('='|'#'|'['|EOL)+  {System.out.println("key");};
keyValues
	: '=' (keyValue';')*;
keyValue:	~(';'|'#'|EOL)* ;	


More information about the antlr-interest mailing list