Solved

converting c code to pascal code

Posted on 2003-10-31
16
333 Views
Last Modified: 2010-04-05
I am looking for a good converter which translate "K&R C source code" to Pascal code
0
Comment
Question by:sozgen
  • 5
  • 3
  • 3
16 Comments
 
LVL 5

Accepted Solution

by:
DeerBear earned 20 total points
ID: 9661699
Hi,

LOL!

That's definitely not a 20 points task :-)
Even 500 are a bit too low :-)

The best thing I can do for you is to try and introduce you to some
nasty tips&tricks to help you translating code.

This is a very small primer on the topic.

Let's see some procedure declarations:

void MyRoutine( Parameters );

This is a procedure. "Void" in C means it's "empty" thus no value.

void *MyRoutine( Parameters );

This is a function. "Void *" in C means "Pointer to empty", thus an
untyped pointer.

Generally speaking, when you see an * near an identifier, that is a
pointer.

char * = PChar = lpsz

The former is a very common equation to make. The last form is
especially used in Windows APIs.

char []

This is an array, but beware: arrays in C are the same as pointers,
thus you can treat a pointer as an array and viceversa.
Keep this in mind when translating code.

typedef
  ( void * )( int Function )

This is nasty. This is a function pointer, much like:

Type
   TFunc = function : Integer;

Well, these are the usual pitfalls.

HTH,

Andrew
0
 
LVL 2

Expert Comment

by:DeNavigator
ID: 9662407
Try a search on Google with c2pas. One of the results leads to this http://www.programmersheaven.com/zone24/cat277/4171.htm

I have not tested the mentioned utility, but you could give it a try!

0
 
LVL 6

Expert Comment

by:swift99
ID: 9663136
I saw one of these about 15 years back in a Borland magazine.  You might look at Borland's support database.

It's about 8 hours of work to write and deobug the parser if you're already familiar with parsers, and another 8 hours of work to take the parsed tree and write it back out in C instead of Pascal and debug that.

Simple string substitution will handle most of it ...

Pascal   C
begin    {
end      }
:=       =
=        ==
void     procedure
int       integer
double real
record  struct
(*       /*
{         /*
}         */
*)       */

and so on

What cannot be handled by simple string substitution is:
*   pointer dereferencing (always an issue in C programming)
*   "for" loop constructs
*   places where C syntax call for different order than Pascal sequence.

The latter two pieces are trivial in the sense that the compiler will let you know about them.  The former is a pain in the <insert descripter here>.

I would probably start by writing a set of general translator classes without implementations, then write the Pascal reader to build a parsed tree, and a C writer to print the parsed tree with C constructs.  At this point it becomes trivial to reverse the process and write a two way translator.

If I were point starved, I might sit down and do this for the 500 points suggested by DeerBear because it sounds fun.  I'd certainly be willing to help you build one by answering question sand offering a bit of advice just because it does sound fun, although you would have to do all of the work.  I'm not starved for points, so increasing the point count won't get much more from me than advice on this one.

Initially, these are the classes you need.  All are descended from TObject.

TTranslator
TCodeReader
TParsedTree
TCodeWriter

Once these general LANGUAGE INDEPENDENT classes are defined, you need to descend a TPascalCodeReader and a TCCodeWriter.  The TParsedTree will be language independent so it does not need descendents.  

The TTranslator should be and independent wrapper that has a TCodeReader, TParsedTree, and TCodeWriter component, so it should also need no descendents.  

The TParsedTree is actually the most complex piece, although it is primarily a data object.  It implements the recursive structure that is defines a "module" in the abstract sense in any programming language.

I would recommend downloading dUnit at thispoint in time from sourceforge, since you will need to get the parsed tree working before you can really get started on the specific code parsers or code writers.
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 9669917
Hi Swift99,

I saw at least a flaw in your table:

void can, yeah, be used for procedure, but also for pointer in the form void* .

HTH,

Andrew
0
 
LVL 6

Expert Comment

by:swift99
ID: 9670973
Yep ... hence the need for something more robust than string substitution is this is not a one-shot conversion.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Expert Comment

by:DeNavigator
ID: 12547277
Swift99 was the first to give some samples, the only thing DeerBear has added was pointing at a minor flaw
So Swift99 should be accepted, I don't care about the points ;)
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 12547398
Hi DeNavigator,

Please check again *all* the messages :-)

I don't usually respond to these posts, but your inaccuracy this time yelded my reply.
Please *do* check again *all* the posts.

Cheers,

Andrew
0
 
LVL 6

Expert Comment

by:swift99
ID: 12547654
DeerBear explained the principles and provided examples of the manual process.  I provided a rough outline of automating the process, but put no work into it.

I concur that DeerBear deserves the points.
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 12548352
kacor,

Please keep in mind that nobody is under accusation.
I replied simply because it was stated that I only provided a small correction and nothing else.
This was simply not true, and wanting correction.

As for the points, I think nobody of us is really concerned about earning them.
Speaking for me, my motivation is helping out other people and if I can get some points, the better :-)

I just replied because I felt DeNavigator message to be inaccurate to say the least.
Cheers,

Andrew
0
 
LVL 2

Expert Comment

by:DeNavigator
ID: 12549035
Sorry DeerBear I overlooked your message. But then again ... whatever let it go, it is worse enough that the asker hasnot accepted an answer after a year.
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 12549163
No problem :-)

Well, I've left a few questions unaccepted too for a long time. It can happen, I guess :-)

Cheers,

Andrew
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now