[antlr-interest] Parsing problem in C++

Arnulf Heller aheller at gmx.at
Wed Nov 10 06:11:37 PST 2010


Am 10.11.2010 10:31, schrieb David Wigg:
> Amulf,
>
> Many thanks for your prompt and most useful response.
>
> There is an interesting point here. In the grammar you can have either
> "class" or "typename" as a type-parameter. In our case it is "class".
> In your example it is "typename", At the moment in both "class E" and
> "typename E" I classify E as a typedef but I could distinguish between
> the two cases if necessary. There does not appear to be any
> significant difference between the two in the Stroustrup grammar
> definition but then neither does it say whether E should be classified
> as a class or a typedef.
>
> The question I need to ask is would the use of  "class" in your
> example make any difference to your reply?
no, it would not. the keywords "class" and "typename" can be 
interchanged (in the template argument list)
(see: http://www.codeguru.com/forum/showthread.php?t=422570)

> I see in your example you quote "// cannot use T x = 0; for classes!"
> although referring to "typename" as the type-parameter so perhaps
> there is no difference "class" and "typename" here..
you can also look at this small example:

template <typename T> class Couch
{
public:
     Couch() { T t = 0; }
};

template <typename T> class Cnoouch
{
public:
     Cnoouch() { T t = T(); }
};

class V
{
     V() {}
};

int main()
{
     Couch<int> coi; // ok
     Couch<V> cov; // ouch! 0 cannot be assigned to V
     Cnoouch<int> cnoi; // ok
     Cnoouch<V> cnov; // ok (no ouch:)

     // this effectively happens in Cnoouch; does also compile directly
     int i = int(); // i == 0 !
     char u = char(); // u == 0 !

     return 0;
}

> Unfortunately I can't see anything about T() (or T(0)) in the
> Stroustrup grammar definition as a zero (or null) initialisation.
Yes, Stroustrup templates are quite early; there are only a couple of 
pages in his book and much changed since then.

My favorite book can also be found here:
http://books.google.de/books?id=yQU-NlmQb_UC&printsec=frontcover&dq=vandervoorde+josuttis&source=bl&ots=EdpN59ZzEX&sig=yFJ6b1vXMBx9WfsA75uep1jZEB4&hl=de&ei=jqHaTJfAD43IswaP-PXsBw&sa=X&oi=book_result&ct=result&resnum=8&ved=0CFAQ6AEwBw#v=onepage&q&f=false

If you are lucky enough to get page 56 you will find more on zero 
initialization.
Type traits like your char_traits<> example are a chapter of their own.

Its also quite interesting to see the microsoft implementation:


template<class _Elem,
     class _Int_type>
     struct char_traits
     {    // they use the notation _Elem() in order to look for the null 
termination char
     static size_t length(const _Elem *_First)
         {    // find length of null-terminated sequence
         size_t _Count;
         for (_Count = 0; !eq(*_First, _Elem()); ++_First)
             ++_Count;
         return (_Count);
         }


template<>
     struct char_traits<wchar_t>
     {    // partial specialization for wchar_t calls build-in function; 
probably faster, more safe, ... ?
     static size_t length(const _Elem *_First)
         {    // find length of null-terminated sequence
         return (_CSTD wcslen(_First));
         }

> Thank you for your encouraging comment about looking forward to a C++
> grammar for Antlr3!
If you manage to parse the STL, you are a king!
If you manage to parse the boost, you are god ;)

cheers,
Arnulf




More information about the antlr-interest mailing list