Change native C command names and compiler messages

101 Views Asked by At

How can I be able to write C code with different command names instead of the existing, and also to get different compiling warning, errors etc?

For example, replace the 'while' loop declaration with 'foobar', so that:

foobar (i<10) i++;

will act as:

while (i<10) i++;

and to replace a compilation error message from:

prog.c:4:2: error: 'i' undeclared

to:

prog.c:4:2: banana kiwi: 'i' orange apple tomato

A use case for this is making C based on a human-language other than english.

2

There are 2 best solutions below

2
luser droog On

For the input language, you can use macros to define alternate names for things.

#define foobar int

Or better to use typedef for types.

typedef int foobar;

Neither of these removes int from the namespace.

If the compiler is made using a grammar file and a compiler-compiler, then you may be able to edit the keywords in the grammar to change the names without introducing new names. This will be the .yacc or .bison or .antlr or sthg file.

For the error messages, those are going to be baked-in to the compiler or in a strings or config file. You'll have to edit those files, or better copy into new locale-variant and modify.

Or run the compiler with a filter that replaces error with banana kiwi. sed, awk, m4 -- many common tools for substitutions.

1
Puck On

To give an alias to a type you can use typedef:

typedef int foobar;
foobar i; // is a int

About the error message, it's more complicated and is inside the compiler code...