Skip to main contentCal State San Bernardino / [CNS] / [Comp Sci Dept] / [R J Botting] / [CSci320] [Search ]
[Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Grading] [Contact]
Sessions: [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
A.html (HTML) [A.txt(Text)] Wed Mar 14 07:37:26 PDT 2007

Contents


    Language Reference Manual for the Language A

      Context

      This document is an example and template for a language reference manual --(LRM). It defines a very simple programming language called A.

      The document defines the language piece by piece. For each part it defines the syntax and semantics. It also comments on the purpose for the part and gives examples of the code described. Good LRMs need examples, comments, syntax, and semantics.

      This manual was written using XBNF and translated to HTML by Dr. Botting's mth2html translator that added the contents list and inserted hyperlinks. These are also parts of good manuals.

      This is an example for a class and is the basis for project work, so I've not documented some of the options and issues that arise in the design languages. I've deliberately added one feature that is a bad idea. I wonder if you can spot the language that I ripped it from.... and added a perverse twist of my own. Finally I know of several typographical errors and many omissions.

      Warning

      This is a bad language. Do not try it at home. If you feel like writing a compiler or interpreter don't -- it is not difficult BUT its better to fix the problems with A FIRST. Your first job is to spot the problems and omissions.

      Historical Context

      A is not unlike half-a-dozen "autocodes" used in the 1960's in the United Kingdom. Real languages in this family include: Pegasus Autocode, Elliott 803 Autocode, Mercury Autocode, and K-code. All were compiled, targeted scientific computing, and are not mentioned in most text books. They did not survive competition from FORTRAN, COBOL, and Algol. However, they are small enough to be defined by a small LRM. A is slightly more complex than Pegasus Autocode, and definitely less complex than K-code. The big omission is any form data structure, and so, no indexing or subscripts.

      The Language A

        Programs

        Program written in A consists of a series of executable statements (each on one line), and terminated by an "END" statement:
      1. program::= #(statement eoln) "END" eoln.
      2. eoln::=end of line, any sequence of white space characters including at least one "Carriage Return" character.

        Here is an example of a program written in A:

         READ a
         b=a*a
         PRINT b
         END
        The above program reads a floating point decimal number (7.0 for example, and puts it into floating point location a. The next squares it, and stores the result in b. Then it prints out the result(49.0).

        A program in A starts by executing the the first statement and finishes when the END statement is executed. Normally each statement is taken in sequence, however, some statements can select a different successor statement that is before or after them in the sequence. For example to print the sum of the first n integers one writes:

         i=1
         s=0
         READ n
         j=i>=n
         SKIP 4*j
         s=s+i
         i=i+1
         SKIP -4
         PRINT s
         END

        The following program does the same thing using a formula (s = n(n+1)/2) instead of a loop.

         i=1
         READ n
         s=n+1
         s=s*n
         s=s/2
         PRINT s
         END
        Notice how complex formulas must be written out as a series of simple calculations.

        Statements

        There are four types of statement (other than the END statement):
      3. statement::= assignment | input | output | skip.

      4. assignment::= variable "=" expression.
         d=b*b
         e=4*a
         e=e*c
         d=d-e
        An assignment evaluates the expression on the right hand side and places the value in the variable on the left hand side of the equals sign.

      5. input::= "READ" variable.
         READ a
         READ n
        When an input statement is executed a number (in decimal notation) is taken from the user and placed in the variable.

      6. output::="PRINT" expression | "OUT" text.
         PRINT d
         OUT Real Roots
        A PRINT statement evaluates its expression and converts it to decimal notation before sending it the the user. The OUT statement takes the text, as is, and send that that to the user.

      7. skip::="SKIP" O(sign) expression.
         SKIP -12
         SKIP +3
         SKIP 0
         SKIP +17*k
        SKIP statements are the only control statement. The break the flow of control. After a break statement control is passed to any statement before the or after the SKIP depending on the value of the expression in the SKIP. Notice that expressions can also evaluate simple tests by comparing values -- see Relations later.

        The following indicates which statement is executed given the values 0, -1, -2, +1, and +2 in a SKIP:

         (-2) ...
         (-1)...
             SKIP ?
         (0) ....
         (+1) ...
         (+2) ...
        It is possible to could any control flow using SKIP -- but a wise programmer will draw a flowchart first.

        Variables

        Variable are represented by the 26 lowercase letters. Letters i through r inclusive are for integer (fixed point ) numbers. The rest are hold floating point numbers.
      8. variable::="a" .. "z".
      9. integer_variable::="i".."r".
      10. real_variables::=variable ~ integer_variable.
         a
         i

        Expressions

        An expression has at most one operator or a single function.
      11. expression::= element O( operator second_element ) | function element.

      12. element::= variable | constant.
      13. second_element::=element.
      14. operator::= "-" | "+" | "*" | "/" | relation.
      15. relation::="<" | ">" | "<=" | ">=" | "<>" | "=".
      16. function::=sign | "SQRT" | "ABS" | "LOG" | "COS" | "SIN" | "TAN" | "EXP".
      17. sign::= "+" | "-".

        Examples

         x
         -x
         -i
         i+1
         x+i
         x+1.234
         x+y
         x*y
         x*y
         -3
         SQRT 2
         SQRT d

        In expressions the values are converted from integers to floating point as needed.

        Relations

        The values of a relation are always either one(true) or zero(false). They are typically used in SKIP statements to choose different successors depending on the values of variable. For example the following code places the larger of x and y into z.
         j=x>y
         SKIP 2*j
         z=y
         SKIP 1
         z=x
        exercise: can you write code to put the minimum of x, y into z?

        Constants

        A constant is a number: fixed point integer or floating point number with a decimal point. Only decimal notation is used.
      18. constant::= fixed | floating.
      19. fixed::= N(digit).
         1234

      20. floating::= N(digit) "." N(digit).

         12.34
        Notice that "1." and ".95" must be written as "1.0" and "0.95" respectively.

        Longer Example: Solving a Quadratic (Untested)

         READ a
         READ b
         READ c
         d=b*b
         e=4*a
         e=e*c
         d=d-e
         e=2*a
         r=d<0
         SKIP r*9
         d=SQRT d
         s=d-b
         s=s/e
         t=-t
         t=t-d
         t=t/e
         PRINT s
         PRINT t
         SKIP 8
         d=d-d
         d=SQRT d
         s=b/e
         s=-s
         t=d/e
         PRINT s
         OUT + or - i *
         PRINT t
         END
        Exercise: handle the case when a is zero.

        Semantics

        Here is an outline of the semantic structure of the language A using the UML. [UML semantics]

        Note

        A is not the smallest possible programming language.

      . . . . . . . . . ( end of section The Language A) <<Contents | End>>

      Notation

    1. For X, N(X)::=1 or more X's,
    2. For X, O(X)::=optional X, 0..1, [ cs320xbnf.htm ]

    3. digit::="0".."9", [ cs320xbnf.htm ]

    . . . . . . . . . ( end of section Language Reference Manual for the Language A) <<Contents | End>>

End