BASIC metasyntax

From BeebWiki
Jump to: navigation, search

This page describes the notation used by Acorn to help define the syntax of the BASIC programming language. It gives the syntax of the syntax definitions - hence metasyntax.

Elements

In the BBC Microcomputer User Guide, and in BeebWiki pages on the BASIC keywords, there is a line or two of notation defining the syntax that BASIC expects when the keyword is used. That is to say, the keyword must be preceded and/or followed by the right elements, in the right order and having the right type, to be sure of not causing a Syntax error.

The syntax definition helps the experienced programmer fix errors, and understand how the keyword can be used most efficiently. For the novice, the User Guide also printed examples of what use of the keyword 'looks like' in practice.

Element Meaning
Literal characters
A-Z 0-9 ! " # , . etc.
A character printed in monospaced 'typewriter' font stands for itself, and must appear in the program, exactly as printed.
Sequence When two or more elements appear side by side, with no brackets separating them, they must appear in the program, next to each other and in the same order. All the elements are compulsory. When the sequence is optional, all the elements must appear, or none of them. For the purpose of this definition a single element counts as a sequence of one.
| A vertical bar indicates a choice between the sequence on the left of | or the one on the right. Only one or the other may appear in the program. In the User Guide and these pages the lengths of the alternatives are not defined. A little intuition is required.
For example, DEF PROC|FN<variable name> can really expand to DEF PROCfred or DEF FNcrc16.
DEF PROFNsheila or DEF PROC would be equally valid expansions, but unacceptable to BASIC.
[ ] Square brackets enclose an optional sequence. That is, the sequences on either side can be joined together without a gap, or the sequence inside can appear once between them. For instance:
[LET] <num-var> = <numeric>
may expand to:
X = 5
LET status% = 1
{ } Curly brackets enclose a sequence that is optional, or can be repeated once or more. The repetitions must be chained end-to-end.
PRINT#<numeric>{,<numeric>|<string>}
may expand to
PRINT#channel%
PRINT#OUTFILE,"---END---"
PRINT#fh%,id%,name$
PRINT#Y%,2,from%,to%
...and so on.
<num-const> Stands for a numeric constant, a number value expressed in figures in the text of a program. This may be:
  • an integer: optional + or -, followed by a sequence of the digits 0 to 9; +7, 100, -40.
  • a real number: an integer with at most one decimal point ., inserted before or after a digit; 1100, 1101., -273.15
  • a scientific number: a real number, followed by the letter E and an integer; 9.192E9, 6.673E-11.
  • a hexadecimal number: an & followed by a sequence of hex digits 0 to 9 and A to F; &0, &FFF7.
  • (BASIC V only) a binary number: a % followed by a sequence of digits 0 and 1; %11101010.
<num-var> Stands for a numeric variable, an element that may take a numeric value. This may be:
  • a real variable, whose name is a sequence of letters and numbers that starts with a letter and does not contain a BASIC keyword; A, burntime, chicken2egg5
  • an integer variable, whose name complies with the above but ends in %; X%, y%, @%, Base%
  • a numeric array element: a numeric variable name followed by a numeric value in round brackets; population(I%), field%(30), G%(I)
  • an indirection operation: an expression involving ? or ! that writes (pokes) to memory; !&3000, osblock%!5
  • or a system variable. HIMEM, TIME

In the User Guide, the idiom <num-var> = ... means that the expression to the right of the = behaves as a <numeric> (see below), and can appear anywhere a <numeric> can. The value of the <numeric> does not have to be assigned to a <num-var>, as suggested by the syntax line.
For instance <num-var> = GET means GET is a <numeric>, and can be used as:
keypress = GET
IF GET=32 THEN PROCadvance
ON GET-48 GOSUB 1000,1010,1050,1100

<numeric> From the User Guide: "Means either a <num-const> or a <num-var>, or a combination of these in an expression such as 4*X+6."
<string-const>
(or <str-const>)
Stands for a string constant, a sequence of literal characters beginning and ending in double quotes ". It may be the empty string, "".
Where two or more <string-const>s appear to be next to each other with no gap, it is actually a single <string-const>; the pairs of double quotes become single "s when the string is evaluated.
For instance, these are all valid <string-const>s:
""
"Sheila"
"She said, ""Hello""."
"*RUN """""
<string-var>
(or <str-var>)
Stands for a string variable, an element that may take a string value. This may be:
  • a string variable, whose name is like a real variable's name but ending in $; A$, name$
  • a string array element: a string variable name immediately followed by an integer value in round brackets; N$(2), city$(X%)
  • or a string indirection operator: $<numeric>, that pokes the string into memory. $&7800, $marker, $buffer%

Just as for <num-var>, the idiom <string-var> = ... means the thing after the = is a <string>, and can appear wherever a <string> is called for.

<string> From the User Guide: "Means either a <string-const> or a <string-var>, or an expression such as A$+"LINDA"."
<testable condition> A <numeric> expression that will be interpreted as a Boolean value (TRUE or FALSE). Used as an argument to IF, WHILE and UNTIL.
Any expression that is a valid <numeric> can be used here, but typically it is an equality or inequality expression, for example answer$ = "PARIS" or sample% >= threshold%.
<statement> A complete BASIC statement (that returns no result), or several such statements separated by colons :. IF and ON statements contain subordinate <statement>s that are executed if a certain condition is met.
IF <testable condition> [THEN] <statement> [ELSE <statement>] may expand to:
IF target%=2 fdc_base=&FE84:fdc_latch=&FE80 ELSE fdc_base=&FE80:fdc_latch=&FE84
<variable name> A sequence of characters that make a valid variable name. This element is used for the names of PROCedures and functions (FN). Unlike the names of variables, it may contain BASIC keywords but this is not recommended.

-- beardo 21:31, 5 May 2007 (BST)