Help and ideas with C-like to C transformation tool (long) (Corey Stup)
27 Dec 2001 00:15:31 -0500

          From comp.compilers

Related articles
Help and ideas with C-like to C transformation tool (long) (2001-12-27)
Re: Help and ideas with C-like to C transformation tool (long) (John W. Millaway) (2001-12-29)
Re: Help and ideas with C-like to C transformation tool (long) (2001-12-29)
Re: Help and ideas with C-like to C transformation tool (long) (Roberto Waltman) (2001-12-29)
Re: Help and ideas with C-like to C transformation tool (long) (Ira D. Baxter) (2001-12-29)
Re: Help and ideas with C-like to C transformation tool (long) (2001-12-29)
Re: Help and ideas with C-like to C transformation tool (Ralph Corderoy) (2001-12-29)
| List of all articles for this month |

From: (Corey Stup)
Newsgroups: comp.compilers
Date: 27 Dec 2001 00:15:31 -0500
Organization: Posted via Supernews,
Keywords: tools, question
Posted-Date: 27 Dec 2001 00:15:30 EST

We have a legacy language, which is very C-like, but with some added
features such as type'd defines, library function overloading and some
other features. The "translator" was written in lex/yacc under Unix,
and has a complete C-like grammar defined. It outputs valid C code
that then is compiled by a commercial C compiler. This translator was
built to do complete symbol processing, error handling and the like,
so that it would be the module reporting errors in syntax and usage,
and NOT the C compiler underneath.

However, this is a huge limitation. The language as written does not
allow for any standard C datatypes (only our "built-in" ones) or
structures, new function calls are not easily implemented, etc.

Anyway, what I want to do with the tool is modify it, or make
something new that is simpler. I don't want to verify all constructs
(letting the C compiler do that work after its passed off..), but I
still need its macro, shorthand, and some of its symbol table

For instance, some defines in our language:
def standard_define 0
defn number_define 1
defcs charstring_define 2

If you are to use "standard_define" anywhere, it does a simple
replace, just as a #define would do in C. However, the other two
carry type information with the symbols, such that referencing
"number_define" knows that its defining a numeric type, and
"charstring_define" knows that its a character string type. Using the
latter two symbols in a function call would generate a different C
runtime-function call to be output to the C code. (our version of
overloading) So, our tool will still need to do this type of analysis.

What I *DON'T* want it to need to understand is valid "" or
"if..then..else" syntax. I'd just like any and all constructs to just
be passed through and have the C compiler do its work to validate the

For another example, we have a rule for simple assignments. To
reference a variable in our persistant store, you use a macro notation
that gets transformed in the C-output based on if its a load or store
operation. So in our language: (where the 'a' refers to a "file
pointer", and the "n" refers to a datatype of "number")

Which means, set numeric array index 1 with the numeric value of array
index 0, gets transformed into the C code as:


Due to needing to know context to know if the action is a store or
fetch, one can not just use 'm4' or a simple macro processor to do the
pre-processing - one seems to need to know the context of the
expression in the statement to do the transformation correctly.
Numbers are the simple form, but there are more complexities if the
data types being assigned are character strings, etc.

A while-do construct in our language would be written as:
WHILE an(standard_define)=TRUE DO
          /* */

This gets translated into:
while (fetchnumber(a,0)==TRUE) {

Which then is compiled by a standard C compiler.

I guess what I'm getting at is this:
Is it possible to define a parser that can find expressions such as
"an(0)" or "an(standard_define)" and replace those, no matter where
they are in a stream, without understanding the entire language
syntax? I don't want my transformation tool to understand: "while
(an(0)==0) { foo(); }" so that it validates that the while statement
fits a standard grammar. But I do want it to be able to see the
an(0), and since its not an assignment, its a fetch, and replace it
with the standard "fetchnumber(0)" library call. At first glance, it
seems that what I want might be as simple as just outputting to the
destination file anything that doesn't match our patterns that we do
wish to modify, but I fear that I'm overlooking something.

If I have to, I will add in the standard C grammar to our
transformation tool as others have built lex/yacc versions that
should integrate fine. However, this is a last resort, as I'd like to
make this tool as SIMPLE as possible and leverage the most I can from
the C compilers that have already been written and tested.

If anyone has any suggestions on a toolset to perform this type of
task, please email or post followups to this post. I can provide more
examples if anyone is interested.

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.