NO

Author Topic: C declaration explainer  (Read 187 times)

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 1587
C declaration explainer
« on: September 17, 2017, 11:57:45 PM »
Playing around with compiler techniques I produced something that, at this stage, can be used to explain in more or less plain english the C declarations. Even those very complicate.
I.e. from a declaration as:
Code: [Select]
void (*bsd_signal(int sig, void (*func)(int)))(int);
You will get:
Code: [Select]
// 'bsd_signal' is: function having 2 parameters (int, pointer to function having 1 parameter (int) returning void) returning pointer to function having 1 parameter (int) returning void.
void (*bsd_signal(int sig, void (*func)(int )))(int );

You can use the executable with command line:
Code: [Select]
fcc -T:Ast-C test_decl.c
The file test_decl.c is a sample source included in the zip.
The executable is a based on my compiler, so the I/O use files only.
After compilation, you'll find in the directory a freshly created file named test_decl.AstOut.c (the name of input file  followed by .AstOut postfix). You can also use the -Fo switch to change output file name. I.e.:
Code: [Select]
fcc -T:Ast-C -Fo:test_out.c test_decl.c
Will create an output file named test_out.c.

Added an advanced demo with a test file that does something more that explain declarations ;)
« Last Edit: September 25, 2017, 05:34:42 PM by frankie »

Offline jack

  • Member
  • *
  • Posts: 59
Re: C declaration explainer
« Reply #1 on: September 18, 2017, 11:49:15 AM »
hello frankie
so this is a C declarations to english? could be helpful to a beginner like me.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 1587
Re: C declaration explainer
« Reply #2 on: September 18, 2017, 12:00:53 PM »
Hello Jack,
yes is the first use of a bigger project.
It will (may)be a C compiler in future.
For now generates explanation in english, and help me in the development if everybody reports the problematic declarations (so I can debug the code).
It understands also many constructs, (not the preprocessor, the switch statement, the goto, the enums and something else).
You can try something like:
Code: [Select]
int array_of_ints[10][10];
_Static_assert(sizeof (array_of_ints)  == sizeof(int)*10*10, "'array_of_ints' doesn't " "occupy" " 400 bytes!");
_Static_assert(sizeof(char)  == 1, "'char' is " "not" " equal to 1!");
_Static_assert(sizeof(short) == 2,
"'short'"
" is not"
" equal to 2!");
_Static_assert(sizeof(double) == 8, "'double' is not equal to 8!");
_Static_assert((1<<2)+1 == 2*2+1, "5" " != "
"5"
" ...");
Shows sizeof, arrays and string concatenation. Or:
Code: [Select]
typedef int INTEGER;
INTEGER int1 = 100;
float FloatVal = 0.34e-1;
float FloatHex = 0x1.99999ap-4;
typedef char * CHAR_PTR;
CHAR_PTR pChar;
int fnproto(int a, char *c, float f);
const unsigned int * restrict const * restrict const flt;
That shows constants input and qualified declarations. Or a function definition:
Code: [Select]
int fn(int a, int b)
{
typedef int MyInt;
    MyInt c = (MyInt){2};
    int d = c + 1, z = d-1+array_of_ints[1][1];
_Static_assert((1<<2)+1 == 2*2+1, "5 != 5 ...");

    if (flt == 0)
    {
        c = (MyInt)(void *)(c + d--) >> 2;
    }
    else
    {
        flt = 0;
    }

    do
    {
        c = c+1;
    }
    while (c<100);

    while (++c > c-a)
    {
        c = c - 1;
c += 2;
d |= c;
    }

    int res = (c + a) * b - 2  != 0 ? a >> (int)((3 & 1)) : (int)((b - a) % 4);
    return (c + a) * b - 2  != 0 ? a + (3 & 1) : (b - a) % 4;
    c = 3;
}
This will trigger a warning for unreachable code.
« Last Edit: September 18, 2017, 12:11:10 PM by frankie »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 1587
Re: C declaration explainer
« Reply #3 on: September 24, 2017, 11:21:42 PM »
The advanced demo (see above) shows capacities of fcc.  ;)
Preprocessor and enums are not handled yet.... will come later. For now the core...  8)

Offline jack

  • Member
  • *
  • Posts: 59
Re: C declaration explainer
« Reply #4 on: September 25, 2017, 02:03:18 AM »
thank you frankie :)