[antlr-interest] C++ beginner questions

Nicola Cuomo ncuomo at gmail.com
Fri Sep 30 10:38:27 PDT 2005


Hi,  i'm  also  using  C++  with  antrl  and some examples can surelly
clarify it's use

Friday, September 30, 2005, 6:08:19 PM, Peggy Fieland 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??

---
spec
        : zum (zum)* { std::cout << $getText << std::endl; }
        ;

zum
        : UNDERLINE CONST_IDENT 
        ;
---

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)

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.
-- 
 Nicola                            mailto:ncuomo at gmail.com



More information about the antlr-interest mailing list