Syntax

Top  Previous  Next

A program consists of a bunch of characters. The rules that organize these characters into meaningful patterns are called the syntax of a language. Beginning with the most detailed level, the syntax of XPL0 is broken down as follows:

 

Factors

Expressions

Statements

Blocks

Subroutines

 

A factor is the smallest part of a program that can have a numeric value. A factor is usually a constant or a variable. Constants are numbers such as 100, 5280, and 3.14. Variables are places to store numbers. They are given names by the programmer such as "Number", "Percent", and "FEET".

 

Factors are combined using operators to form expressions. An operator is usually one of the familiar arithmetic operators such as add, subtract, multiply, or divide. An expression calculates to a single value. Here are some examples of expressions:

 

        Percent - 10

        12.0 * FEET

        (Frog + 20.5) / 0.23

 

A statement is a request to do something. A typical statement combines expressions and commands. Here are two statements:

 

        Number:= Ran(100) + 1;

        if Guess = Number then Text(0, "Correct!")

 

Several statements can be combined into a single statement called a block. A block must start with a "begin" and terminate with an "end". Statements within a block must be separated by a semicolon (;). Here is an example of a block:

 

        begin

        Number:= 52 + 6;

        InputGuess;

        if Guess > Number then Text(0, "Too high");

        CrLf(0)

        end

 

XPL0 is very flexible in the way it allows statements and blocks to be combined. For example, blocks can be placed inside statements:

 

if Guess < Number then

        begin

        Text(0, "Too low");

        InputGuess;

        if Guess < Number then Text(0, "Still too low")

        end

 

Here we have an "if" statement containing a block. The block itself consists of three statements separated by semicolons.

 

Subroutines are the highest level of organization. In XPL0 there are several different types of subroutines; the most common is the procedure. A procedure is a block of statements that does a specific job. A program can contain any number of procedures. Procedures are given names and called as subroutines from other parts of the program. Here is an example of a procedure:

 

        procedure InputGuess;

        begin

        Text(0, "Input guess: ");

        Guess:= IntIn(0)

        end

 

This gives a you quick idea of what XPL0 is about. In the next sections we will examine each of these levels of syntactic organization in detail.