#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    39
    Rep Power
    12

    Question 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.
  2. #2
  3. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Learn C first, then come back to it.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    39
    Rep Power
    12
    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?
  6. #4
  7. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    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.
  8. #5
  9. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Originally posted by childish_gal
    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?
    I've built one before. It's not a task for someone who describes themself as a beginner in C.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    39
    Rep Power
    12
    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?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    39
    Rep Power
    12
    hi, i really need from guideline froem someone about this C generator. hope someone can help
  14. #8
  15. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    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.
  16. #9
  17. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    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:

    void *comp()

    and outputs:

    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.
  18. #10
  19. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244

    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!

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo