oberon2_oberon.atg 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. (* Tags: OBERON2, TypeBound, Vischap, Extended *)
  2. COMPILER Oberon
  3. (* A grammar for Oberon *)
  4. CHARACTERS
  5. eol = CHR(13) .
  6. letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
  7. digit = "0123456789" .
  8. hexDigit = digit + "ABCDEF" .
  9. noQuote = ANY - "'" - eol .
  10. IGNORE CHR(9) .. CHR(13)
  11. COMMENTS
  12. FROM "(*" TO "*)" NESTED
  13. TOKENS
  14. ident = letter { letter | digit } .
  15. integer = digit { digit } | digit { hexDigit } "H" .
  16. real = digit { digit } "." { digit }
  17. [ ("E" | "D") [ "+" | "-" ] digit { digit } ] .
  18. CharConstant = digit { hexDigit } "X" .
  19. string = "'" { noQuote } "'" .
  20. PRODUCTIONS
  21. Oberon = module .
  22. number = integer | real .
  23. identdef = ident [ "*" ] .
  24. qualident = ident { "." ident } .
  25. ConstantDeclaration = identdef "=" ConstExpression .
  26. ConstExpression = expression .
  27. TypeDeclaration = identdef "=" type .
  28. type = qualident | ArrayType | RecordType | PointerType | ProcedureType .
  29. ArrayType = "ARRAY" length { "," length } "OF" type .
  30. length = ConstExpression .
  31. RecordType = "RECORD" [ "(" BaseType ")" ] FieldListSequence "END" .
  32. BaseType = qualident .
  33. FieldListSequence = FieldList { ";" FieldList } .
  34. FieldList = [ IdentList ":" type ] .
  35. IdentList = identdef { "," identdef } .
  36. PointerType = "POINTER" "TO" type .
  37. ProcedureType = "PROCEDURE" [ FormalParameters ] .
  38. VariableDeclaration = IdentList ":" type .
  39. designator = qualident { "." ident | "[" ExpList "]" | "(" [ ExpList ] ")" | "^" } .
  40. ExpList = expression { "," expression } .
  41. expression = SimpleExpression [ relation SimpleExpression ] .
  42. relation = "=" | "#" | "<" | "<=" | ">" | ">=" | "IN" | "IS" .
  43. SimpleExpression = [ "+" | "-" ] term { AddOperator term } .
  44. AddOperator = "+" | "-" | "OR" .
  45. term = factor { MulOperator factor } .
  46. MulOperator = "*" | "/" | "DIV" | "MOD" | "&" .
  47. factor = number | CharConstant | string | "NIL" | set
  48. | designator [ ActualParameters ]
  49. | "(" expression ")" | "~" factor .
  50. set = "{" [ element { "," element } ] "}" .
  51. element = expression [ ".." expression ] .
  52. ActualParameters = "(" [ ExpList ] ")" .
  53. statement = [ ident IdentTail | IfStatement |
  54. CaseStatement | WhileStatement | RepeatStatement |
  55. LoopStatement | ForStatement | WithStatement | "EXIT" |
  56. "RETURN" [ expression ] ] .
  57. IdentTail = { "." ident | "[" ExpList "]" | "(" [ ExpList ] ")" | "^" }
  58. [ ":=" expression | "(" [ ExpList ] ")" ] .
  59. StatementSequence = statement { ";" statement } .
  60. IfStatement = "IF" expression "THEN" StatementSequence
  61. { "ELSIF" expression "THEN" StatementSequence }
  62. [ "ELSE" StatementSequence ] "END" .
  63. CaseStatement = "CASE" expression "OF" case { "|" case }
  64. [ "ELSE" StatementSequence ] "END" .
  65. case = [ CaseLabelList ":" StatementSequence ] .
  66. CaseLabelList = CaseLabels { "," CaseLabels } .
  67. CaseLabels = ConstExpression [ ".." ConstExpression ] .
  68. WhileStatement = "WHILE" expression "DO" StatementSequence "END" .
  69. RepeatStatement = "REPEAT" StatementSequence "UNTIL" expression .
  70. LoopStatement = "LOOP" StatementSequence "END" .
  71. ForStatement = "FOR" ident ":=" expression "TO" expression
  72. [ "BY" ConstExpression ] "DO" StatementSequence "END" .
  73. WithStatement = "WITH" qualident ":" qualident "DO" StatementSequence "END" .
  74. ProcedureDeclaration = ProcedureHeading ";" ProcedureBody ident .
  75. ProcedureHeading = "PROCEDURE" [ "(" [ "VAR" ] ident ":" ident ")" ] [ "*" ] identdef [ FormalParameters ] .
  76. ProcedureBody = DeclarationSequence [ "BEGIN" StatementSequence ] "END" .
  77. ForwardDeclaration = "PROCEDURE" "^" ident [ "*" ] [ FormalParameters ] .
  78. DeclarationSequence = { "CONST" { ConstantDeclaration ";" }
  79. | "TYPE" { TypeDeclaration ";" }
  80. | "VAR" { VariableDeclaration ";" } }
  81. { ProcedureDeclaration ";"
  82. | ForwardDeclaration ";" } .
  83. FormalParameters = "(" [ FPSection { ";" FPSection } ] ")" [ ":" qualident ] .
  84. FPSection = [ "VAR" ] ident { "," ident } ":" FormalType .
  85. FormalType = { "ARRAY" "OF" } (qualident | ProcedureType) .
  86. ImportList = "IMPORT" import { "," import } ";" .
  87. import = ident [ ":=" ident ] .
  88. module = "MODULE" ident ";"
  89. [ ImportList ] DeclarationSequence
  90. [ "BEGIN" StatementSequence ] "END" ident "." .
  91. END Oberon.