Thread: Code Translation

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    71

    Exclamation Code Translation

    Hi,
    I desperately need advice on planning a program that performs Pascal-to-C++ code conversion. I'm including a sample Pascal program with its C++ equivalent. You don't need to know Pascal - if you can help me plan a program that'll be able to convert this sample code, I'll be more than satisfied.
    Leaving out the reading-from-file and writing-to-new-file parts of it, (just knowing that it'll convert line by line) how should the program be structured? (How should the conversion be divided, what should each division do?)

    I will be forever thankful to anyone who helps me with this program!

    Linette


    Pascal code:
    Code:
    program pas (input, output); 
    uses    crt;
    var
            avg: real;
            count, sum, grd, n : integer;
    
            procedure readint (lo,hi : integer; var x: integer);
    	var       nothing : char;    {here for no reason}
            begin
                 readln(x);
                 while (x<lo) or (x>hi) do
                 begin
                   writeln (x, ' is not an acceptable grade.');
                   write ('Enter a grade: ');
                   readln (x);
                 end;
            end;
    
    begin
         clrscr;
         write ('How many grades? ');
         readln (n);
         writeln;
         sum := 0;
         for count := 1 to n do
         begin
              write ('Enter a grade: ');
              readint (0,100,grd);
              sum := sum + grd;
         end;
         avg := sum / n;
         writeln;
         writeln ('The total is ',sum);
         writeln ('The average is ',avg:5:2);  {don't worry about the formatting yet}
         readkey;
    end.
    C++ equivalent:
    Code:
    #include <iostream.h>
    #include <conio.h>
    
    void readint (double lo, double hi, double &x) {
    	char nothing; /* here for no reason */
    	cin >> x;
    	while ( (x<lo) || (x>hi) ) {
    		cout << x << " is not an acceptable grade." << endl;
    		cout << "Enter a grade: ";
    		cin >> x;
    	}
    	return;
    }
    
    void main () {
    	double avg;
    	int count;
    	double sum;
    	double grd;
    	int n;
    	clrscr ();
    	cout << "How many grades? ";
    	cin >> n;
    	cout << "\n";
    	sum = 0;
    	for (count=1; count<=n; count++) {
    		cout << "Enter a grade: ";
    		readint (0,100,grd);
    		sum = sum + grd;
    	}
    	avg = sum / n;
    	cout << "\n";
    	cout << "The total is " << sum << endl;
    	cout << "The average is " << avg << endl;  /*don't worry about the formatting yet*/
    	return;
    }
    Last edited by Linette; 03-29-2002 at 08:16 PM.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    by the way, indentation isn't an issue.. you may assume the pascal file isn't indented (so there's no space at the beginning of lines)..

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    Wow, I can't believe I got so many replies!..

    Has anyone ever seen anything more pathetic than someone having gotten no replies to their post but from themselves?!..

    Please help!..

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Wow, I can't believe I got so many replies!..
    Yeah, whats it like to be so popular
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Your conversion is not right.

    * In Pascal the variables after var are global variables. So in C++ the variables should also be global.

    * The variable type integer in Pascal is the type int in C++.

    * If I am correct then readkey has a C++ equivalent: getch.

    * Function main should be of type int and return an int.

    The structure seems ok to me.

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    This cannot be done in simple text substitution. Well, maybe it can be done this way. How complex should it be ? What is it for ? Does it have to translate this program only, or should it be a general translator ?

    You will need to get this as one long string and tokenize it. Make a linked list of words and put all the words of the pascal code in there. From there you can then start translating it.

    If it has to be more complicated, maybe books about building parsers and compilers might help.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    Shiro:
    Thanks for pointing out all the things you found wrong!..
    I don't know how to declare variables for the main program in pascal other than the way I did it. (and that's fine by my teacher so it's ok).. As for getch(), thanks - I put return because I used to use readkey as just something to end functions/procedures with in pascal, and now in C++ I do that with return.. but I guess getch() would make more sense.. the integer thing was an honest mistake though, just pretend those variables are of type real in pascal (I changed them to double in C++ to avoid having to typecast, but then I forgot to change them in pascal, that's how this happened).. As for void main, I've had other people tell me to use int main (and return 0) too, but this way is fine, it works..
    So any suggestions on how to do the conversion after all?!..

    Nvoigt:
    It should convert any program, but the level will be this, not more advanced. So it should handle a minimum of input, output, calculations, variable declarations, loops, and maybe (as an extension) procedures/functions.
    I originally thought to convert it line by line, but if you think making the whole thing one big string would be better then I'll do it that way.. So how do I tokenize it? What should I do first?


    Thanks for your replies.
    Linette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM