C language > Work in progress

C declaration explainer

(1/2) > >>

frankie:
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: ---void (*bsd_signal(int sig, void (*func)(int)))(int);

--- End code ---
You will get:

--- Code: ---// '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 );

--- End code ---

You can use the executable with command line:

--- Code: ---fcc -T:Ast-C test_decl.c

--- End code ---
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: ---fcc -T:Ast-C -Fo:test_out.c test_decl.c

--- End code ---
Will create an output file named test_out.c.

Added an advanced demo with a test file that does something more that explain declarations ;)

jack:
hello frankie
so this is a C declarations to english? could be helpful to a beginner like me.

frankie:
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: ---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"
" ...");

--- End code ---
Shows sizeof, arrays and string concatenation. Or:

--- Code: ---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;

--- End code ---
That shows constants input and qualified declarations. Or a function definition:

--- Code: ---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;
}

--- End code ---
This will trigger a warning for unreachable code.

frankie:
The advanced demo (see above) shows capacities of fcc.  ;)
Preprocessor and enums are not handled yet.... will come later. For now the core...  8)

jack:
thank you frankie :)

Navigation

[0] Message Index

[#] Next page

Go to full version