Board index » delphi » Question regarding file i/o and other nifty stuff

Question regarding file i/o and other nifty stuff

   I am working on a program that uses a script file. I have been doing the
script file in text. I need a way of accessing a text type file to read in a
character at a time, to read data such as this :

rep(112,122);

it would read the first three characters, then using a CASE
statement, run another similar routine that was setup to read the
variables, in this case, they are bytes. but I need to know how to get rid
of the
nulls like the (), and ;.

   I am thinking along the lines of reading the first three characters, then
use a CASE statement (like I said above) to determine what command we're
processing, then run another read procedure that can read in characters,
eliminating () and using . to differentiate between byte or int values. I
guess I could create a procedure that only accepted numeric characters and
chucked everything else, and then limited it to three numbers for each
value.

   I know I'm probably not making much sense here, but that's because I am
confused :>...

 - Bishop

 

Re:Question regarding file i/o and other nifty stuff


Quote
Bishop wrote:
>    I am working on a program that uses a script file. I have been doing the
> script file in text. I need a way of accessing a text type file to read in a
> character at a time, to read data such as this :

> rep(112,122);

> it would read the first three characters, then using a CASE
> statement, run another similar routine that was setup to read the
> variables, in this case, they are bytes. but I need to know how to get rid
> of the
> nulls like the (), and ;.

>    I am thinking along the lines of reading the first three characters, then
> use a CASE statement (like I said above) to determine what command we're
> processing, then run another read procedure that can read in characters,
> eliminating () and using . to differentiate between byte or int values. I
> guess I could create a procedure that only accepted numeric characters and
> chucked everything else, and then limited it to three numbers for each
> value.

>    I know I'm probably not making much sense here, but that's because I am
> confused :>...

A case statement with 3 characters at once? That won't work, you must proceed
each character individually. To prevent this from becoming a bunch of nested and
complicated case statements, I suggest you work using symbols.

Considering that your script language only consists of a predefined set of
characters, you can assign a symbol (a name) to a group of characters, like
this:

type
  SymbolType = (_string, _numeric, _openpar, _closepar,  _comma);

a..z : _string;
0..9 : _numeric
( : _openpar
) : _closepar
, : comma

Thus, rep(112,122) would correspond to the following sequence of symbols:

_string _openpar _numeric _comma _numeric _closepar

Declare an

Symbols : array[1..n] of record
                           Symbol : SymbolType;
                           Value  : word;  { if Symbol = _numeric }
                           Name   : string { if Symbol = _string }
                         end;

n is a arbitrary number.

Read in single characters from a file of char.

Read(f, ch);
ch := upcase(ch);

If ch in ['A'..'Z'] then
begin
  SymStr := '';
  while ch in ['A'..'Z'] do
  begin
    SymStr := SymStr + ch;
    read(f, ch);
    ch := upcase(ch)
  end;
  Symbols[x].Symbol := _string;
  Symbols[x].Name := SymStr
end
else
  if ch in ['0'..'9'] do
  begin
    SymVal := 0;
    while ch in ['0'..'9'] do
    begin
      SymVal := SymVal * 10 + byte(ch) - 48
      read(f, ch)
    end;
    Symbols[x].Symbol := _numeric;
    Symbols[x].Value := SymVal
  end
 else
   if ch in ['(', ')'] then
   begin
     case ch of
       '(' : Symbols[x].Symbol := _openpar;
       ')' : Symbols[x].Symbol := _closepar
     else
       writeln('Invalid character')
     end;
     read(f, ch)
   end;
inc(x);

Repeat this procedure until you reach the end of the file. X is a variable of
type word incremented each time a new Symbol is to be found out.
Then, proceed the array. You have to set up some kind of primitive grammar for
your script language.

If, for example, a Symbol of type _string is not followed by a _openpar, issue
the error message " '(' expected". Or, if a _comma is not followed by _numeric,
issue the error message " Numeric value expected".

This is a fairly primitive approach to compiler design. :-)

Other Threads