Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C grammar from http://wiki.tcl.tk/3906 fails for valid C ???

41 views
Skip to first unread message

Michael Keith

unread,
Jul 19, 2017, 10:34:27 AM7/19/17
to
Hello everybody,
the following C code cannot be parsed by the grammar mentioned above:
---
#include <stdio.h>

struct myStrT {
int i, j;
union {
char c1;
int i1;
}; // <- this seems to be the point of failure
};

struct myStrT myStruct = {11, 22, '?'};

void main(void) {
printf("myStruct: i = %d, j = %d, c1 = %c\n", myStruct.i,
myStruct.j, myStruct.c1);
}
---
It errors out with
parse error reading "SEMICOLON" in state 183 (line 8, column 7)
expecting one of:
IDENTIFIER
struct_declarator_list
COLON
LPAREN
declarator
direct_declarator
struct_declarator
pointer
MULT_STAR

The code above is valid C: It is accepted by gcc without any complaint
and prints the expected result.

I am clueless about
- either fixing the grammar
- or fixing Yeti
- or ???

Any ideas what to do will be happily accepted.
M'

kearnh

unread,
Jul 19, 2017, 12:36:47 PM7/19/17
to
As far as I can recall anonymous unions (the union is not named inside the struct) are a feature specific to gcc and not actually part of any C standard.

You need to do something like:

struct myStrT {
int i, j;
union {
char c1;
int i1;
} u;
};

and then address fields like "myStruct.u.c1"

Michael Keith

unread,
Jul 19, 2017, 1:51:21 PM7/19/17
to
On Wed, 19 Jul 2017 09:36:42 -0700 (PDT), kearnh
<theke...@gmail.com> wrote:

>On Wednesday, 19 July 2017 16:34:27 UTC+2, Michael Keith wrote:
>> Hello everybody,
>> the following C code cannot be parsed by the grammar mentioned above:
>> ---
>> #include <stdio.h>
>>
>> struct myStrT {
>> int i, j;
>> union {
>> char c1;
>> int i1;
>> }; // <- this seems to be the point of failure
>> };
>> ---
>> It errors out with
>> parse error reading "SEMICOLON" in state 183 (line 8, column 7)
>> expecting one of:
>> IDENTIFIER
>> struct_declarator_list
>> COLON
>> LPAREN
>> declarator
>> direct_declarator
>> struct_declarator
>> pointer
>> MULT_STAR

Hi Kearn,
>As far as I can recall anonymous unions (the union is not named inside the struct) are a feature specific to gcc and not actually part of any C standard.
no, my (old) Borland compiler from around 2000 accepts it, too, so I
assume it to be (at least) part of C99..

If you program hardware you often see something like
struct UART_t {
int someReg;
union {
int THR; // the register holding the character to send
int RBR; // the register which holds (buffers) the last
// char received - they are _two_ registers
// residing at the _same_ address
}
...
} *uartPtr;
so you can access all registers via a pointer with the same syntax,
like
uartPtr->someReg = 0xFF;
uartPtr->THR = '?';
recChar = uartPtr->RBR;

>struct myStrT {
> int i, j;
> union {
> char c1;
> int i1;
> } u;
>};
>
>and then address fields like "myStruct.u.c1"

yes, this works, but then the above example would have to be
uartPtr->u->THR = '?';
recChar = uartPtr->u->RBR;
or something similar and I don't think I ever had to do this.

But thanks for answering.
M'

Michael Keith

unread,
Jul 19, 2017, 2:46:20 PM7/19/17
to
On Wed, 19 Jul 2017 19:51:20 +0200, Michael Keith <mjk...@gmx.de>
wrote:


> uartPtr->u->THR = '?';
> recChar = uartPtr->u->RBR;
This looks terribly wrong and should probably be
uartPtr->u.THR = '?';
recChar = uartPtr->u.RBR;
which makes me even more certain that I never had to do this.
M'

0 new messages