0

Hi I would like to make a light weight language transformation stuff.

By "language transformation" it means transfer language into a unified form, to eliminate synthetic sugar or eliminate useless expressions. One of the example as follow in C:

// before ==================
int a[2][2];
a[0][0] = 1;
// after ===================
int a[4];
a[0] = 1;

What I'm thinking of is to use flex and bison to find target patterns to replace.

However I don't what is the normal way of doing this kind of things, thus I would like to ask and seeking advices.

Thanks a lot!!!!

Shore
  • 827
  • 9
  • 24
  • 1
    That's not very lightweight. For example, you would also need to transform `a[1][1]` to `a[3]`, which means you need to remember the dimensions of original `a`, which means you need to track variable scope, which basically means you need to implement a big chunk (or all?) of the C language grammar. – Amadan Mar 29 '22 at 09:45
  • @amadan yep, what I'm thinking of is to use an hash table to save them, and only return limited tokens and skip all other characters. The problem is that I have to figure out a grammar to make bison successfully reduce them...... – Shore Mar 30 '22 at 01:43
  • What is the purpose of this, though? If it's readability, I would say the "before" is better. If it is speed, it is almost certainly a matter of [premature optimisation](https://en.wikipedia.org/wiki/Program_optimization#When_to_optimize). If it's just for practice... Good luck, I guess? It is probably vastly beyond the scope of what you can get from a StackOverflow answer. But the best start is probably to search for "AST from C code" or such, e.g. [this](https://stackoverflow.com/questions/239722/ast-from-c-code) or [this](https://stackoverflow.com/questions/20250702/build-ast-from-c-code). – Amadan Mar 30 '22 at 02:05
  • @amadan hmmmm, my job is to transfer certain language into a form that is easier to deal with. The detail is company classified so I cannot really tell....... It is sort of a practice to me but there is commercial software who actually do this....... it will transform the original text into a form that is much easier to deal with in later procedures.... – Shore Mar 30 '22 at 04:17

0 Answers0