Topic: parsing question - type sepc. vs. declarator
Author: Steve Clamage <stephen.clamage@sun.com>
Date: 1999/11/19 Raw View
saroj@bear.com wrote:
>
> Hi,
>
> I am trying to do some pretty-printing by parsing C++ code. I only
> know if a name is a type or not. So int and X (where X is a class name)
> are equivalent.
> Given a code fragment:
>
> X :: name ...
>
> how can I know if :: is part of X or if it is part of name?
> e.g. struct X { typedef int Y;};
>
> X::Y i; // Y is a typedef in class X (X::Y printed together)
> X ::Y::f(); // f is a function in namespace Y returning a X.
> // (::Y::f is printed together)
Good question. The standard is not clear on the issue of how
to parse a declaration when the declarator starts with a
global scope modifier, as in your example. It is currently
a subject of discussion in the C++ Committee.
Some compilers apply the "maximum munch" rule, and parse the
second line as
X::Y::f(); // call function X::Y::f
If you are writing code, you should use parentheses around the
declarator to disambiguate:
X ( ::Y::f )(); // declare function ::Y::f
X ( ::Y::f() ); // another way to parenthesize
( X::Y::f )(); // call function X::Y::f
( X::Y::f() ); // another way to parenthesize
In these examples I'm using spaces to help show how the
tokens go together; the spaces have no syntactic meaning.
Note that you cannot put parens around the type:
(X) ::Y::f(); // syntax error
--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: saroj@bear.com
Date: 1999/11/17 Raw View
Hi,
I am trying to do some pretty-printing by parsing C++ code. I only
know if a name is a type or not. So int and X (where X is a class name)
are equivalent.
Given a code fragment:
X :: name ...
how can I know if :: is part of X or if it is part of name?
e.g. struct X { typedef int Y;};
X::Y i; // Y is a typedef in class X (X::Y printed together)
X ::Y::f(); // f is a function in namespace Y returning a X.
// (::Y::f is printed together)
Thank you,
Saroj Mahapatra
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]