Concrete Grammar for SIMPLE

The concrete grammar for SIMPLE is given in Extended Backus-Naur Form (EBNF). The grammar has two distinct parts: the regular part describes the structure of certain tokens for the scanner; the context-free part describes the structure of sentences for the parser (but also includes keywords and punctuation tokens for the scanner).

The character set is assumed to be 7-bit ASCII throughout; in addition to the SPACE character, the control characters TAB (horizontal tabulator), LF (line feed), CR (carriage return), and FF (form feed) are considered whitespace.

The notation for comments is described elsewhere (it changes from year to year to keep things interesting); comments are treated as whitespace.

Note that SIMPLE does not formally restrict the length of identifiers or integers, but implementations may enforce reasonable limits (at least 32 characters for an identifier, at least 32-bit signed integers); violations of such limits must be reported as implementation-specific errors.

Context-Free Grammar

Program = "PROGRAM" identifier ";" Declarations
  ["BEGIN" Instructions] "END" identifier "." .

Declarations = { ConstDecl | TypeDecl | VarDecl } .
ConstDecl = "CONST" {identifier "=" Expression ";"} .
TypeDecl = "TYPE" {identifier "=" Type ";"} .
VarDecl = "VAR" {IdentifierList ":" Type ";"} .

Type = identifier | "ARRAY" Expression "OF" Type |
  "RECORD" {IdentifierList ":" Type ";"} "END" .

Expression = ["+"|"-"] Term {("+"|"-") Term} .
Term = Factor {("*"|"DIV"|"MOD") Factor} .
Factor = integer | Designator | "(" Expression ")" .

Instructions = Instruction {";" Instruction} .
Instruction = Assign | If | Repeat | While | Read | Write .
Assign = Designator ":=" Expression .
If = "IF" Condition "THEN" Instructions ["ELSE" Instructions] "END" .
Repeat = "REPEAT" Instructions "UNTIL" Condition "END" .
While = "WHILE" Condition "DO" Instructions "END" .
Condition = Expression ("="|"#"|"<"|">"|"<="|">=") Expression .
Write = "WRITE" Expression .
Read = "READ" Designator .

Designator = identifier Selector .
Selector = {"[" ExpressionList "]" | "." identifier} .
IdentifierList = identifier {"," identifier} .
ExpressionList = Expression {"," Expression} .

Regular Grammar

identifier = letter {letter | digit} .
integer = digit {digit} .
letter = "a" | "b" | .. | "z" | "A" | "B" | .. | "Z" .
digit = "0" | "1" | .. | "9" .