oberon07_oberon.atg 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. (* Tags: OBERON07, Modern, Simplified, GC *)
  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 { digit } CONTEXT ( ".." )
  16. | digit { hexDigit } "H" .
  17. real = digit { digit } "." digit { digit }
  18. [ ("E" | "D") [ "+" | "-" ] digit { digit } ] .
  19. CharConstant = digit { hexDigit } "X" .
  20. string = '"' { noQuote } '"' .
  21. PRODUCTIONS
  22. Oberon = module .
  23. number = integer | real .
  24. identdef = ident [ "*" ] .
  25. qualident = ident { "." ident } .
  26. ConstantDeclaration = identdef "=" ConstExpression .
  27. ConstExpression = expression .
  28. TypeDeclaration = identdef "=" type .
  29. type = qualident | ArrayType | RecordType | PointerType | ProcedureType .
  30. ArrayType = "ARRAY" length { "," length } "OF" type .
  31. length = ConstExpression .
  32. RecordType = "RECORD" [ "(" BaseType ")" ] FieldListSequence "END" .
  33. BaseType = qualident .
  34. FieldListSequence = FieldList { ";" FieldList } .
  35. FieldList = [ IdentList ":" type ] .
  36. IdentList = identdef { "," identdef } .
  37. PointerType = "POINTER" "TO" type .
  38. ProcedureType = "PROCEDURE" [ FormalParameters ] .
  39. VariableDeclaration = IdentList ":" type .
  40. designator = qualident { "." ident | "[" ExpList "]" | "^" } .
  41. ExpList = expression { "," expression } .
  42. expression = SimpleExpression [ relation SimpleExpression ] .
  43. relation = "=" | "#" | "<" | "<=" | ">" | ">=" | "IN" | "IS" .
  44. SimpleExpression = [ "+" | "-" ] term { AddOperator term } .
  45. AddOperator = "+" | "-" | "OR" .
  46. term = factor { MulOperator factor } .
  47. MulOperator = "*" | "/" | "DIV" | "MOD" | "&" .
  48. factor = number | CharConstant | string | "NIL" | set
  49. | designator [ ActualParameters ]
  50. | "(" expression ")" | "~" factor .
  51. set = "{" [ element { "," element } ] "}" .
  52. element = expression [ ".." expression ] .
  53. ActualParameters = "(" [ ExpList ] ")" .
  54. statement = [ AssignmentOrProcedureCall | IfStatement |
  55. CaseStatement | WhileStatement | RepeatStatement |
  56. LoopStatement | ForStatement | WithStatement | "EXIT" |
  57. "RETURN" [ expression ] ] .
  58. AssignmentOrProcedureCall = designator ( ":=" expression | [ ActualParameters ] ) .
  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" [ "*" ] 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.