c++0x: initializer lists and ambiguity

Milian Wolff mail at milianw.de
Thu Aug 11 13:20:33 UTC 2011


Hey all,

I have a hard time adding init-list support for c++0x to kdevelop. Even after 
nearly a week of different approaches I'm still in ambiguity hell.

On one hand there is a huge issue with our current c++ parser not following 
the spec, esp. in parseDeclarationInternal - that is pretty crude.

However I think I've finally managed to get a *nearly* working patch with 
minimal changes to our AST, see attached. It's called minimal and still 
touches hundreds of lines. You should have seen my first approach...

Right now I have a big issue in resolving the following ambiguity:

void foo() {};

The parser will now parse this as a simple-declaration while it should be a 
function-definition. For looking at the c++0x grammar I suggest [1]. When 
reading it, I can't figure out how this ambiguity is supposed to be resolved:

Here's the explanation for simple-declaration:

"void foo() {};"
-> simple-declaration

void
 -> decl-specifier-seq -> decl-specifier -> type-specifier -> trailing-type-
specifier -> simple-type-specifier

foo() {}
 -> init declarator-list -> init-declarator

foo()
  -> declarator -> ptr-declarator -> noptr-declarator

foo
  -> declarator-id -> ...

() -> parameters-and-qualifiers

{} 
  -> initializer -> braced-init-list

So this should be possible to be parsed as a simple-declaration. But this 
actually is supposed to be a function-definition followed by an empty 
declaration:

"void foo() {}" -> function-definition
";" -> empty-declaration

Looking at http://www.nongnu.org/hcb/#declaration you will see that the list 
first mentions block-declaration and hence simple-declaration and function-
definition only afterwards. Should this list be parsed from the end to resolve 
this issue? Or is "6.8 Ambiguite resolution" of the spec applying here:

> There is an ambiguity in the grammar involving expression-statements and
> declarations: An expression- statement with a function-style explicit type
> conversion (5.2.3) as its leftmost subexpression can be indis-
> tinguishable from a declaration where the first declarator starts with a
> (. In those cases the statement is a declaration. [ Note: To disambiguate,
> the whole statement might have to be examined to determine if it is an
> expression-statement or a declaration. This disambiguates many examples.

I wonder because both, simple-declaration and function-definition are 
declarations. Or is the braced-init-list stuff automatically an expression-
statement?

Bye

[1]: http://www.nongnu.org/hcb/
-- 
Milian Wolff | milian.wolff at kdab.com | Software Engineer
KDAB (Deutschland) GmbH&Co KG, a KDAB Group company
Tel. Germany +49-30-521325470, Sweden (HQ) +46-563-540090
KDAB - Qt Experts - Platform-independent software solutions

-- 
Milian Wolff
mail at milianw.de
http://milianw.de
-------------- next part --------------
A non-text attachment was scrubbed...
Name: initlists_minimal.patch
Type: text/x-patch
Size: 39071 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kdevelop-devel/attachments/20110811/c16e52a3/attachment.patch>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kdevelop-devel/attachments/20110811/c16e52a3/attachment.sig>


More information about the KDevelop-devel mailing list