pimmod2.atg 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. COMPILER Mod2
  2. (* COCO/R Grammar for Modula-2 based on "Programming in Modula-2 *)
  3. CHARACTERS
  4. eol = CHR(13) .
  5. letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
  6. octDigit = "01234567" .
  7. digit = octDigit + '89' .
  8. hexDigit = digit + 'ABCDEF' .
  9. noQuote1 = ANY - "'" - eol .
  10. noQuote2 = ANY - '"' - eol .
  11. IGNORE CHR(9).. CHR(13)
  12. COMMENTS
  13. FROM '(*' TO '*)' NESTED
  14. TOKENS
  15. ident = letter { letter | digit } .
  16. integer = digit { digit }
  17. | digit { digit } CONTEXT ("..")
  18. | octDigit { octDigit } ("B" | "C")
  19. | digit { hexDigit } "H" .
  20. real = digit { digit } "." { digit }
  21. [ "E" [ "+" | "-" ] digit { digit } ] .
  22. string = "'" { noQuote1 } "'"
  23. | '"' { noQuote2 } '"' .
  24. PRODUCTIONS
  25. Mod2 = DefinitionModule | [ "IMPLEMENTATION" ] ProgramModule .
  26. Number = integer | real .
  27. QualIdent = ident { "." ident } .
  28. ConstantDeclaration = ident "=" ConstExpression .
  29. ConstExpression = Expression .
  30. TypeDeclaration = ident "=" Type .
  31. Type = SimpleType | ArrayType | RecordType | SetType
  32. | PointerType | ProcedureType .
  33. SimpleType = QualIdent | Enumeration | SubrangeType .
  34. Enumeration = "(" IdentList ")" .
  35. IdentList = ident { "," ident } .
  36. SubrangeType = [ ident ] "[" ConstExpression ".." ConstExpression "]" .
  37. ArrayType = "ARRAY" SimpleType { "," SimpleType } "OF" Type .
  38. RecordType = "RECORD" FieldListSequence "END" .
  39. FieldListSequence = FieldList { ";" FieldList } .
  40. FieldList = [ IdentList ":" Type
  41. | "CASE" [ ident ] ":" QualIdent "OF"
  42. Variant { "|" Variant }
  43. [ "ELSE" FieldListSequence ] "END" ] .
  44. Variant = [ CaseLabelList ":" FieldListSequence ] .
  45. CaseLabelList = CaseLabels { "," CaseLabels } .
  46. CaseLabels = ConstExpression [ ".." ConstExpression ] .
  47. SetType = "SET" "OF" SimpleType .
  48. PointerType = "POINTER" "TO" Type .
  49. ProcedureType = "PROCEDURE" [ FormalTypeList ] .
  50. FormalTypeList = "(" [ [ "VAR" ] FormalType
  51. { "," [ "VAR" ] FormalType } ] ")" [ ":" QualIdent ] .
  52. VariableDeclaration = IdentList ":" Type .
  53. Designator = QualIdent { "." ident | "[" ExpList "]" | "^" } .
  54. ExpList = Expression { "," Expression } .
  55. Expression = SimpleExpression [ Relation SimpleExpression ] .
  56. Relation = "=" | "#" | "<" |"<=" | ">" | ">=" | "IN" .
  57. SimpleExpression = [ "+" | "-" ] Term { AddOperator Term } .
  58. AddOperator = "+" | "-" | "OR" .
  59. Term = Factor { MulOperator Factor } .
  60. MulOperator = "*" |"/" | "DIV" | "MOD" | "AND" .
  61. Factor = Number | string | Set | "NOT" Factor
  62. | Designator [ ActualParameters ]
  63. | "(" Expression ")" .
  64. Set = [ QualIdent ] "{" [ Element { "," Element } ] "}" .
  65. Element = Expression [ ".." Expression ] .
  66. ActualParameters = "(" [ ExpList ] ")" .
  67. Statement = [ Assignment | ProcedureCall | IfStatement
  68. | CaseStatement | WhileStatement | RepeatStatement
  69. | LoopStatement | ForStatement | WithStatement
  70. | "EXIT" | "RETURN" [ Expression ] ] .
  71. Assignment = Designator ":=" Expression .
  72. ProcedureCall = Designator [ ActualParameters ] .
  73. StatementSequence = Statement { ";" Statement } .
  74. IfStatement = "IF" Expression "THEN" StatementSequence
  75. { "ELSIF" Expression "THEN" StatementSequence }
  76. [ "ELSE" StatementSequence ] "END" .
  77. CaseStatement = "CASE" Expression "OF" Case { "|" Case }
  78. [ "ELSE" StatementSequence ] "END" .
  79. Case = [ CaseLabelList ":" StatementSequence ] .
  80. WhileStatement = "WHILE" Expression "DO" StatementSequence "END" .
  81. RepeatStatement = "REPEAT" StatementSequence "UNTIL" Expression .
  82. ForStatement = "FOR" ident ":=" Expression "TO" Expression
  83. [ "BY" ConstExpression ] "DO" StatementSequence "END" .
  84. LoopStatement = "LOOP" StatementSequence "END" .
  85. WithStatement = "WITH" Designator "DO" StatementSequence "END" .
  86. ProcedureDeclaration = ProcedureHeading ";" Block ident .
  87. ProcedureHeading = "PROCEDURE" ident [ FormalParameters ] .
  88. Block = { Declaration } [ "BEGIN" StatementSequence ] "END" .
  89. Declaration = "CONST" { ConstantDeclaration ";" }
  90. | "TYPE" { TypeDeclaration ";" }
  91. | "VAR" { VariableDeclaration ";" }
  92. | ProcedureDeclaration ";"
  93. | ModuleDeclaration ";" .
  94. FormalParameters = "(" [ FPSection { ";" FPSection } ] ")"
  95. [ ":" QualIdent ] .
  96. FPSection = [ "VAR" ] IdentList ":" FormalType .
  97. FormalType = [ "ARRAY" "OF" ] QualIdent .
  98. ModuleDeclaration = "MODULE" ident [ Priority ] ";"
  99. [ Import ] [ Export ] Block ident .
  100. Priority = "[" ConstExpression "]" .
  101. Export = "EXPORT" [ "QUALIFIED" ] IdentList ";" .
  102. Import = [ "FROM" ident ] "IMPORT" IdentList ";" .
  103. DefinitionModule = "DEFINITION" "MODULE" ident ";"
  104. { Import } { Definition } "END" ident "." .
  105. Definition = "CONST" { ConstantDeclaration ";" } |
  106. | "TYPE" { ident [ "=" Type ] ";" }
  107. | "VAR" { VariableDeclaration ";" }
  108. | ProcedureHeading ";" .
  109. ProgramModule = "MODULE" ident [ Priority ] ";"
  110. { Import } Block ident "." .
  111. END Mod2.