Last active
May 30, 2022 20:07
-
-
Save tkurtbond/c4d7f10de6d5ef808dc0cec82bf6e339 to your computer and use it in GitHub Desktop.
Coco/R grammar for Oberon-2, from the body of the Oberon-2 report.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
COMPILER Oberon2 | |
CHARACTERS | |
letter = 'A' .. 'Z' + 'a' .. 'z'. | |
digit = "0123456789". | |
hexDigit = digit + 'A' .. 'F'. | |
sqChar = ANY - '"'. | |
dqChar = ANY - "'". | |
TOKENS | |
ident = letter {letter | digit}. | |
integer = digit {digit} | digit {hexDigit} "H". | |
real = digit {digit} "." {digit} [("E" | "D")["+" | "-"] digit {digit}]. | |
character = digit {hexDigit} "X". | |
string = '"' {sqChar} '"' | "'" {dqChar} "'". | |
COMMENTS FROM "(*" TO "*)" NESTED | |
PRODUCTIONS | |
number = integer | real. | |
Qualident = [ident "."] ident. | |
IdentList = IdentDef {"," IdentDef}. | |
IdentDef = ident ["*" | "-"]. | |
ConstExpression = Expression. | |
TypeDeclaration = IdentDef "=" Type. | |
Type = Qualident | ArrayType | RecordType | PointerType | ProcedureType. | |
ArrayType = "ARRAY" [Length {"," Length}] "OF" Type. | |
Length = ConstExpression. | |
RecordType = "RECORD" ["(" BaseType ")"] FieldList {";" FieldList} "END". | |
BaseType = Qualident. | |
FieldList = [IdentList ":" Type]. | |
PointerType = "POINTER" "TO" Type. | |
ProcedureType = "PROCEDURE" [FormalParameters]. | |
VariableDeclaration = IdentList ":" Type. | |
Designator = Qualident {"." ident | "[" ExpressionList "]" | "^" | |
| "(" Qualident ")"}. | |
ExpressionList = Expression {"," Expression}. | |
Expression = SimpleExpression [Relation SimpleExpression]. | |
SimpleExpression = ["+" | "-"] Term {AddOperator Term}. | |
Term = Factor {MulOperator Factor}. | |
Factor = Designator [ActualParameters] | |
| number | character | string | "NIL" | Set | "(" Expression ")" | |
| "~" Factor. | |
Set = "{" [Element {"," Element}] "}". | |
Element = Expression [".." Expression]. | |
ActualParameters = "(" ExpressionList ")". | |
Relation = "=" | "#" | "<" | "<=" | ">" | ">=" | "IN" | "IS". | |
AddOperator = "+" | "-" | "OR". | |
MulOperator = "*" | "/" | "DIV" | "MOD" | "&". | |
Statement = [Assignment | ProcedureCall | IfStatement | CaseStatement | |
| WhileStatement | RepeatStatement | ForStatement | |
| LoopStatement | WithStatement | |
| "EXIT" | "RETURN" [Expression]]. | |
Assignment = Designator ":=" Expression. | |
ProcedureCall = Designator [ActualParameters]. | |
StatementSequence = Statement {";" Statement}. | |
IfStatement = "IF" Expression "THEN" StatementSequence | |
{"ELSIF" Expression "THEN" StatementSequence} | |
["ELSE" StatementSequence] | |
"END". | |
CaseStatement = "CASE" Expression "OF" Case {"|" Case} | |
["ELSE" StatementSequence] "END". | |
Case = [CaseLabelList ":" StatementSequence]. | |
CaseLabelList = CaseLabels {"," CaseLabels}. | |
CaseLabels = ConstExpression [".." ConstExpression]. | |
WhileStatement = "WHILE" Expression "DO" StatementSequence "END". | |
RepeatStatement = "REPEAT" StatementSequence "UNTIL" Expression. | |
ForStatement = "FOR" ident ":=" Expression "TO" Expression | |
["BY" ConstExpression] "DO" StatementSequence "END". | |
LoopStatement = "LOOP" StatementSequence "END". | |
WithStatement = "WITH" Guard "DO" StatementSequence | |
{"|" Guard "DO" StatementSequence} | |
["ELSE" StatementSequence] "END". | |
Guard = Qualident ":" Qualident. | |
ProcedureDeclaration = ProcedureHeading ";" ProcedureBody ident. | |
ProcedureHeading = "PROCEDURE" [Receiver] IdentDef [FormalParameters]. | |
Receiver = "(" ["VAR"] ident ":" ident ")". | |
ProcedureBody = DeclarationSequence ["BEGIN" StatementSequence] "END". | |
DeclarationSequence = {"CONST" {ConstDeclaration ";"} | | |
"TYPE" {TypeDeclaration ";"} | | |
"VAR" {VariableDeclaration ";"} } | |
{ProcedureDeclaration ";" | ForwardDeclaration ";"}. | |
ConstDeclaration = IdentDef "=" ConstExpression. | |
ForwardDeclaration = "PROCEDURE" "^" [Receiver] IdentDef [FormalParameters]. | |
FormalParameters = "(" [FPSection {";" FPSection}] ")" [":" Qualident]. | |
FPSection = ["VAR"] ident {"." ident} ":" Type. | |
Module = "MODULE" ident ";" [ImportList] DeclarationSequence | |
["BEGIN" StatementSequence] "END" ident ".". | |
ImportList = "IMPORT" Import {"," Import} ";". | |
Import = [ident ":="] ident. | |
Oberon2 = Module. | |
END Oberon2. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment