1
votes

I am attempting to write a comment stripper in pascal. I run my code and pass it a C source code file and it strips the comments from the file and prints the result to terminal.

I am fairly new to pascal. I am getting some very strange output and I cannot figure out why. The code checks for comments line by line and prints characters one at a time. The comment stripper is printing what seems to be random characters whenever it reaches the start of a new line. I am using pascals Write(Str[i]) function to print characters and WriteLn() once the end of a line is reached.

I have no idea why im receiving weird output. I am running Linux Mint and can compile and run my code, but I receive this strange output. I also tried running my code on a Mac and received a run-time error:

Program Path: ./Assignment1
File Name: lol.c
Runtime error 2 at $00011532
$00011532
$0002F7F6
$000113FD
$00011328
$00000002

Here is my code

program Assignment1;

uses
    Sysutils;

var UserFile    : TextFile;
   TString      : String;
   OLine        : String;
   i            : integer;
   isComment    : boolean;
   skip         : boolean;

begin

    {$I+}
   WriteLn('Program Path: ', ParamStr(0));
   WriteLn('File Name: ', ParamStr(1));

   Assign(UserFile, ParamStr(1) + '.c');
   Reset(UserFile);

   isComment := false;
   skip := true;

    Repeat

        Readln(UserFile, TString);

        for i:= 0 to ((Length(TString) - 1)) do
        begin
            if(skip) then
                begin
                    skip := false;
                    continue;
                end;


            if(isComment = false) Then
            begin

                if(TString[i] = '/') Then
                begin
                    if(TString[i+1] = '/') Then
                    begin
                        break;
                    end

                    else if(TString[i+1] = '*') Then
                    begin
                        isComment := true;
                        skip := true;
                        continue;
                    end;
                end;

                Write(TString[i]);
                if(i = Length(TString) - 1) Then
                    begin
                    Write(TString[i + 1]);
                    end;

            end

            else
            begin
                if(TString[i] = '*') Then
                begin
                    if(TString[i + 1] = '/') Then
                    begin
                        isComment := false;
                        skip := true;
                        continue;
                    end;
                end;
            end;

        end;

        WriteLn();

    Until Eof(UserFile);

end.

I receive random characters which range from standard keyboard symbols to unicode blocks such as the ones found here.

Does anyone have any suggestions?

1
In Pascal, strings are typically 1-based. - 500 - Internal Server Error
if(TString[i + 1] : could lead to a range error. you should rather accumulate an identifier and test its value. - Abstract type

1 Answers

2
votes

As 500 - Internal Server Error says, Pascal strings are 1-based. Your references to slot zero are returning garbage. If these are 256-byte strings you're getting the length code, I don't recall the memory layout of the pointer-based strings to know what you're getting in that case. You're also losing the last character of every string because of this.

Beyond that I see a definite bug: Look at what happens with a line ending in /

I also do not understand this:

            if(i = Length(TString) - 1) Then
                begin
                Write(TString[i + 1]);
                end;

It seems to me it's writing an extra character but I'm not sure.