Click here to Skip to main content
15,904,415 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Can anyone please tell me the advantage of using C as the target language of a source to source compiler?
Thanks.
Posted
Comments
Andreas Gieriet 27-Dec-12 22:48pm    
What is a source to source compiler?
Is it e.g. C# --> Java via C?
Or is it simple some language (e.g. C#) to C?
Andi

Assuming you talk about a "compiler" that uses C as a backend language (like the early C++ compilers did), then there are several good reasons for this:

  1. C compilers are available for any platform -> your language is available on any platform and architecture where C is available
  2. C compilers can agressively optimize -> you do not have to care any register allocation, common sub-expression elimination, loop unrolling, etc.
  3. C language is easier to understand than any "proprietary" intermediate language


The cons are the additional compilation step for the C compiler. But that is easily balanced for a student project by the above mentioned benefits.

Cheers
Andi
 
Share this answer
 
Comments
nv3 28-Dec-12 17:23pm    
I think you hit the nail, Andreas. This is the first answer that interprets the term "target language" as the output of the tool in discussion. And the reasons you gave are good ones. There is actually a fourth one: In some cases it is a lot easier to write a compiler that outputs C than to do a full implementation of a stand-alone compiler, thus saving to write an optimizer and code generator. Get's my 5.
Andreas Gieriet 28-Dec-12 17:34pm    
Thanks for your 5!
Cheers
Andi
suleman115 29-Dec-12 23:13pm    
Thank you Andreas and nv3:)
No. language translation (more in case if the machine languages, than in case of spoken ones) is a complicated text manipulation. Indifferent of what language you use, you will need text processing tools like regular expression, lexers, dictionaries and so on. These are not the highlights of the plain old C. Of course, using C you will make a small and probably fast tool - if you don't mess it up, which can have a higher likelihood with C, than with C# for example, if you have to make your text processing tools on your own. So in general I would note recommend C for that as first approach. The same logic in C# would be a smaller and maintainable code, faster developement, but larger footprint in resource consumption, and would probably run much slower.
But that's only one opinion.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-May-13 13:53pm    
5ed.
—SA
rdlph 8-Oct-18 22:03pm    
this does not address the answer correctly. The topic is the 'target language' of a compiler: the language in which code emitted from the compiler is encoded. Not the language in which the compiler itself is written. You would not need text processing tools in the target language, you would need them in the 'source' language; the one the compiler is actually written in. This is a category error.
C is a fairly simple language in terms of structure and syntax. Unfortunately it also lacks many of the features that are useful in modern development.
 
Share this answer
 
Comments
rdlph 8-Oct-18 21:57pm    
This answer does not address the question correctly. The question is about the target language (what is emitted during compilation) of a compiler. Not the implementation language; the one used to actually write the compiler itself.
Richard MacCutchan 9-Oct-18 3:56am    
Why did you wait six years just to make this rather pointless comment?
rdlph 9-Oct-18 9:11am    
What is 'rather pointless' about it? And what does the age of the answer have to do with anything? I assure you I didn't wait 6 years to make this comment. I made it immediately after reading your answer.
Richard MacCutchan 9-Oct-18 11:23am    
Well you must have searched jolly hard to find a six year old question, that no one (even the OP) has any interest in. The person who posted it has not been active here for years, and probably did nothing about the information provided. So, as I say, a rather pointless comment.
Thank you for your answers, actually it is an exercise in my subject compilers. So i needed to know some reasonable answer. What i can think as an answer is, if someone needs a code in C but doesn't know it, he can use this translator to code in some other known language and translate it. But this answer I consider childish. What do you think?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-May-13 13:52pm    
Not an answer.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900