Re: question about linker

Liste des GroupesRevenir à cl c  
Sujet : Re: question about linker
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.c
Date : 14. Dec 2024, 22:34:38
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vjktle$5sfr$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
User-Agent : Mozilla Thunderbird
On 14/12/2024 20:17, Waldek Hebisch wrote:
Bart <bc@freeuk.com> wrote:
>
OK, if it's so simple, explain it to me.
>
Apparently the first line here needs a semicolon after }, the second
doesn't:
>
   int X[1] = {0};
   void Y() {}
>
Similarly here:
>
   if (x) y;
   if (x) {}
>
Why?
>
"Because that's what the grammar says" isn't a valid answer.
 In first approximation, for some constructs it is clear where
the end is, so no need for semicolon, for some other semicolon
is needed to mark the end.  This rule does no work in all cases,
for example 'continue' and 'break' are keywords and defining
corresponding statements without semicolon would lead to
no ambiguity.  Similarly in case of 'goto'.  'return' is
more tricky, but is seems that one could define it without
semicolon.  But here probably consistency won: 'break',
'contunue', 'goto' statement, 'return' statement are perceived
as simple statements and are covered by informal rule
"simple statement needs terminationg semicolon".  '{}'
is a compound statement, hence not a simple statement.
Similarly, 'if' is a complex statement, and needs no semicolon
on its own.  As you noted without terminating semicolon do-while
loop would be ambigious, so it needs semicolon.  I do
not have example for declarations, but I suspect that defining
then without semicolon would be ambigious.  Function
_definition_ is anambigious without terminating semicolon,
so why put is there.
For the same reason that f(1 2 3) might be unambiguous, as usually it's written f(1, 2, 3).
Actually, since C likes to declare/define lists of things where other languages don't allow them, such as:
  typedef int T, U, *V;
  struct tag {int x;} a, b, c;
  enum {red, green} blue, yellow;
I couldn't quite see why you can't do the same with functions:
  int F(void){return 1;}, G(void){return 2;}

 Concerning "grammar says it", grammar for C90 from which one
can generate working parser has 74 nonterminals.  You could
change some rules and still get working parser for a different
language.  So in this sense part of grammar is purely
arbitrary.  But other changes would lead to grammar that
fails to work.  If you look at rules you will see
substantial similarities between some rules, so grammar
is really simpler than what size alone would would suggest.
So, having working and sane (that is relatively simple)
grammar puts restrictions on the language, some changes
simply do not fit.  Some changes would lead to completely
different language, that was not an option for C, as
very first versions were intentionaly similar to earlier
languages and later there was a body of existing programs
and programmers.
 You write about confusion.  I think that what you present
grammarians would call "garden paths", that is perceiving/
trying to make up different rules than grammar rules.  In
your 'if' example you ignore simple thing: 'if' needs no
terminator on its own.
Now that you mention it, why not? In:
   if (c) s1; else s2;
s1 and s2 are statements, but so is the whole if-else construct; why doesn't that need its own terminator?
This is legal in C: {}{}{}{}.
As I said, I wouldn't be able to explain it.

It is null statement that needs
terminating semicolon, and "empty" compound statement that
does not need a terminator.  Null statement and compound
statement are quite different, in particular without
semicolon you would not know that null statement is there,
Identifying the end of /some/ statements shouldn't mean not needing terminators in those cases. It needs to be a consistent rule.

while compound statement can be easily recognized without
need for terminator.  There is no special rule for "if with
null statement", if it were you would get needlessly complex
grammar.
 In the first pair, one line is a declaration, another is
function definition.  Again, quite different constructs,
one needing terminator, other not needing it.
 Garden paths are common in natural language and people
cope quite well.  So for normal (even beginer)
programmers garden paths are not a real problem: you get
confused once, learn the right way and go on.  In many
cases learning is unconcious, you simply get used to
the way code is written, and when you make mistakes
compiler tells you that there is an error, so you
correct it.
But the exact rules remain fuzzy.
I wondered if my syntaxes are any better. There, first it is necessary to consider programs written on one line, since auto-semicolon-insertion would confuse matters.
Then, notionally, declarations, definitions, expressions and statements are all separated with a semicolon.
In some cases, because I haven't been rigorous, one can be missed out, and it still works. But that would be an oversight in the parser. (And usually, the two parts would be separated by a newline, which turns into a semicolon.)
The rule in the language (and in the grammar if I had one) is clear:
   if c then s1 else s2 fi
Here no ";" are needed, as at each level of statement, there is only one expression or statement; nothing to separate. Here:
   if c;d then s1;s2 else s2;s3 fi; if e then s4 fi
that ";" in "fi;" is needed to separate this statement from the next, even though you could probably figure out that the statement ends there.
(Note the "c;d" is not an error.) Another example with function defs, although I never write them like this:
   proc F = end; proc G = end
So, in this language, it is clear and consistent. Explaining why the multi-line version can leave the semicolons out is harder:
     if c
        d
     then
         s1
         s2
     else
         s2
         s3
     fi
     if
         e
     then
         s4
     fi
(If I was going to attempt a formal grammar, I'd try and make semicolon injection part of the lexer. Then the parser would always see the semicolons it needs.)

Date Sujet#  Auteur
26 Nov 24 * question about linker382Thiago Adams
26 Nov 24 +* Re: question about linker16Thiago Adams
26 Nov 24 i`* Re: question about linker15Bart
26 Nov 24 i `* Re: question about linker14Thiago Adams
27 Nov 24 i  +* Re: question about linker2BGB
27 Nov 24 i  i`- Re: question about linker1Bart
27 Nov 24 i  +* Re: question about linker5David Brown
27 Nov 24 i  i`* Re: question about linker4Thiago Adams
27 Nov 24 i  i +* Re: question about linker2David Brown
27 Nov 24 i  i i`- Re: question about linker1Thiago Adams
2 Dec 24 i  i `- Re: question about linker1BGB
27 Nov 24 i  `* Re: question about linker6Michael S
27 Nov 24 i   `* Re: question about linker5Thiago Adams
27 Nov 24 i    `* Re: question about linker4Michael S
27 Nov 24 i     +- Re: question about linker1David Brown
28 Nov 24 i     +- Re: question about linker1Tim Rentsch
2 Dec 24 i     `- Re: question about linker1BGB
26 Nov 24 +* Re: question about linker20Bart
26 Nov 24 i`* Re: question about linker19Thiago Adams
26 Nov 24 i `* Re: question about linker18Bart
27 Nov 24 i  +* Re: question about linker3BGB
27 Nov 24 i  i`* Re: question about linker2fir
27 Nov 24 i  i `- Re: question about linker1BGB
27 Nov 24 i  `* Re: question about linker14Bart
27 Nov 24 i   +* Re: question about linker12Thiago Adams
27 Nov 24 i   i+- Re: question about linker1Thiago Adams
27 Nov 24 i   i`* Re: question about linker10Bart
27 Nov 24 i   i +* Re: question about linker6Bart
27 Nov 24 i   i i`* Re: question about linker5Thiago Adams
27 Nov 24 i   i i +* Re: question about linker3Thiago Adams
27 Nov 24 i   i i i`* Re: question about linker2Thiago Adams
27 Nov 24 i   i i i `- Re: question about linker1Bart
27 Nov 24 i   i i `- Re: question about linker1Bart
27 Nov 24 i   i `* Re: question about linker3Thiago Adams
27 Nov 24 i   i  `* Re: question about linker2Bart
27 Nov 24 i   i   `- Re: question about linker1Thiago Adams
28 Nov 24 i   `- Re: question about linker1Tim Rentsch
27 Nov 24 `* Re: question about linker345Waldek Hebisch
27 Nov 24  `* Re: question about linker344Thiago Adams
28 Nov 24   `* Re: question about linker343Keith Thompson
28 Nov 24    `* Re: question about linker342Thiago Adams
28 Nov 24     +* Re: question about linker337Bart
28 Nov 24     i`* Re: question about linker336Keith Thompson
29 Nov 24     i `* Re: question about linker335Bart
29 Nov 24     i  `* Re: question about linker334Keith Thompson
29 Nov 24     i   `* Re: question about linker333Bart
29 Nov 24     i    +* Re: question about linker3Keith Thompson
29 Nov 24     i    i`* Re: question about linker2Bart
29 Nov 24     i    i `- Re: question about linker1Keith Thompson
29 Nov 24     i    `* Re: question about linker329David Brown
29 Nov 24     i     `* Re: question about linker328Bart
29 Nov 24     i      +- Re: question about linker1Ike Naar
29 Nov 24     i      +* Re: question about linker325Michael S
29 Nov 24     i      i+* Re: question about linker322Bart
29 Nov 24     i      ii`* Re: question about linker321Michael S
29 Nov 24     i      ii +* Re: question about linker319David Brown
29 Nov 24     i      ii i`* Re: question about linker318Bart
29 Nov 24     i      ii i +* Re: question about linker164Keith Thompson
29 Nov 24     i      ii i i`* Re: question about linker163Bart
30 Nov 24     i      ii i i `* Re: question about linker162Keith Thompson
30 Nov 24     i      ii i i  +* Re: question about linker95Waldek Hebisch
30 Nov 24     i      ii i i  i+- Re: question about linker1Keith Thompson
30 Nov 24     i      ii i i  i+* Re: question about linker3James Kuyper
30 Nov 24     i      ii i i  ii`* Re: question about linker2Michael S
3 Dec 24     i      ii i i  ii `- Re: question about linker1Tim Rentsch
1 Dec 24     i      ii i i  i`* Re: question about linker90David Brown
1 Dec 24     i      ii i i  i +* Re: question about linker88Bart
1 Dec 24     i      ii i i  i i`* Re: question about linker87David Brown
1 Dec 24     i      ii i i  i i `* Re: question about linker86Bart
2 Dec 24     i      ii i i  i i  `* Re: question about linker85David Brown
2 Dec 24     i      ii i i  i i   `* Re: question about linker84Bart
2 Dec 24     i      ii i i  i i    +* Re: question about linker78David Brown
2 Dec 24     i      ii i i  i i    i+* Re: question about linker72Janis Papanagnou
2 Dec 24     i      ii i i  i i    ii+* Re: question about linker70Bart
2 Dec 24     i      ii i i  i i    iii+* Re: question about linker68David Brown
2 Dec 24     i      ii i i  i i    iiii`* Re: question about linker67Bart
3 Dec 24     i      ii i i  i i    iiii `* Re: question about linker66David Brown
3 Dec 24     i      ii i i  i i    iiii  +* Re: question about linker53Bart
3 Dec 24     i      ii i i  i i    iiii  i`* Re: question about linker52David Brown
3 Dec 24     i      ii i i  i i    iiii  i `* Re: question about linker51Bart
4 Dec 24     i      ii i i  i i    iiii  i  `* Re: question about linker50David Brown
4 Dec 24     i      ii i i  i i    iiii  i   `* Re: question about linker49Bart
4 Dec 24     i      ii i i  i i    iiii  i    `* Re: question about linker48David Brown
4 Dec 24     i      ii i i  i i    iiii  i     +* Re: question about linker24Bart
5 Dec 24     i      ii i i  i i    iiii  i     i`* Re: question about linker23David Brown
5 Dec 24     i      ii i i  i i    iiii  i     i +- Re: question about linker1Janis Papanagnou
5 Dec 24     i      ii i i  i i    iiii  i     i `* Re: question about linker21Bart
6 Dec 24     i      ii i i  i i    iiii  i     i  `* Re: question about linker20David Brown
6 Dec 24     i      ii i i  i i    iiii  i     i   `* Re: question about linker19Bart
6 Dec 24     i      ii i i  i i    iiii  i     i    +* Re: question about linker5Ike Naar
6 Dec 24     i      ii i i  i i    iiii  i     i    i+- Re: question about linker1Bart
7 Dec 24     i      ii i i  i i    iiii  i     i    i+- Re: question about linker1Keith Thompson
7 Dec 24     i      ii i i  i i    iiii  i     i    i`* Re: question about linker2Bart
7 Dec 24     i      ii i i  i i    iiii  i     i    i `- Re: question about linker1Keith Thompson
7 Dec 24     i      ii i i  i i    iiii  i     i    +* Re: question about linker10David Brown
7 Dec 24     i      ii i i  i i    iiii  i     i    i`* Re: question about linker9Bart
7 Dec 24     i      ii i i  i i    iiii  i     i    i `* Re: question about linker8David Brown
7 Dec 24     i      ii i i  i i    iiii  i     i    i  `* Re: question about linker7Bart
7 Dec 24     i      ii i i  i i    iiii  i     i    i   `* Re: question about linker6David Brown
7 Dec 24     i      ii i i  i i    iiii  i     i    i    `* Re: question about linker5Bart
8 Dec 24     i      ii i i  i i    iiii  i     i    i     +* Re: question about linker3Ben Bacarisse
8 Dec 24     i      ii i i  i i    iiii  i     i    i     `- Re: question about linker1David Brown
8 Dec 24     i      ii i i  i i    iiii  i     i    `* Re: question about linker3Waldek Hebisch
5 Dec 24     i      ii i i  i i    iiii  i     +* Re: question about linker15Waldek Hebisch
11 Dec 24     i      ii i i  i i    iiii  i     `* Re: question about linker8James Kuyper
3 Dec 24     i      ii i i  i i    iiii  `* Re: question about linker12Bart
3 Dec 24     i      ii i i  i i    iii`- Re: question about linker1Janis Papanagnou
2 Dec 24     i      ii i i  i i    ii`- Re: question about linker1Bart
2 Dec 24     i      ii i i  i i    i`* Re: question about linker5Bart
4 Dec 24     i      ii i i  i i    `* Re: question about linker5Waldek Hebisch
1 Dec 24     i      ii i i  i `- Re: question about linker1Janis Papanagnou
30 Nov 24     i      ii i i  +* Re: question about linker44Bart
30 Nov 24     i      ii i i  +- Re: question about linker1Janis Papanagnou
1 Dec 24     i      ii i i  `* Re: question about linker21David Brown
30 Nov 24     i      ii i `* Re: question about linker153David Brown
5 Dec 24     i      ii `- Re: question about linker1Tim Rentsch
30 Nov 24     i      i`* Re: question about linker2Tim Rentsch
29 Nov 24     i      `- Re: question about linker1David Brown
28 Nov 24     `* Re: question about linker4Keith Thompson

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal