p2_pascal.atg 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. (* Tags: ORIGINAL, OISOberon, Classic, ETHOberon *)
  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 "]" | "(" qualident ")" | "^" } .
  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 = [ assignment | ProcedureCall | IfStatement |
  54. CaseStatement | WhileStatement | RepeatStatement |
  55. LoopStatement | WithStatement | "EXIT" |
  56. "RETURN" [ expression ] ] .
  57. assignment = designator ":=" expression .
  58. ProcedureCall = designator [ 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. WithStatement = "WITH" qualident ":" qualident "DO" StatementSequence "END" .
  72. ProcedureDeclaration = ProcedureHeading ";" ProcedureBody ident .
  73. ProcedureHeading = "PROCEDURE" [ "*" ] identdef [ FormalParameters ] .
  74. ProcedureBody = DeclarationSequence [ "BEGIN" StatementSequence ] "END" .
  75. ForwardDeclaration = "PROCEDURE" "^" ident [ "*" ] [ FormalParameters ] .
  76. DeclarationSequence = { "CONST" { ConstantDeclaration ";" }
  77. | "TYPE" { TypeDeclaration ";" }
  78. | "VAR" { VariableDeclaration ";" } }
  79. { ProcedureDeclaration ";"
  80. | ForwardDeclaration ";" } .
  81. FormalParameters = "(" [ FPSection { ";" FPSection } ] ")" [ ":" qualident ] .
  82. FPSection = [ "VAR" ] ident { "," ident } ":" FormalType .
  83. FormalType = { "ARRAY" "OF" } (qualident | ProcedureType) .
  84. ImportList = "IMPORT" import { "," import } ";" .
  85. import = ident [ ":=" ident ] .
  86. module = "MODULE" ident ";"
  87. [ ImportList ] DeclarationSequence
  88. [ "BEGIN" StatementSequence ] "END" ident "." .
  89. END Oberon.