Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax ambiguities of C++

Assume there is a declaration:

struct A { static int i; };
A a;

As I know, the input string int decltype(a)::i = 0; has no strictly described behaviour.

It can be parsed as int decltype(a)::i = 0;, where: int is a decl-specifier and decltype(a)::i the declarator.

However,it can be parsed as int decltype(a) ::i = 0;,where int and decltype(a) are parsed asdecl-specifers, and ::i is the (re)declaration of a global variable i - the compiler should give a error msg which goes like "a decl-specifier-seq should not contain two type-specifiers".

I clearly know the first way to parse should be the right one but I can't find any proof.

Anyway, in int A::a = 0;, A is sure to be parsed as part of declarator because A is a type-name and as described in standard

If a type-name is encountered while parsing a decl-specifier-seq, it is interpreted as part of the decl-specifier-seq if and only if there is no previous type-specifier other than a cv-qualifier in the decl-specifier-seq.

In constrant, decltype(a) is not a type-name, it's a type-specifier.

I am not 'finding a quarrel in a straw', I have this question because I'm writing my parser for C++.

So, I wonder if the description should be:

If a type-specifier is encountered while parsing a decl-specifier-seq, it is interpreted as part of the decl-specifier-seq if and only if there is no previous type-specifier other than a cv-qualifier in the decl-specifier-seq.

like image 345
ChungkingExpress Avatar asked Mar 20 '16 11:03

ChungkingExpress


People also ask

What is ambiguity in syntax?

In English grammar, syntactic ambiguity (also called structural ambiguity or grammatical ambiguity) is the presence of two or more possible meanings within a single sentence or sequence of words, as opposed to lexical ambiguity, which is the presence of two or more possible meanings within a single word.

Is C an ambiguous language?

Programming languages like Pascal, C and Java follow this convention, so there is no ambiguity in the semantics of the language, though the use of a parser generator may lead to ambiguous grammars.

What is ambiguity in programming language?

A grammar is said to be ambiguous if there exists more than one leftmost derivation or more than one rightmost derivative or more than one parse tree for the given input string.

How to deal with syntax ambiguity?

Removing ambiguity Syntactic ambiguity can be handled in two ways It can be removed in the grammar - unambiguous syntax leads to unambiguous semantics; or it can be handled by the semantic phase of the compiler, by applying a rule.

What are the different types of ambiguity?

Types of Ambiguity. We can crudely classify the sorts of ambiguity found in sentences as follows: 1. Pure syntactic ambiguity: old men and women. French silk underwear. 2. Quasi-syntactic ambiguity: The astronaut entered the atmosphere again.

What is the difference between syntactic and semantic ambiguity?

[1] In syntactic ambiguity, the same sequence of words is interpreted as having different syntactic structures. In contrast, in semantic ambiguity the structure remains the same, but the individual words are interpreted differently.

What is an ambiguous structure in grammar?

Ambiguous Structures. "Syntactic ambiguity occurs when a sequence of words can be structured in alternative ways that are consistent with the syntax of the language. For instance, . . . [this word group] is ambiguous: (1) a. John told the woman that Bill was dating.


1 Answers

Your definition is explicitly disallowed by [dcl.meaning]/1:

The nested-name-specifier of a qualified declarator-id shall not begin with a decltype-specifier.

(GCC and VC++ are buggy in this respect.)

Your implementation's concrete diagnostic (whether referring to multiple type-specifiers or an invalid nested-name-specifier) is then just a QoI issue. In actuality, implementations will probably implement some variation of the maximum munch principle on type-specifiers, similar to what the original wording of your quote resembled (which is why GCC and VC++ accept your code). However, ICC gives the exact error message you expected:

error: invalid combination of type specifiers

Note that your "resolution" is also incorrect, because we can have multiple type-specifiers; see [dcl.type]/2. In fact, the wording is fine as is, because if the beginning of a valid declarator (in your invalid case, decltype(a)) is a type-specifier, it also is a type-name.

like image 161
Columbo Avatar answered Sep 19 '22 08:09

Columbo