Browsable Modula-3 Grammar

CC-BY

Grammar extracted by Vadim Zaytsev, see the Grammar Zoo entry for details: modula/modula-3/dagenais-cordy/extracted
Source used for this grammar: Michel Dagenais, James R. Cordy, Modula-3 Grammar for TXL-3, Adapted to TXL 10. Rev 21.3.01, March 2001 [TGZ]

Summary

Syntax

program ::=
	Interface
	Module
	GInterface
	GModule
	IInterface
	IModule
Interface ::=
	"UNSAFE"? "INTERFACE" id ";" (NL NL) Import* Decl* NL "END" id "." NL
IInterface ::=
	"UNSAFE"? "INTERFACE" id "=" id GenActls "END" id "." NL
Module ::=
	"UNSAFE"? "MODULE" id Exports? ";" (NL NL) Import* Block id "." NL
IModule ::=
	"UNSAFE"? "MODULE" id Exports? "=" id GenActls "END" id "." NL
GInterface ::=
	"GENERIC" "INTERFACE" id GenFmls ";" Import* Decl* NL "END" id "." NL
GModule ::=
	"GENERIC" "MODULE" id GenFmls ";" (NL NL) Import* Block id "." NL
Exports ::=
	"EXPORTS" IdList
Import ::=
	AsImport
	FromImport (NL NL)
AsImport ::=
	"IMPORT" ImportItem ComaImportItem* ";" (NL NL)
ComaImportItem ::=
	"," ImportItem
FromImport ::=
	"FROM" id "IMPORT" IdList ";" (NL NL)
Block ::=
	Decl* "BEGIN" Stmts "END"
Decl ::=
	ProcedureHead EqualBlock? ";" (NL NL)
	"VAR" (IN NL) VariableDecl* EX
	"TYPE" (IN NL) TypeDecl* (EX NL)
	"CONST" (IN NL) ConstDecl* (EX NL)
	"EXCEPTION" (IN NL) ExceptionDecl* (EX NL)
	"REVEAL" (IN NL) RevealDecl* (EX NL)
EqualBlock ::=
	"=" (IN NL) Block id EX
GenFmls ::=
	"(" IdList? ")"
GenActls ::=
	"(" IdList? ")"
ImportItem ::=
	id AsId?
AsId ::=
	"AS" id
ConstDecl ::=
	id ColonType? "=" ConstExpr ";" NL
ColonType ::=
	":" Type
TypeDecl ::=
	id "=" Type ";" NL
	id "<:" Type ";" NL
RevealDecl ::=
	QualId "=" Type ";" NL
	QualId "<:" Type ";" NL
ExceptionDecl ::=
	id ParenthesizedType? ";" NL
ParenthesizedType ::=
	"(" Type ")"
VariableDecl ::=
	IdList ColonType? EqualExpr? ";" NL
EqualExpr ::=
	":=" Expr
ProcedureHead ::=
	"PROCEDURE" id Signature
Signature ::=
	"(" Formals? ")" ColonType? Raises?
Raises ::=
	"RAISES" RaisesList
Formals ::=
	Formal SemicolonFormal* ";"?
SemicolonFormal ::=
	";" Formal
Formal ::=
	Mode? IdList ColonType? EqualConstExpr?
EqualConstExpr ::=
	":=" ConstExpr
Mode ::=
	"VALUE"
	"VAR"
	"READONLY"
RaisesList ::=
	"{" {QualId ","}* "}"
	"ANY"
Stmts ::=
	(IN NL) OptStmts? EX
OptStmts ::=
	Stmt SemiColonStmt* ";"? NL
SemiColonStmt ::=
	";" NL Stmt
Stmt ::=
	IfStmt
	ForStmt
	ReturnStmt
	WhileStmt
	AssignStmt
	CallStmt
	Block
	CaseStmt
	ExitStmt
	EvalStmt
	LockStmt
	LoopStmt
	RaiseStmt
	RepeatStmt
	TypecaseStmt
	TryStmt
	WithStmt
AssignStmt ::=
	Expr ":=" Expr
CallStmt ::=
	Expr "(" {Actual ","}* ")"
CaseStmt ::=
	"CASE" Expr "OF" NL Case? VerticalCase* ElseStmts? "END"
VerticalCase ::=
	"|" Case
ElseStmts ::=
	"ELSE" Stmts
ExitStmt ::=
	"EXIT"
EvalStmt ::=
	"EVAL" Expr
ForStmt ::=
	"FOR" id ":=" Expr "TO" Expr ByExpr? "DO" Stmts "END"
ByExpr ::=
	"BY" Expr
IfStmt ::=
	"IF" Expr "THEN" Stmts Elsif* ElseStmts? "END"
Elsif ::=
	"ELSIF" Expr "THEN" Stmts
LockStmt ::=
	"LOCK" Expr "DO" Stmts "END"
LoopStmt ::=
	"LOOP" Stmts "END"
RaiseStmt ::=
	"RAISE" QualId ParenthesizedExpr?
ParenthesizedExpr ::=
	"(" Expr ")"
RepeatStmt ::=
	"REPEAT" Stmts "UNTIL" Expr
ReturnStmt ::=
	"RETURN" Expr?
TypecaseStmt ::=
	"TYPECASE" Expr "OF" NL TCase? VerticalTCase* ElseStmts? "END"
VerticalTCase ::=
	"|" TCase
TryStmt ::=
	"TRY" Stmts ExceptOrFinally "END"
ExceptOrFinally ::=
	"EXCEPT" NL Handler? VerticalHandler* ElseStmts?
	"FINALLY" Stmts
VerticalHandler ::=
	"|" Handler
WhileStmt ::=
	"WHILE" Expr "DO" Stmts "END"
WithStmt ::=
	"WITH" Binding+ "DO" Stmts "END"
Case ::=
	{Labels ","}* "=>" Stmts
Labels ::=
	ConstExpr DotDotConstExpr?
DotDotConstExpr ::=
	".." ConstExpr
Handler ::=
	{QualId ","}* ParenthesizedId? "=>" Stmts
ParenthesizedId ::=
	"(" id ")"
TCase ::=
	{Type ","}* ParenthesizedId? "=>" Stmts
Binding ::=
	id "=" Expr
Actual ::=
	Type
	Expr
	id ":=" Expr
Type ::=
	TypeName
	ArrayType
	EnumerationType
	RecordType
	ObjectType
	ProcedureType
	RefType
	SetType
	SubrangeType
	PackedType
	"(" Type ")"
ArrayType ::=
	"ARRAY" {Type ","}* "OF" Type
PackedType ::=
	"BITS" ConstExpr "FOR" Type
EnumerationType ::=
	"{" IdList? "}"
ObjectType ::=
	ObjectOrTypeName? Brand? "OBJECT" (IN NL) Fields TheMethods? TheOverrides? "END" EX
ObjectOrTypeName ::=
	TypeName
	ObjectType
TheMethods ::=
	"METHODS" NL Methods
TheOverrides ::=
	"OVERRIDES" NL Overrides
ProcedureType ::=
	"PROCEDURE" Signature
RecordType ::=
	"RECORD" (IN NL) Fields "END" EX
RefType ::=
	"UNTRACED"? Brand? "REF" Type
SetType ::=
	"SET" "OF" Type
SubrangeType ::=
	"[" ConstExpr ".." ConstExpr "]"
Brand ::=
	"BRANDED" ConstExpr?
Fields ::=
	FieldList?
FieldList ::=
	IN Field SemiColonField* ";"? (EX NL)
SemiColonField ::=
	";" NL Field
Field ::=
	IdList ColonType? EqualConstExpr?
Methods ::=
	MethodList?
MethodList ::=
	IN Method SemiColonMethod* ";"? (EX NL)
SemiColonMethod ::=
	";" NL Method
Method ::=
	id Signature EqualConstExpr?
Overrides ::=
	OverrideList?
OverrideList ::=
	IN Override SemiColonOverride* ";"? (EX NL)
SemiColonOverride ::=
	";" NL Override
Override ::=
	id ":=" ConstExpr
ConstExpr ::=
	Expr
Expr ::=
	E1 OrE1*
OrE1 ::=
	"OR" E1
E1 ::=
	E2 AndE2*
AndE2 ::=
	"AND" E2
E2 ::=
	"NOT"* E3
E3 ::=
	E4 RelOpE4*
RelOpE4 ::=
	RelOp E4
E4 ::=
	E5 AddOpE5*
AddOpE5 ::=
	AddOp E5
E5 ::=
	E6 MulOpE6*
MulOpE6 ::=
	MulOp E6
E6 ::=
	SignOp* E7
E7 ::=
	E8 Selector*
E8 ::=
	id
	Number
	charlit
	stringlit
	"(" Expr ")"
	Constructor
SignOp ::=
	"+"
	"-"
RelOp ::=
	"="
	"#"
	"<"
	"<="
	">"
	">="
	"IN"
AddOp ::=
	"+"
	"-"
	"&"
MulOp ::=
	"*"
	"/"
	"DIV"
	"MOD"
Selector ::=
	"." id
	"[" Expr ComaExpr* "]"
	"(" {Actual ","}* ")"
	"^"
ComaExpr ::=
	"," Expr
Constructor ::=
	Type "{" AnyCons "}"
AnyCons ::=
	SetCons
	RecordCons
	ArrayCons
SetCons ::=
	{SetElt ","}*
SetElt ::=
	Expr DotDotExpr?
DotDotExpr ::=
	".." Expr
RecordCons ::=
	{RecordElt ","}*
RecordElt ::=
	Expr EqualExpr?
ArrayCons ::=
	{Expr ","}* ComaDotDot?
ComaDotDot ::=
	"," ".."
IdList ::=
	{id ","}*
QualId ::=
	id DotId?
DotId ::=
	"." id
TypeName ::=
	QualId
	"ROOT"
	"UNTRACED" "ROOT"

GrammarLabMaintained by Dr. Vadim Zaytsev a.k.a. @grammarware. Last updated in September 2015. []