[antlr-interest] Unix-like parameters grammar

Cristian Peraferrer corellian.c at gmail.com
Wed Jan 23 10:28:55 PST 2008


Hi,

I am trying to build a grammar to parse unix-like parameters, but I'm  
having problems with the FileName token.
Any help or suggestion will be appreciated.

Thanks in advance.
Cristian

Here the grammar:

grammar params;

options {k = 1;}

@members {
   public String output_filename = null;
   public String input_filename = null;
}

/*------------------------------------------------------------------
  * PARSER RULES
  *------------------------------------------------------------------*/

// Parameters grammar
parameters
	:	inputMethod? opts? ;

inputMethod
	:	'-' {System.out.println("input method: stdin"); }
	|	FileName {input_filename = $FileName.text;}
	;

opts	:	(longOption | shortOption)+ ;

// Long format options grammar
longOption
	:	'--output' FileName {output_filename = $FileName.text;} ;
	
// Abbreviated format options grammar
shortOption
	:	'-o' FileName {output_filename = $FileName.text;} ;
	
/*------------------------------------------------------------------
  * LEXER RULES
  *------------------------------------------------------------------*/

FileName
	:	(~('-'|' -'))* ;
// White spaces
WS	:	(' '|'\t') {$channel = HIDDEN;} ;


-- 
GPG Key-ID: 0x564903FA - Jabber ID: corellian at swissjabber.ch



More information about the antlr-interest mailing list