[antlr-interest] C++ beginner questions

Ric Klaren ric.klaren at gmail.com
Mon Oct 3 15:42:35 PDT 2005


Nicola Cuomo wrote:
>>C++ Lexer its $getText. For example:
> 
>>IDENT: (ID_START_LETTER) (ID_LETTER)*
>>{
>> std::string s = $getText;
>>       $setText( LowerCase(s) ); }
>>resolving ambiguities:  syntactic or semantic
>>predicates
> 
> 
> It's possible to do that also in the parser??

$getText() is lexer only.

> Compiling this give me the error
> 
> underline.g: In member function `void TestParser::spec()':
> underline.g:20: error: `text' undeclared (first use this function)
> underline.g:20: error: (Each undeclared identifier is reported only once for
>    each function it appears in.)
> underline.g:20: error: `_begin' undeclared (first use this function)

Antlr is not very good in giving errors for incorrect use of the
shorthands in the actions :(

> As  far  as  construction own type during parsing i'm getting on using
> returns/passing role parameter something like that:
> 
> variableListInit returns [list<VariableInit> rinitVariable]
> { 
>         VariableInit tvariableInit;
>         VariableInit tvariableInits;
> }
>   : tvariableInit=variableInit { rinitVariable.push_back( tvariableInit ); }
>         (COMMA tvariableInits=variableInit { rinitVariable.push_back( tvariableInits ); } )*
>   ;
> 
> variableInit returns [VariableInit rvariableInit]
> {
>         string texpression1;
>         string texpression2;
>         
> }
>         : texpression1=expression EQUAL texpression2=expression
>         { rvariableInit.name = texpression1;  rvariableInit.value = texpression2;} 
>         ;
> 
> and it seem to work.

A little bit depending on how your parser is put together it may be more
efficient to pass in a list reference from the top (saves copying):

variableListInit[list<VariableInit>& rinitVariable]
{
  VariableInit tvariableInit;
  VariableInit tvariableInits;
} : tvariableInit=variableInit { rinitVariable.push_back( tvariableInit ; }
         (COMMA tvariableInits=variableInit {
rinitVariable.push_back(tvariableInits ); } )*
   ;

Cheers,

Ric


More information about the antlr-interest mailing list