[antlr-interest] Grammar issue

maulattu maulattu at yahoo.it
Wed Mar 16 08:11:19 PDT 2011


Hi all,
I'm trying to develop a simple lexer/parser in order to parse a list "key - 
value" like:

PARAM_1 = 0x05
PARAM_2 = 75
MESSAGE_1 = this is a message
MESSAGE_2 = go away
VARIABLE = true
...

There are keys (left side) that should be with indexes ("PARAM_1", "PARAM_2", 
...).or without (see "VARIABLE").
The values associated to keys could be numbers (decimal/hexadecimal) or strings.
There are specific keys accepting numbers and other keys accepting strings as 
value, so:
  * ok --> PARAM_1 = 38
  * error --> PARAM_1 = true

Whit the grammar explained below I'm able to parse sentences like:
  * PARAM_1 = 1

but not
  * MESSAGE_1 = this is a message
  * VARIABLE = true

I suppose the error should be in the "TEXT_LITERAL" below (fragment), but I'm 
not sure about it.
Leaving it, the lexer hangs up immediately when it reads "PARAM_1..."
Maybe the whole grammar is not properly structured :-|

Thank you all for your precious help :)

This is the grammar I said above:
/*----------------------------------------*/
parametersList
  : parametersDeclaration+ EOF
  ;

parametersDeclaration
  : parameter_with_index /* like PARAM_1 above*/
  | paramemter_without_index /* like VARIABILE above */
  ;

parameter_with_index
  : parameterName = 'PARAM_' parameterIndex '=' numericLiteral
  | parameterName = 'MESSAGE_' parameterIndex '=' textLiteral
  ;

paramemter_without_index
  : parameterName = 'VARIABLE' '=' textLiteral
  ;
/*----------------------------------------*/
parameterIndex
  : DECIMAL_LITERAL
  ;

numericLiteral
  :  HEX_LITERAL
  |  DECIMAL_LITERAL
  ;

textLiteral
  : TEXT_LITERAL
  ;

HEX_LITERAL
  : '0' ('x' | 'X') HEX_DIGIT+
  ;

DECIMAL_LITERAL
  : '0' | '1'..'9' '0'..'9'*
  ;
/*----------------------------------------*/
fragment
TEXT_LITERAL
  : LETTER (LETTER | '0'..'9')*
  ;

fragment
HEX_DIGIT
  : ('0'..'9' | 'a'..'f' | 'A'..'F')
  ;

fragment
LETTER
  : 'A'..'Z'
  | 'a'..'z'
  | '_'
  | '+'
  ;



      


More information about the antlr-interest mailing list