Reputation: 5034
I copied the full c++ grammar into my parser generator but is having problems parsing C++ declaration. How should we interpret a decl-specifier when parsing a pointer type declaration like char* varname?
Using MSDN's c++ grammar as an example http://msdn.microsoft.com/en-us/library/0aah9xbf%28v=vs.71%29.aspx:
A specifier is like that:
decl-specifiers :
decl-specifiersopt decl-specifier
decl-specifier :
storage-class-specifier
type-specifier
fct-specifier
friend
typedef
__declspec ( extended-decl-modifier-seq )
Acccording to that msdn's description char *lpszAppName; should have a decl-specifier = "char * ", name = "lpszAppName". But my test run shows that the decl-specifier should be char, the declarator (i.e. name) should be " * lpszAppName" . Note that the asterisk precedes the name - NOT FOLLOWING the type keyword char. This test result seems reasonable as under decl-specifier, I cannot find anything defining a pointer i.e. there is no rule under the decl-speciifer that can lead to a ptr-operator.
I tested with ANTLR4 parser generator with the full c++ grammar copied from the specification to parse this:
char *
testfunction(int *cx)
{
return;
}
The resultant parse tree is like this:
Clearly a return type "char *" is actually parsed as return type "char" i.e. a node under the decl-specifier, while the * is associated with the function name i.e. a node under the declarator. The (int* cx ) parameter declaration is also considered type "int" while the pointer * is a node under the declarator for cx.
Is my test correct? If yes, then what should be the grammar looks like if we have to support Visual C++ specific function modifier keywords like __cdecl, __stdcall, or __fastcall e.g.
char * __fastcall
testfunction(int *cx)
{
return;
}
Clearly, the syntax requires the "char*" be really treated as a type i.e. a node under abstract-declarator.
Upvotes: 1
Views: 836
Reputation: 179877
Consider the famous char* x, y;
declaration. y
is a char
, not a char*
. The reason is that the *
applies to x
only. The decl-specifier
indeed is just char
.
Upvotes: 0