Programming Language Macro: Grammar

CS 4410

Please note some self-evident issues such as the result of arithmetic and relational operators or execution of if/loop statements will not be included in this specification.

BNF

The following is a BNF description of the Macro language syntax. Curly braces are used to indicate items which may be repeated 0 or more times, while optional items are indicated by being enclosed in square brackets. Non-terminals appear on the left hand side of a production and are with a leading upper-case letter. Terminals are in lower case, and reserved words are in bold face. Special symbols are quoted.

tabular12

tabular38

}

If you look carefully at the above BNF you can find it will allow some invalid structures. However, such thing can be dealt with by propagating attributes. Also, it is not entirely appropriate to construct your LALR(1) grammar based only upon the given BNF. You should check the grammar against the specifications in following sections.

Define New Types

Predefined Types

We have four kinds of predefined types, one implicitly defined, and three explicitly defined.

Boolean
Implicitly defined. Appear as result of the relational operators, useful in if and exit when statements
Integer
As in Pascal and Ada. 32 bits, signed. Allow arithmetic and relational operations.
Float
As in Pascal and Ada. You can use C float or double to implement it. Allow arithmetic and relational operations.
String
In principle unlimited length. Allow relational operations and '+' as concatenation.

Type Definition

The syntax is in the above BNF. Some examples are as follows.

type foobar is integer;
It defines a new type named foobar, which behave exactly like an integer. For more complicated type structures, Macro provides array and record.

The range defining an array are integer constants (but allows negative numbers). Examples are:

type arr1 is array[1..9] of integer;
type arr2 is array[-1..1] of arr1;
type arr3 is array[-1..1] of array[1..9] of integer;

Types are defined as in Pascal or Ada. We allow nested records. Names of fields cannot be duplicated within the same level of definition, but different record types can have fields of the same name. The similar criteria applies to nested records. For example:

type r1 is record
i, j: float;
k: integer;
m: record i:string; end record;
end record

In Macro, we assume name equivalence, i.e., only type of the same name are considered the same type. So arr2 and arr3 are different types, although they share the same structure. Anonymous types, like type of m in record r1, are considered non-equivalent to each other.

Variables

Variable definition is like record field definition. We assume static scoping in Macro. Besides variables, static scoping also applies to types and procedures/functions.

Component Access

Access of components of a variable of structured type are by subscript access ('[' ']') and field access ('.') qualifications. Subrange check of array elements are done at run-time.

Expressions

Evaluation of arithmetic expressions follows the natural order as multiplicative first, additive second, and are left-associative. Evaluation of string + operation is also left-associative. The evaluation order of parameters for functions/procedures are from left to right.

For expressions involving both integer and float, coersion from integer to float is required, but the reverse direction is invalid.

Procedures/Functions

We use in/out modes to specify type of parameter passing. It means we use value-result, or copy-in copy-out.

As you can see, procedures/functions can be nested, and they also can be recursively invocated.

We don't consider overloaded procedures/functions.

Forward Reference

If a variable, type, function or procedure is to be used, it must be defined first. There is no problem with variables or types. But sometime you really need refer to a function/procedure before it can be completely defined. In this case, we should first provide a protocol like definition before it is referred to, and define the implementation in a later part.

Statements

Statements are quite the same as in Ada. We only discuss the difference here.

exit Statement

Macro has no goto statement. exit serves for a restricted version of goto. exit can only be used in a loop statement. exit without label will leave the innermost loop structure containing the exit statement. exit with a label will leave the loop labeled with the corresponding label. exit is only allowed to leave loops in the same subroutine or main program.

return Statement

return will terminate the execution of a subroutine. If it is in a function, it must have a value to return. If it is in a procedure, it must NOT return a value. Return from main body of a package is also allowed.

Packages

Basically, a package provides a bunch of interface other packages can use. The interface is defined between package MyID is and body. Anything defined within body is invisable to outside packages. In principle, the access is enabled with the dot notion like field access. In our project, we will also test multi-packages programs. The body part of a package is considered the initialization code. So if a package is supposed to be the main program, its body functions as the main function in C.

About this document ...

Programming Language Macro: Grammar

This document was generated using the LaTeX2HTML translator Version 96.1-e (April 9, 1996) Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.

The command line arguments were:
latex2html -split 0 grammar.

The translation was initiated by Yaxin Liu on Mon Oct 19 16:41:44 EDT 1998


Yaxin Liu
Mon Oct 19 16:41:44 EDT 1998