10.2. Declarations

The obvious next step is to decide what we mean by a declaration. My intent here is to have two kinds of declarations: variables and procedures/functions. At the top level, only global declarations are allowed, just as in C.

For now, there can only be variable declarations, identified by the keyword VAR (abbreviated 'v'):

     <top-level decls> ::= ( <data declaration> )*
     <data declaration> ::= VAR <var-list>

Note that since there is only one variable type, there is no need to declare the type. Later on, for full KISS, we can easily add a type description.

The procedure Prog becomes:

{  Parse and Translate a Program }
procedure Prog;
begin
   Match('p');
   Header;
   TopDecls;
   Main;
   Match('.');
end;

Now, add the two new procedures:

{ Process a Data Declaration }
procedure Decl;
begin
   Match('v');
   GetChar;
end;

{ Parse and Translate Global Declarations }
procedure TopDecls;
begin
   while Look <> 'b' do
      case Look of
        'v': Decl;
      else Abort('Unrecognized Keyword ''' + Look + '''');
      end;
end;

Note that at this point, Decl is just a stub. It generates no code, and it doesn't process a list … every variable must occur in a separate VAR statement.

OK, now we can have any number of data declarations, each starting with a 'v' for VAR, before the BEGIN-block. Try a few cases and see what happens.