August 9th, 2003, 10:36 AM
C code generator
hi,i'm a beginner in C.i get a project to build a C code generator from Z specification. I should be using C to build it. but i got no experience how it sholud be done. I'm so blur tll i dunno where should I start. hope someone can help me out. thanx alot.
August 9th, 2003, 10:38 AM
Learn C first, then come back to it.
August 9th, 2003, 10:44 AM
well, thanx for tat suggestion. well, i got some background in C. just i dunno how to build a language translator. anyone build it b4?
August 9th, 2003, 10:59 AM
Basically, you need to know how to parse text, like a compiler does. You needn't know anything about machine instructions, or anything, but this is a pretty complex job, as is. In university, I took a 3rd year course that dealt specifically with techniques of translators for programming languages, preprocessors, compilers, interpreters and assemblers. Not that it is that hard - but normally things covered in a 3rd year course is not a job for someone who barely knows C.
August 9th, 2003, 11:23 AM
I've built one before. It's not a task for someone who describes themself as a beginner in C.
August 10th, 2003, 02:29 AM
i know its not easy to build a translator. but i got no choice . the project is posted to me, i cant do anything except to learn how to do it. so u mind to teach me?
August 11th, 2003, 09:08 AM
hi, i really need from guideline froem someone about this C generator. hope someone can help
August 11th, 2003, 09:14 AM
As Jason stated, this boils down to parsing text. Presumably you'd be reading in a text file, then you need to break down each word and determine if it is a keyword, variable, etc. You'll basically need a ruleset to follow to examine the words to make sure that they are valid syntax. There is actually more to do here that just this. This is actually a quite complex task but it all boils down to parsing and analyzing the text. This is where you need to start at.
August 11th, 2003, 02:18 PM
there's some code in k&r's c programming book at the end of chapter 5 that would be a useful start for parsing c source text. the code's called dcl. it helps you deal with complicated declerations by taking the complicated decleration (or not so complicated decleration) and outputs english-ish phrases from it.
so it takes say:
comp: pointer to function returning void
obviously the english output wouldn't be so useful but in order to give english output it needs to split it into it's elements and check it etc. it's a pretty complicated bit of code as it operates recursively. also the one in the book is severely limited as it doesn't handle arguments or defaults and probably other stuff. - those bits are left to exercises and i still haven't completely got it sussed. attempting that exercise will give you an idea of how hard it'll be to cover the whole c language. i've found it one of the most toughest exercises in the book so far.
a totally different approach maybe to use regular expressions. i personally find regex brilliant for any kind of information extraction. very powerfull. just an alternative suggestion. but regex will only help you split it up into parts, and i think it might be the structuring and checking of the structure that could be the hardest part. i don't though.
Last edited by balance; August 11th, 2003 at 02:24 PM.
August 13th, 2003, 10:17 AM
Lets see some Z specifications
I presume your specifications are well documented and there is a way to test them for correctness? If not you have another nightmare of parsing and validating all the data in your specification. I do not think you have a trivial job on your hands and I am willing to bet it will be many months before you have something that works reliably and correctly. My approach would be to validate all your tokens in your specification files first by loading them into some sort of structure and comparing each part of each token against some database of known structures (unless you are required to use C, there are lots of (relatively) easy ways to do so in C++ using the STL). Once you know you have valid input data (from your spec), then it is a somewhat strait forward process to translate each spec token to an equivalent C token (which may not be 1:1, meaning a single token from the spec may wind up being many lines of C code).
If you are successful with this you should expect a massive raise!