Article View: comp.lang.modula2
Article #636Re: m2 grammar
From: Pat Terry
Date: Thu, 05 Apr 1990 04:32
Date: Thu, 05 Apr 1990 04:32
439 lines
11203 bytes
11203 bytes
> First thanks to all those who responded to my original pley for a Modula-2
> grammar. I think it should be posted again.
Okay. It may come in two pieces because of local limits on message size;
COLLECTED MODULA-2 CONCRETE SYNTAX
This version compiled 1 January 1990 by Pat Terry, from D106, appendix A
of the Draft Proposal for Modula-2 and may not be 100% accurate. Any
errors are, of course, unintentional
It will not mean anything to claim conformance with this at this stage.
WG13 on Modula-2 invites comments (although I believe these should have
been in by 2nd April)
A.1 Programs and Compilation Units
A.1.1 Programs
compilation unit =
program module | definition module | implementation module ;
A.1.2 Program Modules
program module =
"MODULE", module identifier, [ protection ], ";",
import lists,
module block,
module identifier, "." ;
module identifier = identifier ;
protection = "[", constant expression, "]" ;
constant expression = expression ;
A.1.3 Definition Modules
definition module =
"DEFINITION", "MODULE", module identifier, ";",
import lists,
definitions,
"END", module identifier, "." ;
A.1.4 Implementation Modules
implementation module =
"IMPLEMENTATION", "MODULE", module identifier, [ protection ], ";",
import lists,
module block,
module identifier, "." ;
A.1.5 Module Blocks
module block = declarations, [ "BEGIN", statement sequence ], "END" ;
A.1.6 Import Lists
import lists = { import list } ;
import list = simple import | unqualified import ;
A.1.6.1 Simple Imports
simple import = "IMPORT", identifier list, ";" ;
A.1.6.2 Unqualified Imports
unqualified import =
"FROM", module identifier, "IMPORT", identifier list, ";";
A.1.7 Export Lists
export list = unqualified export | qualified export ;
A.1.7.1 Unqualified Exports
unqualified export = "EXPORT", identifier list, ";" ;
A.1.7.2 Qualified Exports
qualified export = "EXPORT", "QUALIFIED", identifier list, ";" ;
A.2 Definitions and Declarations
A.2.1 Definitions
definitions = { definition } ;
definition =
"CONST", { constant declaration, ";" } |
"TYPE", { type definition, ";" } |
"VAR", { variable declaration, ";" } |
procedure heading, ";" ;
type definition = type declaration | opaque type definition ;
procedure heading = proper procedure heading | function procedure heading ;
opaque type definition = identifier ;
A.2.2 Declarations
declarations = { declaration } ;
declaration =
"CONST", { constant declaration, ";" } |
"TYPE", { type declaration, ";" } |
"VAR", { variable declaration, ";" } |
procedure declaration, ";" |
module declaration, ";" ;
A.2.3 Constant Declarations
constant declaration = identifier, "=", constant expression ;
A.2.4 Type Declarations
type declaration = identifier, "=", type ;
A.2.5 Variable Declarations
variable declaration =
variable identifier list, ":", type ;
variable identifier list =
identifier, [ machine address ], { ",", identifier, [ machine address ] } ;
machine address= "[", value of machine address or address type, "]" ;
value of machine address or address type = constant expression ;
A.2.6 Types
type = type identifier | new type ;
type identifier = qualified identifier ;
A.2.7 New Types
new type =
enumeration type |
subrange type |
set type |
pointer type |
procedure type |
array type |
record type ;
A.2.7.1 Enumeration Types
enumeration type = "(", identifier list, ")" ;
identifier list = identifier, { ",", identifier } ;
A.2.7.2 Subrange Types
subrange type =
[ range type ], "[", constant expression, "..", constant expression, "]" ;
range type = ordinal type identifier ;
A.2.7.3 Set Types
set type = "SET", "OF", base type ;
base type = ordinal type ;
ordinal type = qualified identifier | enumeration type | subrange type;
ordinal type identifier = type identifier ;
A.2.7.4 Pointer Types
pointer type = "POINTER", "TO", bound type ;
bound type = type ;
A.2.7.5 Procedure Types
procedure type = proper procedure type | function procedure type ;
proper procedure type =
"PROCEDURE", [ formal parameter type list ] ;
function procedure type =
"PROCEDURE", formal parameter type list , function result ;
function result = ":", type identifier ;
formal parameter type list =
"(", [ formal parameter type, { ",", formal parameter type } ], ")" ;
formal parameter type = variable formal type | value formal type ;
variable formal type = "VAR", formal type ;
value formal type = formal type ;
formal type = { "ARRAY", "OF" }, type identifier ;
A.2.7.6 Array Types
array type = "ARRAY", index type, { ",", index type } , "OF", component type ;
index type = ordinal type ;
component type = type ;
A.2.7.7 Record Types
record type = "RECORD", [ field list ], "END" ;
field list = fields, { ";", fields } ;
fields = fixed fields | variant fields ;
fixed fields = identifier list, ":", type ;
variant fields =
"CASE", [ tag identifier ], ":", tag type, "OF",
variant, { "|", variant },
[ "ELSE", field list ], "END" ;
tag identifier = identifier ;
tag type = ordinal type identifier ;
variant = [ case label list, ":", field list ] ;
case label list = case label, { ",", case label } ;
case label = constant expression, [ "..", constant expression ] ;
A.2.8 Procedure Declarations
procedure declaration =
proper procedure declaration | function procedure declaration ;
A.2.8.1 Proper Procedure Declarations
proper procedure declaration =
proper procedure heading, ";",
("FORWARD" | proper procedure block, procedure identifier) ;
procedure identifier = identifier ;
A.2.8.2 Proper Procedure Headings
proper procedure heading =
"PROCEDURE", procedure identifier, [ formal parameter list ] ;
formal parameter list =
"(", [ formal parameter, { ";", formal parameter } ], ")" ;
A.2.8.3 Proper Procedure Block
proper procedure block =
declarations, [ "BEGIN", statement sequence ], "END" ;
A.2.8.4 Function Procedure Declarations
function procedure declaration =
function procedure heading, ";",
("FORWARD" | function procedure block, procedure identifier) ;
A.2.8.5 Function Procedure Headings
function procedure heading =
"PROCEDURE", procedure identifier, formal parameter list, function result ;
A.2.8.6 Function Procedure Block
function procedure block =
declarations, "BEGIN", statement sequence , "END" ;
+++++ Note: this may be a typographical error. Wirth had
function procedure block =
declarations, [ "BEGIN", statement sequence ], "END" ;
+++++ however, a function procedure MUST have a RETURN statement.
A.2.8.7 Parameters
formal parameter =
value parameter specification | variable parameter specification ;
value parameter specification = identifier list, ":", formal type ;
variable parameter specification =
"VAR", identifier list, ":", formal type ;
A.2.9 Module Declarations
module declaration =
"MODULE", module identifier, [ protection ], ";",
{ import lists },
[ export list ],
module block,
module identifier, "." ;
+++++ Note this may be a typographical error. All that is needed is
module declaration =
"MODULE", module identifier, [ protection ], ";",
import lists ,
[ export list ],
module block,
module identifier, "." ;
+++++ the {} is already present in the production for import lists in A.1.6
A.3 Statements
statement =
empty statement |
assignment statement |
procedure call |
return statement |
with statement |
if statement |
case statement |
while statement |
repeat statement |
loop statement |
exit statement |
for statement ;
A.3.1 Statement Sequences
statement sequence = statement, { ";", statement } ;
A.3.2 Empty Statements
empty statement = ;
A.3.3 Assignment Statements
assignment statement = variable designator, assignment operator, expression ;
assignment operator =":=" ;
A.3.4 Procedure Calls
procedure call = procedure designator, [ actual parameters ] ;
procedure designator = value designator ;
actual parameters = "(", [ actual parameter list ], ")" ;
actual parameter list = actual parameter, { ",", actual parameter } ;
actual parameter = variable designator | expression | type parameter ;
type parameter = type identifier ;
A.3.5 Return Statements
return statement = simple return statement | function return statement ;
A.3.5.1 Simple Return Statements
simple return statement = "RETURN" ;
A.3.5.2 Function Return Statements
function return statement = "RETURN", expression ;
A.3.6 With Statements
with statement = "WITH", record designator, "DO", statement sequence, "END" ;
record designator = variable designator | value designator ;
A.3.7 If Statements
if statement = guarded statements, [ "ELSE", statement sequence ], "END" ;
guarded statements =
"IF", Boolean expression, "THEN", statement sequence,
{ "ELSIF", Boolean expression, "THEN", statement sequence } ;
Boolean expression = expression ;
A.3.8 Case Statements
case statement =
"CASE", case selector, "OF",
case, { " | ", case },
[ "ELSE", statement sequence ],
"END" ;
case selector = ordinal expression ;
case = [ case label list, ":", statement sequence ] ;
+++++ Note that the productions
case label list = case label, { ",", case label } ;
case label = constant expression, [ "..", constant expression ] ;
+++++ have already been given in A.2.7.7
A.3.9 While statements
while statement =
"WHILE", Boolean expression, "DO", statement sequence, "END" ;
A.3.10 Repeat Statements
repeat statement =
"REPEAT", statement sequence, "UNTIL", Boolean expression ;
A.3.11 Loop Statements
loop statement = "LOOP", statement sequence, "END" ;
A.3.12 Exit Statements
exit statement = "EXIT" ;
A.3.13 For Statements
for statement =
"FOR", control variable, assignment operator, initial value,
"TO", final value,
[ "BY", step value],
"DO", statement sequence, "END" ;
--
uucp: uunet!m2xenix!puddle!5!494!4.101!Pat.Terry
Internet: Pat.Terry@p101.f4.n494.z5.fidonet.org
Technical Details
Message-ID:
<3517.261B25CB@puddle.fidonet.org>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!zephyr.ens.tek.com!tektronix!nosun!qiclab!m2xenix!puddle!p101.f4.n494.z5.fidonet.org!Pat.TerryShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!zephyr.ens.tek.com!tektronix!nosun!qiclab!m2xenix!puddle!p101.f4.n494.z5.fidonet.org!Pat.Terry From: Pat.Terry@p101.f4.n494.z5.fidonet.org (Pat Terry) Newsgroups: comp.lang.modula2 Subject: Re: m2 grammar Message-ID: <3517.261B25CB@puddle.fidonet.org> Date: 5 Apr 90 04:32:51 GMT Sender: ufgate@puddle.fidonet.org (newsout1.26) Organization: FidoNet node 5:494/4.101 - Settler City Fido, Grahamstown RSA Lines: 439