about null statement and declaration

about null statement and declaration

Post by hunso » Fri, 19 Mar 1999 04:00:00



#include <stdio.h>

int
main(void)
{
        int     i;;             /* XXX */
        int     another;

        i = another = 0;

        return 0;

Quote:}

=======
I know that if I compile above program, my C compiler will give me an error
message, and I know that I should not give TWO ';'s after the 'int i'.

I guess that the error message "b.c:7: parse error before `int'" is due
to the facts that 'a null statement', that is, ';', is immediately
followed by another declaration "int another;", and that in my knowledge,
declarations must precede any statements in C.

But I could not explain these things to others in the view points of the
ANSI C standard, mainly because I have never read the C standard.  So I beg
you to explain me the real meaning of the above error message ""b.c:7: parse
error before `int'"" in the words of ANSI C standard, or if possible,
by the words of the ANSI C draft.

Please consider my embarrassing situation.  I talked to my friends that
"The one who can not catch an error in the aboce C program on sight
should not be considered as a C expert."  Is there any lines that
are similar to "A statement should not be followed by a declaration"
in ANSI C standard or 'the New Testament'?

=======
Don't reply privately.  My email system has some problems Now.

 
 
 

about null statement and declaration

Post by Jens Schweikhard » Fri, 19 Mar 1999 04:00:00



# #include <stdio.h>

# int
# main(void)
# {
#         int     i;;             /* XXX */
#         int     another;

#         i = another = 0;

#         return 0;
# }

...
# But I could not explain these things to others in the view points of the
# ANSI C standard, mainly because I have never read the C standard.  So I beg
# you to explain me the real meaning of the above error message ""b.c:7: parse
# error before `int'"" in the words of ANSI C standard, or if possible,
# by the words of the ANSI C draft.

It's a matter of grammar. In ISO 9899 6.6.2 a compound statement is
defined as:

compound-statement:
        '{' declaration-list(optional)  statement-list(optional) '}'

"int i;" fits the declaration-list, the next ";" fits only statement-list.
There is no way the C grammar could allow "int another;" following.
So it's a syntax error which *has to be* diagnosed by the translator.

Tell that to your friends; they should RTFS :-)

Regards,

        Jens
--
Jens Schweikhardt  http://www.shuttle.de/schweikh/
SIGSIG -- signature too long (core dumped)

 
 
 

about null statement and declaration

Post by Elias Martenso » Fri, 19 Mar 1999 04:00:00



> #include <stdio.h>

> int
> main(void)
> {
>         int     i;;             /* XXX */
>         int     another;

>         i = another = 0;

>         return 0;
> }

> =======
> I know that if I compile above program, my C compiler will give me an error
> message, and I know that I should not give TWO ';'s after the 'int i'.

> I guess that the error message "b.c:7: parse error before `int'" is due
> to the facts that 'a null statement', that is, ';', is immediately
> followed by another declaration "int another;", and that in my knowledge,
> declarations must precede any statements in C.

> But I could not explain these things to others in the view points of the
> ANSI C standard, mainly because I have never read the C standard.  So I beg
> you to explain me the real meaning of the above error message ""b.c:7: parse
> error before `int'"" in the words of ANSI C standard, or if possible,
> by the words of the ANSI C draft.

> Please consider my embarrassing situation.  I talked to my friends that
> "The one who can not catch an error in the aboce C program on sight
> should not be considered as a C expert."  Is there any lines that
> are similar to "A statement should not be followed by a declaration"
> in ANSI C standard or 'the New Testament'?

Section 6.6.2 specifies the syntax of a block in C:

        compound-statement:
                { declaration-list[opt] statement-list[opt] }

        declaration-list:
                declaration
                declaration-list declaration

        statement-list:
                statement
                statement-list statement

The specifies that there must not be any declarations after a
statement. Now, let's take alook at the definition of a statement:

6.6.3 Expression and null statements

        Syntax

                expression-statement:
                        expression[opt] ;

        Semantics
                The expression in an expression statement is evaluated
                as a void expression for its side effects.

                A *null statement* (consisting of just a semicolon)
                performs no operations.

I think this speaks for itself. A null statement is a legal statement,
and according to 6.6.2 there may be no declarations after the first
statement.

Hope this helps.

--
Elias Martenson
elias.martenson (atsign) sweden.sun.com

 
 
 

about null statement and declaration

Post by Will Ro » Fri, 19 Mar 1999 04:00:00


: #include <stdio.h>

: int
: main(void)
: {
:         int     i;;             /* XXX */
:         int     another;
:         i = another = 0;
:         return 0;
: }

: =======
: I know that if I compile above program, my C compiler will give me an error
: message, and I know that I should not give TWO ';'s after the 'int i'.

: I guess that the error message "b.c:7: parse error before `int'" is due
: to the facts that 'a null statement', that is, ';', is immediately
: followed by another declaration "int another;", and that in my knowledge,
: declarations must precede any statements in C.

Yes.  That's why 'int main(void) { int i; ; i = 0; return 0; }' will
work, or at least not produce complaints.

Will

 
 
 

about null statement and declaration

Post by Clive D.W. Feathe » Fri, 19 Mar 1999 04:00:00




Quote:>        int     i;;             /* XXX */
>        int     another;
>I guess that the error message "b.c:7: parse error before `int'" is due
>to the facts that 'a null statement', that is, ';', is immediately
>followed by another declaration "int another;", and that in my knowledge,
>declarations must precede any statements in C.

Right. There's no way to generate that sequence of tokens from the
grammar. The exact way in which your compiler detects - and reports -
this depends on how it's implemented.

For example, a top-down compiler will get to the second "int" and find
there's no construct that can begin with it. So it reports something
like "parse error" or "'int' not valid in this context". A bottom-up
compiler will convert this into:

    { declaration statement declaration }

and then find no construct that matches this, so it will say something
like "statement cannot be followed by declaration".

Quote:>But I could not explain these things to others in the view points of the
>ANSI C standard, mainly because I have never read the C standard.  So I beg
>you to explain me the real meaning of the above error message ""b.c:7: parse
>error before `int'"" in the words of ANSI C standard, or if possible,
>by the words of the ANSI C draft.

They don't mean anything in the terms of the Standard. The Standard says
something like (I don't have it to hand):

    block:
        { dec-list-opt stat-list-opt }

    dec-list:
        declaration
        dec-list declaration

    stat-list:
        statement
        stat-list statement

There is no way to have a statement followed by a declaration using
those rules.

The phrase "parse error" usually means "you have a sequence of tokens
that can't be generated by the syntax in the Standard".

Quote:>Is there any lines that
>are similar to "A statement should not be followed by a declaration"
>in ANSI C standard

The Standard does it using the above grammar.

The point is moot in C9X, because we *do* now allow statements and
declarations to be randomly mixed.

--


Fax: +44 181 371 1037 | Demon Internet Ltd.    | Web:  <http://www.davros.org>
Written on my laptop; please observe the Reply-To address

 
 
 

about null statement and declaration

Post by Norman Diamo » Sat, 20 Mar 1999 04:00:00





>>        int     i;;             /* XXX */
>>        int     another;

>The point is moot in C9X, because we *do* now allow statements and
>declarations to be randomly mixed.

Oddly, this was an existing practice in pre-standard days too.
If a statement other than a null statement were followed by a
declaration then an error message was likely, and obviously was
correct.  But if a null statement were present in the middle of
otherwise legal declarations then an error message was unlikely,
at least in my experience.  I couldn't figure out if this was
intentional or not.

Of course C89 conforming implementations couldn't keep silent about it.

--
 <<  If this were the company's opinion, I would not be allowed to post it.  >>
"I paid money for this car, I pay taxes for vehicle registration and a driver's
license, so I can drive in any lane I want, and no innocent victim gets to call
the cops just 'cause the lane's not goin' the same direction as me" - J Spammer

 
 
 

1. Null statement?

Debugging a program, I found this line:

(s_tag,' ',3);

It should have read

memset(s_tag,' ',3);

but did not... and the sucker compiled.  We're trying to figure out why.
What did the compiler see this as?  A null statement?

TIA,

+============================================================================+


| AOL: Mountain                                                              |
| WWW: http://www.sover.net/~mountain/      "Fight, Win, Prevail!"           |
+============================================================================+

2. src.rpm unpacker for win32

3. Help with if then statement and loop statement

4. kprobes for 2.5.45

5. Matrox Mystique ands X.

6. linking mistery

7. suspect list_empty( {NULL, NULL} )

8. Problems With XF86Configuration

9. HELP: 2>&1 > /dev/null != 2>&- > /dev/null ???

10. pccardd[49]: No card in database for "(null)"("(null)")

11. setvbuf (logfile, NULL, _IOLBF, NULL) in a separate function

12. cp /dev/null or cat /dev/null

13. PROBLEM: ioremap returning NULL and non-NULL for the same high memory adresses