BASIC metasyntax
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 charactersA -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:
|
<num-var> | Stands for a numeric variable, an element that may take a numeric value. This may be:
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. |
<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:
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 PROC edures 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)