LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   p2c (https://www.linuxquestions.org/questions/slackware-14/p2c-4175437459/)

sinuhe 11-16-2012 12:18 PM

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.

wpeckham 11-16-2012 03:33 PM

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?

sinuhe 01-22-2013 10:43 AM

No, it really is p2c.
 
Quote:

Originally Posted by wpeckham (Post 4831015)
Does that cod3e work anywhere else?

Yes. fpc and gpc on Debian 6.0 (Sparc64), fpc on Fedora 18 (x86_64) and Slackware 14.0.

Quote:

You do realize that the inner loop can get an EOF that it is NOT checking for while seeking a non-existant EOL?
Here's a simpler example, avoiding the separate while loop:

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:

I assume you tried the traditional "Hello World" with no problems?
Yes, but a hello world only write's text, it doesn't interact or pull from a stream of characters.

lkraemer 02-19-2013 07:23 PM

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);
VAR c: char;
BEGIN
WHILE NOT eof DO
BEGIN read(c); write(c);  { missing ; }
END;                      { missing ; }
END.

REF:
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;

var
    i: integer;

begin
    write('Number, please?  ');
    while not eof do begin
      readln(i);
      writeln('That was a ', i: 2, '.');
      writeln;
      write('Number, please? ');
    end;
end.

Quote:

bash-4.2$ ./eof_example3
Number, please? 1
That was a 1.

Number, please? 2
That was a 2.

Number, please? 3
That was a 3.

Number, please? 4
That was a 4.

Number, please? 5
That was a 5.

Number, please? 6
That was a 6.

Number, please? 7
That was a 7.

Number, please? 8
That was a 8.

Number, please? 9
That was a 9.

Number, please? 10
That was a 10.

Number, please? ^Z
[1]+ Stopped ./eof_example3
bash-4.2$ ls -alt eof_example*
-rwxr-xr-x 1 larry users 17059 Feb 19 19:03 eof_example3
-rw-r--r-- 1 larry users 228 Feb 19 18:59 eof_example3.pas
bash-4.2$
I modified your sample as follows:
Code:

PROGRAM copy (input, output);
VAR c: char;
BEGIN
WHILE NOT eof DO
BEGIN
write('Number, please?  ');
readln(c);
writeln('That was a ',c '.\n');
END;
END.

By using EOF (Control Z) it works correctly, when compiled with p2cc.


Larry

sinuhe 02-20-2013 07:35 AM

Quote:

Originally Posted by lkraemer (Post 4895562)
I compiled the eof_example3.pas file and it executes properly.

I tried your example with fpc and gpc on Debian (Sparc64), and fpc on Slackware 14.0, and it worked fine. I tried it on Slackware 14.0 with p2cc, however, with the same results as my own code. I have tried this with older versions of Slackware in years past.

Quote:

I modified your sample as follows:
I tried this as well, with the same end-of-file error. I continue to suspect the flaw as being p2c, not me.

Quote:

By using EOF (Control Z) it works correctly, when compiled with p2cc.
By using EOF on Slackware with Control D, it does not work, when compiled with p2cc, but does work with fpc on Slackware.

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.

sinuhe 02-20-2013 07:57 AM

Quote:

Originally Posted by lkraemer (Post 4895562)
Code:

BEGIN read(c); write(c);  { missing ; }
END;                      { missing ; }


Are you suggesting here that I'm missing semi-colons, i.e. as a terminator? Pascal uses semi-colons as separators, not terminators.

sinuhe 02-20-2013 10:40 AM

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.

GazL 02-20-2013 10:57 AM

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
PROGRAM COPYTEXT;

VAR
  s: String;

BEGIN
  while not eof do
  begin
    readln(s) ;
    if not eof then writeln(s)
  end;
END.

...as long as you don't mind the buffer overflow it produces:
Code:

/* Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator */
/* From input file "test.pas" */


#include <p2c/p2c.h>


main(argc, argv)
int argc;
Char *argv[];
{
  Char s[256];

  PASCAL_MAIN(argc, argv);
  while (!P_eof(stdin)) {
    gets(s);
    if (!P_eof(stdin))
      puts(s);
  }
  exit(EXIT_SUCCESS);
}



/* End. */

gets() ? Seriously? :tisk:

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.

lkraemer 02-20-2013 03:23 PM

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:

Semicolons:

You may have noticed in the code examples there is a semicolon at the end of every line. The reason for this is because in Turbo Pascal, Semicolon signify the end of a STATEMENT. The reason I say statement and not line is because in some cases you will want to spread out your statement over many lines and you will not want a semicolon on those lines. An example of this is a 'IF' statement which we will get to later, but for right now just put a semicolon after everything and you should be alright. The only exception is the 'begin' statement. Do not put a semicolon after any 'begin'.
I searched for the EOF Char and found: http://forums.codeguru.com/showthrea...File-Character
Quote:

EOF (or ^Z) is equivalent to character 26 - to ignore or change them, open the file in binary mode, then look for character 26 or these other text-mode characters:

^Z EOF 26
\n NewLine 10
\r CarriageReturn 13
\t Tab 9
I executed the eof_example3 compiled code, and used "CNTL D" to exit the first time. I get the same error. Then
I executed the code again and used "CNTL Z" to exit the second time.

Exit using CNTL D
Code:

bash-4.2$ ./eof_example3
Number, please?  1
That was a  1.

Number, please? 2
That was a  2.

Number, please? Pascal system I/O error 30 (end-of-file)

Exit using CNTL Z
Code:

bash-4.2$ ./eof_example3
Number, please?  1
That was a  1.

Number, please? 2
That was a  2.

Number, please? ^Z
[1]+  Stopped                ./eof_example3
bash-4.2$

The actual "C" code produced of eof_exampl3.pas is:
Code:

/* Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator */
/* From input file "eof_example3.pas" */


#include <p2c/p2c.h>


main(int argc, Char *argv[])
{
  int i;

  PASCAL_MAIN(argc, argv);
  printf("Number, please?  ");
  while (!P_eof(stdin)) {
    if (scanf("%d", &i) != 1)
      _EscIO(feof(stdin) ? EndOfFile : BadInputFormat);
    if (scanf("%*[^\n]") != 0)
      _EscIO(feof(stdin) ? EndOfFile : BadInputFormat);
    if (getchar() == EOF)
      _EscIO(EndOfFile);
    printf("That was a %2d.\n\n", i);
    printf("Number, please? ");
  }
  exit(EXIT_SUCCESS);
}



/* End. */


Your original code is:
Code:

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.

and it compiles to:
Code:

/* Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator */
/* From input file "example.pas" */


#include <p2c/p2c.h>


main(int argc, Char *argv[])
{
  Char ch;

  PASCAL_MAIN(argc, argv);
  while (!P_eof(stdin)) {
    while (!P_eoln(stdin)) {
      ch = getchar();
      if (ch == EOF)
        _EscIO(EndOfFile);
      if (ch == '\n')
        ch = ' ';
      putchar(ch);
    }
    if (scanf("%*[^\n]") != 0)
      _EscIO(feof(stdin) ? EndOfFile : BadInputFormat);
    if (getchar() == EOF)
      _EscIO(EndOfFile);
    putchar('\n');
  }
  exit(EXIT_SUCCESS);
}



/* End. */

Exit using CNTL D
Quote:

bash-4.2$ ./example
1
1
2
2
Pascal system I/O error 30 (end-of-file)
bash-4.2$
Exit using CNTL Z
Quote:

bash-4.2$ ./example
1
1
2
2
^Z
[2]+ Stopped ./example
bash-4.2$
Does it work the same way for you?


Larry

psionl0 02-20-2013 10:42 PM

Quote:

Originally Posted by lkraemer (Post 4896230)
sinuhe,
The Pascal I've used is Turbo Pascal. I run it on Slackware 14 in DOSBox.

I have the same thing. If you test sinhe's original code on Turbo Pascal 3.0 you will find it doesn't work.

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;
read(kbd, character);

Of course, that only works on the keyboard.

Arcosanti 02-24-2013 01:56 PM

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

sinuhe 02-25-2013 01:38 PM

Quote:

Originally Posted by lkraemer (Post 4896230)
Does it work the same way for you?

It appears you have now experienced the same problem from my original post, yes.

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.

sinuhe 02-25-2013 01:41 PM

Quote:

Originally Posted by Arcosanti (Post 4898798)
As an alternative to p2c or p2cc, you could use the GNU Pascal compiler. Looks like it is compatible with Borland Pascal.

Please actually bother to read what I've posted before responding. All three points you make here are addressed in other posts of mine from this thread.

sinuhe 02-25-2013 01:56 PM

Quote:

Originally Posted by GazL (Post 4896060)
Bur seriously, don't use p2c. It's infamous.

This seems to affirm what I now suspect: p2c is inadequate for those who program in Pascal (as opposed to those who program in C, who wish to convert Pascal to C).

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.