ulm_oberon.atg 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. COMPILER Oberon
  2. IGNORE CASE
  3. (* Oberon grammar extracted from Ulm's Oberon compiler (ulmo)
  4. Source: src/compiler/OberonParser.om and src/compiler/OberonLex.om
  5. Coco/R grammar for the Oberon language as implemented by ulmo 0.11.0 *)
  6. (* ------------------------------------------------------------------ *)
  7. (* Character classes *)
  8. (* ------------------------------------------------------------------ *)
  9. CHARACTERS
  10. letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
  11. digit = "0123456789" .
  12. hexdigit = digit + "ABCDEF" .
  13. noQuote = ANY - '"' - "'" .
  14. eol = CHR(10) .
  15. IGNORE CHR(9) .. CHR(13)
  16. COMMENTS FROM "(*" TO "*)" NESTED
  17. (* ------------------------------------------------------------------ *)
  18. (* Scanner tokens *)
  19. (* ------------------------------------------------------------------ *)
  20. TOKENS
  21. ident = letter { letter | digit } .
  22. integer = digit { digit } | digit { hexdigit } "H" .
  23. real = digit { digit } "." { digit }
  24. [ ("E" | "D") [ "+" | "-" ] digit { digit } ] .
  25. charconst = digit { hexdigit } "X" .
  26. string = '"' { noQuote } '"' .
  27. (* ------------------------------------------------------------------ *)
  28. (* Grammar productions *)
  29. (* ------------------------------------------------------------------ *)
  30. PRODUCTIONS
  31. Oberon = CompilationUnit .
  32. CompilationUnit = DefinitionModule | Module .
  33. DefinitionModule = "DEFINITION" ident ";"
  34. [ ImportList ]
  35. [ DefinitionSequence ]
  36. "END" ident "."
  37. .
  38. DefinitionSequence = { ConstSection | TypeSection | VarSection | DefProcDecl } .
  39. DefProcDecl = ProcedureHeading ";" .
  40. Module = "MODULE" ident ";"
  41. [ ImportList ]
  42. [ DeclarationSequence ]
  43. [ "BEGIN" StatementSequence ]
  44. "END" ident "."
  45. .
  46. ImportList = "IMPORT"
  47. ( ident [ ":=" ident ]
  48. { "," ident [ ":=" ident ] } )
  49. ";"
  50. .
  51. DeclarationSequence = { ConstSection | TypeSection | VarSection | ProcedureDeclaration ";" }
  52. { ForwardDeclaration ";" } .
  53. ConstSection = "CONST" { ConstantDeclaration ";" } .
  54. TypeSection = "TYPE" { TypeDeclaration ";" } .
  55. VarSection = "VAR" { VariableDeclaration ";" } .
  56. ConstantDeclaration = identdef "=" ConstExpression .
  57. TypeDeclaration = identdef "=" type .
  58. VariableDeclaration = IdentList ":" type .
  59. identdef = ident [ "*" ] .
  60. IdentList = identdef { "," identdef } .
  61. qualident = ident { "." ident } .
  62. ConstExpression = expression .
  63. type = qualident | ArrayType | RecordType
  64. | PointerType | ProcedureType .
  65. ArrayType = "ARRAY" length { "," length } "OF" type .
  66. length = ConstExpression .
  67. RecordType = "RECORD" [ "(" BaseType ")" ]
  68. FieldListSequence "END" .
  69. BaseType = qualident .
  70. FieldListSequence = FieldList { ";" FieldList } .
  71. FieldList = [ IdentList ":" type ] .
  72. PointerType = "POINTER" "TO" type .
  73. ProcedureType = "PROCEDURE" [ FormalParameters ] .
  74. FormalParameters = "(" [ FPSection { ";" FPSection } ] ")"
  75. [ ":" qualident ] .
  76. FPSection = [ "VAR" ] ident { "," ident } ":" FormalType .
  77. FormalType = { "ARRAY" "OF" } ( qualident | ProcedureType ) .
  78. designator = qualident
  79. { "." ident | "[" ExpList "]"
  80. | "(" [ ExpList ] ")" | "^" } .
  81. ExpList = expression { "," expression } .
  82. expression = SimpleExpression [ relation SimpleExpression ] .
  83. relation = "=" | "#" | "<" | "<=" | ">" | ">=" | "IN" | "IS" .
  84. SimpleExpression = [ "+" | "-" ] term { AddOperator term } .
  85. AddOperator = "+" | "-" | "OR" .
  86. term = factor { MulOperator factor } .
  87. MulOperator = "*" | "/" | "DIV" | "MOD" | "&" .
  88. factor = number | charconst | string | "NIL" | set
  89. | designator [ ActualParameters ]
  90. | "(" expression ")" | "~" factor .
  91. number = integer | real .
  92. set = "{" [ element { "," element } ] "}" .
  93. element = expression [ ".." expression ] .
  94. ActualParameters = "(" [ ExpList ] ")" .
  95. statement = [ ident IdentTail | IfStatement
  96. | CaseStatement | WhileStatement | RepeatStatement
  97. | LoopStatement | WithStatement | "EXIT"
  98. | "RETURN" [ expression ] ] .
  99. IdentTail = { "." ident | "[" ExpList "]" | "(" [ ExpList ] ")" | "^" }
  100. [ ":=" expression | "(" [ ExpList ] ")" ] .
  101. StatementSequence = statement { ";" statement } .
  102. IfStatement = "IF" expression "THEN" StatementSequence
  103. { "ELSIF" expression "THEN" StatementSequence }
  104. [ "ELSE" StatementSequence ] "END" .
  105. CaseStatement = "CASE" expression "OF" case { "|" case }
  106. [ "ELSE" StatementSequence ] "END" .
  107. case = [ CaseLabelList ":" StatementSequence ] .
  108. CaseLabelList = CaseLabels { "," CaseLabels } .
  109. CaseLabels = ConstExpression [ ".." ConstExpression ] .
  110. WhileStatement = "WHILE" expression "DO" StatementSequence "END" .
  111. RepeatStatement = "REPEAT" StatementSequence "UNTIL" expression .
  112. LoopStatement = "LOOP" StatementSequence "END" .
  113. WithStatement = "WITH" qualident ":" qualident "DO" StatementSequence "END" .
  114. ProcedureDeclaration = ProcedureHeading ";" ProcedureBody ident .
  115. ProcedureHeading = "PROCEDURE" [ "*" ] identdef [ FormalParameters ] .
  116. ProcedureBody = DeclarationSequence [ "BEGIN" StatementSequence ] "END" .
  117. ForwardDeclaration = "PROCEDURE" "^" ident [ "*" ] [ FormalParameters ] .
  118. END Oberon.