So I guess I have 2 question related to most of c/c++ compilers :
1. when the scanner of most of c/c++ compilers see something like MyArray[20], what is the token that it creates? do most of the compilers create a token like array_token or array_token[const_int] or...? ( I want to know what happens to the array size after turning it into a token) this question is kinda related to my second question
2. when we write something like MyArray[20.5] in the middle of our code (not in the declaration) does the parser detects this error using the grammar or we can only detect this using semantic routines?
Important note : I am talking about most of the c/c++ compilers and the most general way, I know that some rare compilers might do it differently but how do the most of compilers act? what is the norm? or at least how does the most popular compiler work? (In our exams they just say its a c/c++ compiler so we just have to assume it acts like most of the compilers)
My take on the second question:
I think the parser cannot detect this because we can have an expression inside the scope like MyArray[I*j] therefore we have something like S--> array_token[expression] in our grammar, and since the expression can have float in it so therefore the parser wont detect the error but please correct me if i'm wrong.
According to the lexical rules described in the ISO C standard, MyArray[20]
is an identifier, followed by a [
, followed by an integer constant, followed by a ]
. I'd expect most (or even all) C compilers to represent it exactly like that. There's no such thing as an array token defined in the standard or any implementation I'm aware of.
The grammar rule for an array subscript is:
postfix-expression: postfix-expression [ expression ]
MyArray[20.5]
matches that rule, so it's syntactically valid. It's a type error, not a syntax error. Thus it's the semantic analyzer's job to detect that error.
(In response to your comment)
if we have something like INT array[10.5] it turns into this -------> int id[const_token] therefore the parser will detect this one correct? ( this time I'm talking about in the declaration ) since we can have a rule in the grammar like S--> id[int_const]
The C891 grammar rule for an array declarator (the part that comes after the int
in your declaration) is as follows:
direct-declarator: direct-declarator [ constant-expressionopt ]
array[10.5]
matches that rule (because direct-declarator: identifier
is another rule), so int array[10.5]
is a syntactically valid declaration. So again, it's a semantic error, not a syntactic one.
PS: Note that constant-expression
is simply defined as an alias for conditional-expression
. The fact that the expression should actually be constant is not enforced by the grammar and is also a semantic property.
1 I used the rule from C89 because it's simpler than in later versions. However later versions don't differ in a way that's relevant to the question.