Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it more difficult to parse the syntactic structure of C++ than other languages?

I have observed that almost in all IDEs code completion for Java and C# is better than that for C++. For example, in Netbeans, Java auto-completion is far superior to C++ auto-completion, while in Visual Studio, C# auto-completion is way better that Visual C++.

There are tons of IDEs out there offering very good Java auto-completion, but the same is not true for C++, even though it is the older language.

Is it more difficult to parse C++? If so, why?

like image 532
enitihas Avatar asked May 24 '14 23:05

enitihas


2 Answers

Parsing C++ is more difficult, because the grammar is very stateful. Knowing whether A * b; is a pointer declaration or multiplication depends on whether the identifier A in the current scope refers to a type or variable.

But it's not just parsing. Autocompletion requires semantic analysis, overload resolution, template expansion, selection of template specializations, evaluation of constexpr functions since they can appear in template argument lists...

Basically to determine the type of an arbitrary C++ expression and list the members of that type, you need all of a non-optimizing compiler except the machine code generation.

Most of the above steps don't apply to languages that don't have template specialization.

like image 180
Ben Voigt Avatar answered Sep 19 '22 09:09

Ben Voigt


The C++ FQA has a wealth of information about this (although it is written in a style that could be described as "very informal").

Here is some of what it has to say about the factors involved:

is AA BB(CC); an object definition or a function declaration? It turns out that the answer depends heavily on the code before the statement - the "context". This shows (on an intuitive level) that the C++ grammar is quite context-sensitive.

...

So let's locate the definition of CC and move on, right?

...

there's no way to tell in which files CC is defined, or which files must be parsed in order to "understand" its definition

And all of that's before we even begin to consider the turing-complete template system which can automatically generate new code at compile time.

Another advantage that Java and C# have over C++ in this area is reflection. Once you've compiled a Java class, you can load it into a JVM (and you know what file it's in because Java specifies these things) and use the standard interface to inquire as to its methods and so forth. C++ does not provide that capability.

like image 41
Samuel Edwin Ward Avatar answered Sep 21 '22 09:09

Samuel Edwin Ward