7.3. Some Experiments In Scanning

Before getting back to our compiler, it will be useful to experiment a bit with the general concepts.

Let's begin with the two definitions most often seen in real programming languages:

<ident> ::= <letter> [ <letter> | <digit> ]*
<number ::= [<digit>]+

Note

Remember, the * indicates zero or more occurences of the terms in brackets, and the +, one or more.

We have already dealt with similar items in Chapter 3, More Expressions. Let's begin (as usual) with a bare cradle. Not surprisingly, we are going to need a new recognizer:

{ Recognize an Alphanumeric Character }
function IsAlNum(c: char): boolean;
begin
   IsAlNum := IsAlpha(c) or IsDigit(c);
end;

Using this let's write the following two routines, which are very similar to those we've used before:

{ Get an Identifier }
function GetName: string;
var x: string[8];
begin
   x := '';
   if not IsAlpha(Look) then Expected('Name');
   while IsAlNum(Look) do begin
     x := x + UpCase(Look);
     GetChar;
   end;
   GetName := x;
end;

{ Get a Number }
function GetNum: string;
var x: string[16];
begin
   x := '';
   if not IsDigit(Look) then Expected('Integer');
   while IsDigit(Look) do begin
     x := x + Look;
     GetChar;
   end;
   GetNum := x;
end;

Note

Notice that this version of GetNum returns a string, not an integer as before.

You can easily verify that these routines work by calling them from the main program, as in

     WriteLn(GetName);

This program will print any legal name typed in (maximum eight characters, since that's what we told GetName). It will reject anything else.

Test the other routine similarly.