p2c
p2c/p2cc does not seem to be working for me. I'm getting an end-of-file error from compiled programs. Is there something I should be using in p2c's syntax, or is this a bug in the Slackware build (upstream?) of p2c? I'm on slackware64-14.0.
Please do not respond with 'Why are you using Pascal?' or 'You should use foo compiler or language instead'. I am interested in working specifically with p2c and Pascal on Slackware. The following is a simple test program that works fine with free pascal, gpc, and p5, (mostly on other Linux systems), but not p2c on Slackware (I've not tested p2c on another Linux system): program copytext (input, output); var ch : char; begin while not eof do begin while not eoln do begin read(ch); write(ch) end; readln; writeln end end. To test, run the compiled binary: './copytext' and interactively type text, press enter, see the result, then type Ctrl-d multiple times until it exits with the error. Also, try './copytext <footextfile' and again note the failed exit status and end-of-file error. |
logic?
Does that cod3e work anywhere else?
You do realize that the inner loop can get an EOF that it is NOT checking for while seeking a non-existant EOL? That situation is defined and well handled by most implementations of Pascal, but this is a C translation of Pascal. You might want to try something with fewer 'interesting' ways to complicate the issues. I assume you tried the traditional "Hello World" with no problems? |
No, it really is p2c.
Quote:
Quote:
PROGRAM copy (input, output); VAR c: char; BEGIN WHILE NOT eof DO BEGIN read(c); write(c) END END. Compiled with p2cc -ocopy copy.pas, the following is the behaviour: $ ./copy hello world hello world Ctrl-D Pascal system I/O error 30 (end-of-file) $ When compiled with fpc or gpc (e.g. fpc -ocopy copy.pas), I get the following: $ ./copy hello world hello world Ctrl-D $ Is this a bug in p2c, or is this a build problem with the package? Is there some special incantation (p2c or p2cc) I should be using? Quote:
|
sinuhe,
It appears to me that your logic is faulty. Shouldn't you assign a filename to the datafile. Then you should reset the file pointer, to the beginning of the file. At that point you are pointing to the beginning of the file, and you can read the file until the End of File Marker (EOF). At that point you close the datafile. Your EOF marker should have been inserted when you created the file, wrote the data, and closed the file, which wrote your data and the EOF to your disk. Open your file with a HEX Editor and look for the EOF. Plus, your code as shown has two errors that shouldn't compile with your Pascal Compiler. Code:
PROGRAM copy (input, output); http://www.amath.unc.edu/sysadmin/DO...ef_io.doc.html I compiled the eof_example3.pas file and it executes properly. Code:
program eof_example3; Quote:
Code:
PROGRAM copy (input, output); Larry |
Quote:
Quote:
Quote:
I am suspicious that you did not actually test your code on Slackware with p2c/p2cc, but did so on Windows. (Do you have a build of p2c/p2cc for Windows?) To pull it away from my own code, another program I tried was in Software Tools in Pascal, pg. 9, which uses an explicit end-of-file sentinel, with the same result. |
Quote:
|
p2c's purpose
Since I have had two responses focusing on Pascal source correctness, instead of answering the original problem I posted, i.e. that p2c/p2cc does not produce binaries that function as expected, I would like to answer my own question, and have someone correct me if I'm wrong.
I was hoping that there was a simple solution to my end-of-file problem with p2c, such as an option to be passed to p2c that I had missed, or even the suggestion that there is a bug in the p2c code. Instead, I suspect that the problem is my expectation. As Dave Gillespie's home page indicates, "P2c's main goals are readability and correctness of the output code. Translators usually fall in one of two categories: Either they work on the text of the program directly without really "understanding" the program, or they are really more like a Pascal compiler that outputs C instead of machine language." My expectation was for the latter, whereas p2c was designed to be the former. If you intend to convert code from Pascal to C, and carry on development of the program in C, then p2c is the right tool, albeit editing of the result may be needed. (Compare my examples noted here, and the p2c C output from those examples, with the copy command in the C Programming Language.) If you need a compiler, then look elsewhere if p2c doesn't work by default. ISO 7185 compatible processors are hard to find these days, and p2c's claim of compliance, and its default installation with Slackware, had me hopeful, but I think I need to move on and look elsewhere. Other options for Slackware include: gpc (hard to compile successfully on Slackware these days), p5 (Scott Moore), and ptoc. FPC is also moving towards compliance, so maybe I should stick with that, and avoid features in the Pascal Report (and ISO 7185 standard) not supported by it. Thank you to those who provided responses. It seems that p2c is an inadequate, and now mostly unmaintained, tool for someone programming in Pascal. |
ctrl-z is SIGSTOP not EOF. The only reason the error isn't showing is that the program never gets to that point.
Changing the variable type to String seems to solve it. Code:
gazl@ws1:/tmp$ cat test.pas Code:
/* Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator */ It doesn't seem to want to work with c: char;, the p2c translation was using some very weird looking scanf format string that I couldn't be arsed to try and interpret. Bur seriously, don't use p2c. It's infamous. |
2 Attachment(s)
sinuhe,
The Pascal I've used is Turbo Pascal. I run it on Slackware 14 in DOSBox. When I compiled the sample code I was on my Laptop running Slackware 14 (32 Bit)....Not Windows, and using p2cc. Here is an excerpt from Turbo Pascal, as my memory may have been wrong..: Quote:
Quote:
I executed the code again and used "CNTL Z" to exit the second time. Exit using CNTL D Code:
bash-4.2$ ./eof_example3 Code:
bash-4.2$ ./eof_example3 Code:
/* Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator */ Your original code is: Code:
program copytext (input, output); Code:
/* Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator */ Quote:
Quote:
Larry |
Quote:
I remember that when I used to use that package, in order to process keyboard characters you had to do the following: Code:
repeat until keypressed; |
As an alternative to p2c or p2cc, you could use the GNU Pascal compiler. Looks like it is compatible with Borland Pascal.
http://www.gnu-pascal.de/gpc/h-index.html |
Quote:
PS Semi-colon termination in Pascal is unique to Borland's dialect of Pascal. PPS Note a previous poster's comment about Ctrl-Z and Ctrl-D. |
Quote:
|
Quote:
Since no one has been able to point out a way to use p2c as requested, or indicated a bug in p2c, I think this thread has served its purpose. I'll mark it as solved. |
All times are GMT -5. The time now is 11:00 AM. |