c symbols with different meanings in different contexts

  • Thread starter amit.codename13
  • Start date
A

amit.codename13

in the statement

int a=1,2;

i get the following error with a gcc compiler

/home/amit/main.c||In function ‘main’:|
/home/amit/main.c|6|error: expected identifier or ‘(’ before numeric
constant|
||=== Build finished: 1 errors, 0 warnings ===|

the error suggests that either we put a identifier after the comma :
int a=1,b;

or we put a '(' before numeric constant :
int a=(1,2);

so one makes comma act as a initializer list separator and the other
makes it act as a comma operator...

so what may be actual reason for generation of the error...

does it violate the constraints in the syntax of a comma operator or
in the syntax of declarations...

how does the standard deals with symbols with different meanings in
different contexts
 
K

Keith Thompson

in the statement

int a=1,2;

I was going to say that that's a declaration, not a statement. But
strictly speaking it's neither, because of the syntax error.
i get the following error with a gcc compiler

/home/amit/main.c||In function 'main':|
/home/amit/main.c|6|error: expected identifier or '(' before numeric
constant|
||=== Build finished: 1 errors, 0 warnings ===|

(I've replaced a couple of non-ASCII characters in the above.)
the error suggests that either we put a identifier after the comma :
int a=1,b;

or we put a '(' before numeric constant :
int a=(1,2);

so one makes comma act as a initializer list separator and the other
makes it act as a comma operator...
Right.

so what may be actual reason for generation of the error...

The reason is that the line is not syntactically valid.

There could be any number of ways to correct the error. You could
change the ',' to '.', making it "int a=1.2;", with an implicit
conversion from double to int. You could delete the ',', making it
"int a=12;". You could delete he "1,", or the ",2". You could change
it to "int a[]={1,2};". You could delete the "=1,2", or the entire
line. The language says nothing about which correction is the right
one.

Presumably the programmer who wrote the line knows what he meant to
write. The compiler can only make a guess; ideally, such a guess will
help the programmer figure out how to transform the invalid line into
a something that's both legal and semantically correct.
does it violate the constraints in the syntax of a comma operator or
in the syntax of declarations...
Yes.

how does the standard deals with symbols with different meanings in
different contexts

By looking at the context. The grammar defines which sequences of
characters are syntactically valid C programs, and which are not. The
parser (part of the compiler) analyzes a source file and determines
whether it satisfies the grammar or not; if it doesn't, it can
*probably* determine where the error is, but that can be difficult in
some cases.

If you're asking how parsers work, that's a big subject.
 
L

lawrence.jones

int a=1,2; [...]
does it violate the constraints in the syntax of a comma operator or
in the syntax of declarations...

The syntax of declarations. An initializer must be an assignment
expression and assignment expressions do not allow the comma operator,
so the "," must be parsed as a separator.
 
A

amit.codename13

in the statement
int a=1,2;

I was going to say that that's a declaration, not a statement.  But
strictly speaking it's neither, because of the syntax error.
i get the following error with a gcc compiler
/home/amit/main.c||In function 'main':|
/home/amit/main.c|6|error: expected identifier or '(' before numeric
constant|
||=== Build finished: 1 errors, 0 warnings ===|

(I've replaced a couple of non-ASCII characters in the above.)
the error suggests that either we put a identifier after the comma :
int a=1,b;
or we put a '(' before numeric constant :
int a=(1,2);
so one makes comma act as a initializer list separator and the other
makes it act as a comma operator...
Right.

so what may be actual reason for generation of the error...

The reason is that the line is not syntactically valid.

There could be any number of ways to correct the error.  You could
change the ',' to '.', making it "int a=1.2;", with an implicit
conversion from double to int.  You could delete the ',', making it
"int a=12;".  You could delete he "1,", or the ",2".  You could change
it to "int a[]={1,2};".  You could delete the "=1,2", or the entire
line.  The language says nothing about which correction is the right
one.

Presumably the programmer who wrote the line knows what he meant to
write.  The compiler can only make a guess; ideally, such a guess will
help the programmer figure out how to transform the invalid line into
a something that's both legal and semantically correct.
does it violate the constraints in the syntax of a comma operator or
in the syntax of declarations...
Yes.

how does the standard deals with symbols with different meanings in
different contexts

By looking at the context.  The grammar defines which sequences of
characters are syntactically valid C programs, and which are not.  The
parser (part of the compiler) analyzes a source file and determines
whether it satisfies the grammar or not; if it doesn't, it can
*probably* determine where the error is, but that can be difficult in
some cases.

If you're asking how parsers work, that's a big subject.

--
Keith Thompson (The_Other_Keith) (e-mail address removed)  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

so,
whenever a syntax error happens, it does not make sense to find the
cause of error cos there can be many ways to correct the error and
either correction would suggest the cause of error to be a deviation
of the incorrect form from that particular correct one.

hmm but when we are asked a question like:

in this example

int i=1,2;

what causes a syntax error...

which is a more appropriate answer...

is it that the comma acts as a initializer list separator an thus a
identifier must follow it or should it be that the comma acts as a
comma operator and assumes expression in its left side ????
 
A

amit.codename13

in the statement
int a=1,2;

I was going to say that that's a declaration, not a statement.  But
strictly speaking it's neither, because of the syntax error.
i get the following error with a gcc compiler
/home/amit/main.c||In function 'main':|
/home/amit/main.c|6|error: expected identifier or '(' before numeric
constant|
||=== Build finished: 1 errors, 0 warnings ===|

(I've replaced a couple of non-ASCII characters in the above.)
the error suggests that either we put a identifier after the comma :
int a=1,b;
or we put a '(' before numeric constant :
int a=(1,2);
so one makes comma act as a initializer list separator and the other
makes it act as a comma operator...
Right.

so what may be actual reason for generation of the error...

The reason is that the line is not syntactically valid.

There could be any number of ways to correct the error.  You could
change the ',' to '.', making it "int a=1.2;", with an implicit
conversion from double to int.  You could delete the ',', making it
"int a=12;".  You could delete he "1,", or the ",2".  You could change
it to "int a[]={1,2};".  You could delete the "=1,2", or the entire
line.  The language says nothing about which correction is the right
one.

Presumably the programmer who wrote the line knows what he meant to
write.  The compiler can only make a guess; ideally, such a guess will
help the programmer figure out how to transform the invalid line into
a something that's both legal and semantically correct.
does it violate the constraints in the syntax of a comma operator or
in the syntax of declarations...
Yes.

how does the standard deals with symbols with different meanings in
different contexts

By looking at the context.  The grammar defines which sequences of
characters are syntactically valid C programs, and which are not.  The
parser (part of the compiler) analyzes a source file and determines
whether it satisfies the grammar or not; if it doesn't, it can
*probably* determine where the error is, but that can be difficult in
some cases.

If you're asking how parsers work, that's a big subject.

--
Keith Thompson (The_Other_Keith) (e-mail address removed)  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

so,
whenever a syntax error happens, it does not make sense to find the
cause of error cos there can be many ways to correct the error and
either correction would suggest the cause of error to be a deviation
of the incorrect form from that particular correct one.

hmm but when we are asked a question like:

in this example

int i=1,2;

what causes a syntax error...

which is a more appropriate answer...

is it that the comma acts as a initializer list separator an thus a
identifier must follow it or should it be that the comma acts as a
comma operator and assumes expression in its left side ????
 
J

jameskuyper

in the statement

int a=1,2;

i get the following error with a gcc compiler

/home/amit/main.c||In function ‘main’:|
/home/amit/main.c|6|error: expected identifier or ‘(’ before numeric
constant|
||=== Build finished: 1 errors, 0 warnings ===|

the error suggests that either we put a identifier after the comma :
int a=1,b;

or we put a '(' before numeric constant :
int a=(1,2);

so one makes comma act as a initializer list separator and the other
makes it act as a comma operator...

so what may be actual reason for generation of the error...

You'll have to ask the programmer who generated the error what the
reason was. :)
does it violate the constraints in the syntax of a comma operator or
in the syntax of declarations...

The reason for the generation of the error message (as opposed to the
reason for the generation of the error itself) is as follows:

The syntax of initializers requires that they be either a single
assignment-expression, or a comma-delimited list of initializers
enclosed in curly brackets: {} (6.7.8p1). In this context, 1,2 cannot
be parsed as an assignment-expression using the comma operator,
because the result of applying a comma operator is a plain expression,
not an assignment expression (6.5.17p1). Only by putting it in
parethesis would it qualify as a primary expression (6.5.1p1), and
therefore (6.5.2 through 6.5.15) as an assignment expression.

Therefore, when the parser reaches the ',' it assumes that the
initializer for 'a' is complete, and treats the ',' as indicating that
the next thing it will see is a new init-declarator (6.7p1). When it
finds the '2', it identifies that a syntax error of some kind has
occurred. As Keith explained, it has no way of being certain what kind
of error was committed, so all it can do is make suggestions for
improvement.
 
K

Keith Thompson

so,
whenever a syntax error happens, it does not make sense to find the
cause of error cos there can be many ways to correct the error and
either correction would suggest the cause of error to be a deviation
of the incorrect form from that particular correct one.

No, that's not what I said.

The standard says what is or is not syntactically correct; it doesn't
say anything about how to correct a given syntax error. It couldn't
provide a definitive correction, since there could be multiple
possible ways to correct a given error, and there are no firm criteria
for determining which correction is the best one.

But a compiler can often (but not always) make a very good guess about
how to correct a given error, and it does make sense for the compiler
to provide whatever guidance it can to the programmer.

For example, given

int i=1,2;

a particularly clever compiler might say something like:

An initializer must be an assignment-expression; ``1,2'' is an
expression, but it's not an assignment-expression.
hmm but when we are asked a question like:

in this example

int i=1,2;

what causes a syntax error...

which is a more appropriate answer...

is it that the comma acts as a initializer list separator an thus a
identifier must follow it or should it be that the comma acts as a
comma operator and assumes expression in its left side ????

Neither answer is really more appropriate than the other. Either is
just a guess. I suppose whichever guess happens to be correct *in
your case*, or perhaps in the majority of real-world cases, might be
considered "more appropriate".

The real question is this:

When you wrote "int i=1,2;", what did *you* really mean?

And why are you asking this question in the first place? If I told
you that one of the two answers is "more appropriate", what would you
do with the information? I'm not trying to be rude; sometimes
understanding why someone is asking a question is important in
determining the best answer.
 
K

Keith Thompson

Richard Heathfield said:
(e-mail address removed) said: [...]
hmm but when we are asked a question like:

in this example

int i=1,2;

what causes a syntax error...
[...]
There is no comma operator in the declaration.

How do you know it's a declaration? Removing the "int" keyword makes
it a valid statement.
 
A

amit.codename13

[...]
so,
whenever a syntax error happens, it does not make sense to find the
cause of error cos there can be many ways to correct the error and
either correction would suggest the cause of error to be a deviation
of the incorrect form from that particular correct one.

No, that's not what I said.

The standard says what is or is not syntactically correct; it doesn't
say anything about how to correct a given syntax error.  It couldn't
provide a definitive correction, since there could be multiple
possible ways to correct a given error, and there are no firm criteria
for determining which correction is the best one.

But a compiler can often (but not always) make a very good guess about
how to correct a given error, and it does make sense for the compiler
to provide whatever guidance it can to the programmer.

For example, given

    int i=1,2;

a particularly clever compiler might say something like:

    An initializer must be an assignment-expression; ``1,2'' is an
    expression, but it's not an assignment-expression.
hmm but when we are asked a question like:
in this example
int i=1,2;
what causes a syntax error...
which is a more appropriate answer...
is it that the comma acts as a initializer list separator an thus a
identifier must follow it or should it be that the comma acts as a
comma operator and assumes expression in its left side ????

Neither answer is really more appropriate than the other.  Either is
just a guess.  I suppose whichever guess happens to be correct *in
your case*, or perhaps in the majority of real-world cases, might be
considered "more appropriate".

The real question is this:

When you wrote "int i=1,2;", what did *you* really mean?

And why are you asking this question in the first place?  If I told
you that one of the two answers is "more appropriate", what would you
do with the information?  I'm not trying to be rude; sometimes
understanding why someone is asking a question is important in
determining the best answer.

--
Keith Thompson (The_Other_Keith) (e-mail address removed)  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

This was one the questions asked in a interview to one of my friend...

How should i explain the error ...

@richard

No, it acts as an init-declarator-list separator.
> an thus
> a identifier must follow it or should it be that the comma acts as
> a comma operator and assumes expression in its left side ????

There is no comma operator in the declaration.

in this example:

int a=(1,2);
there is a comma and i hope it does act as a comma operator.
 
F

Flash Gordon

This was one the questions asked in a interview to one of my friend...

How should i explain the error ...

<snip>

That depends on whether it is the only line, or whether it was presented
as something larger. If the latter then you work out wat the correct
line would be and answer appropriately. If not, it depends on the
interviewer what the best answer is. One possible answer would be that
you cannot have a comma operator there because the comma is being used
to separate the variables (not the most technically accurate answer, but
it could be what the interviewer wants).
 
K

Keith Thompson

int a=1,2; [...]
does it violate the constraints in the syntax of a comma operator or
in the syntax of declarations...

The syntax of declarations. An initializer must be an assignment
expression and assignment expressions do not allow the comma operator,
so the "," must be parsed as a separator.

But how do you know that's the error? There are any number of simple
transformations that could make the above line syntactically correct,
assuming it appears in the proper context:

int a=1.2; // implicit double-to-int conversion
int a=12;
int a=1;
int a=2;
int a=1<2; // , and < are on the same key on US keyboards
int a=(1,2);
int a[]={1,2};
int a=1,b=2;
int a1=2;
int a={1,2}; // a constraint violation but not a syntax error
int a=f(1,2);
a=1,2; // a statement, not a declaration
int a;
; // a null statement
// delete the entire line

I suppose if you assume that the first 5 tokens:
int a = 1 ,
are correct, then you know it's a declaration, and the error occurs
when you see the 2. But the standard doesn't specify how C code is to
be parsed, or anything about error recovery. The compiler is merely
required to distinguish between syntactically correct and
syntactically incorrect code (and to take further actions if it's
correct).

My point is that there's no basis for assuming that the comma either
was or was not intended to be a comma operator.
 
K

Keith Thompson

Richard Heathfield said:
Keith Thompson said:
Richard Heathfield said:
(e-mail address removed) said: [...]
hmm but when we are asked a question like:

in this example

int i=1,2;

what causes a syntax error... [...]
There is no comma operator in the declaration.

How do you know it's a declaration? Removing the "int" keyword
makes it a valid statement.

No it doesn't, because removing the int would mean that i is no
longer defined, making it an invalid statement.

Oh, you didn't see the declaration of i in an outer scope? :cool:}

I assumed the line was meant to be part of a larger translation
unit -- though now that I think about it, I realize that
int i=1;
by itself is a valid translation unit.
 
B

BartC

Keith said:
Richard Heathfield said:
(e-mail address removed) said: [...]
hmm but when we are asked a question like:

in this example

int i=1,2;

what causes a syntax error...
[...]
There is no comma operator in the declaration.

How do you know it's a declaration? Removing the "int" keyword makes
it a valid statement.

Or removing the extraneous space:

inti=1,2;
 
L

lawrence.jones

Keith Thompson said:
But how do you know that's the error?

Because I'm a left-to-right parser with one token of lookahead. :)
There are any number of simple
transformations that could make the above line syntactically correct,
assuming it appears in the proper context:

Absolutely correct. There's no way of knowing which grammer rule the
line was intended to match, only that it doesn't match the only one
that's possible at the point the error is detected (assuming LALR(1)).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top