[antlr-interest] Grammar issue

Jim Idle jimi at temporal-wave.com
Wed Mar 23 09:03:25 PDT 2011


Pretty much the right idea, but you don't need to create all those
non-terminal rules when you just have the single token, you can just do
this:

parameterWithIndex
 : PARAM parameterIndex SEPARATOR EQ NUMBER;

PARAM : '_PARAM' ;
EQ    : '='	;
NUMBER : ('0'..'9')+ ;

Jim


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of maulattu
> Sent: Wednesday, March 23, 2011 2:41 AM
> To: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] Grammar issue
>
> Ok Jim, let me understand if I'm missing something with a short
> example.
> Take this little section of the grammar
>
> parameter_with_index
>     : parameterName = 'PARAM_' parameterIndex '=' numericLiteral
>     ;
>
>
> It shall be changed to
>
> parameter_with_index
>     : parameterNameLiteral  parameterIndex separator numericLiteral
>     ;
> parameterNameLiteral
>     :    PARAM_TOKEN
>     ;
> separator
>     :    SEPARATOR_TOKEN
>     ;
> SEPARATOR_TOKEN
>     :    '='
>     ;
> PARAM_TOKEN
>     :    'PARAM_'
>     ;
>
> is it right?
>
> Thank you all for your precious suggestions :)
>
>
>
>
>
> ________________________________
> Da: Jim Idle <jimi at temporal-wave.com>
> A: antlr-interest at antlr.org
> Inviato: Lun 21 marzo 2011, 16:25:29
> Oggetto: Re: [antlr-interest] Grammar issue
>
> Take the 'literals' out of the parser and create real lexer tokens that
> are declared before the other, more general tokens.
>
> Using literals in the parser confuses you and will also make error
> handling and tree walking more difficult.
>
> Jim
>
> > -----Original Message-----
> > From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> > bounces at antlr.org] On Behalf Of maulattu
> > Sent: Monday, March 21, 2011 1:53 AM
> > To: antlr-interest at antlr.org
> > Subject: Re: [antlr-interest] Grammar issue
> >
> > Hi all,
> > for a matter of convenience I didn't write here all the rules for
> > white spaces and so on, but I've them in order to skip white spaces,
> > tab, \r, \n and so on.
> > It seems the lexer recognize as "textLiteral" the parameter names
> > (i.e.:
> > "VARIABLE" and "MESSAGE_").
> >
> > Any suggestions?
> >
> > Thank you all :)
> >
> >
> > /*----------------------------------------*/
> > 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'
> >   | '_'
> >   | '+'
> >   ;
> >
> >
> >
> >
> > ________________________________
> > Da: Stephen Tuttlebee <themightystephen at googlemail.com>
> > A: antlr-interest at antlr.org
> > Inviato: Gio 17 marzo 2011, 13:00:59
> > Oggetto: Re: [antlr-interest] Grammar issue
> >
> > Hi
> >
> > Is not the failure to parse:
> >      MESSAGE_1 = this is a message
> > due to the whitespace between the words in the string following the
> > equals sign?
> >
> > Do you have a whitespace lexer rule (often called WS)? I suspect the
> > parser is failing due to the fact that it parses up to 'MESSAGE_1 =
> > this' just fine but then after that point it is expecting another
> > 'parametersDeclaration' to follow, for which the remaining input 'is
> a
> > message' would not have any rules that matches it (the parser would
> be
> > expecting one of three things next, either 'PARAM_', 'MESSAGE_' or
> > 'VARIABLE').
> >
> > I'm not an expert on ANTLR, but I think that might be your problem.
> >
> > Thanks
> > Stephen
> >
> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > Unsubscribe:
> > http://www.antlr.org/mailman/options/antlr-interest/your-email-
> address
> >
> >
> >
> >
> >
> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > Unsubscribe: http://www.antlr.org/mailman/options/antlr-
> interest/your-
> > email-address
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>
>
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address


More information about the antlr-interest mailing list