oberon.atg 4.5 KB

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