Modula2.atg 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. (*
  2. GNU Modula-2 grammar converted from GCC's gm2-ebnf.texi
  3. for testing with the GNU Modula-2 version of Coco/R.
  4. IMPORTANT:
  5. The GCC EBNF deliberately leaves the lexical productions Ident,
  6. Integer, Real and string as built-ins. Therefore the lexical rules
  7. below are a TEST lexer, not a claim that they exactly reproduce every
  8. GNU Modula-2 lexical extension. The syntactic productions are a
  9. mechanical conversion of the supplied GCC EBNF.
  10. Source: GNU Modula-2 gm2-ebnf.texi (GCC, 2000-2026).
  11. *)
  12. COMPILER GM2
  13. CHARACTERS
  14. letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".
  15. octDigit = "01234567".
  16. digit = octDigit + '89'.
  17. hexDigit = digit + 'ABCDEF'.
  18. eol = CHR(13).
  19. noQuote1 = ANY - "'" - eol.
  20. noQuote2 = ANY - '"' - eol.
  21. IGNORE CHR(9) .. CHR(13)
  22. COMMENTS FROM "(*" TO "*)" NESTED
  23. TOKENS
  24. Ident = letter { letter | digit | "_" }.
  25. Integer = digit { digit }
  26. | digit { digit } CONTEXT ("..")
  27. | octDigit { octDigit } ("B" | "C")
  28. | digit { hexDigit } "H".
  29. (* A real literal requires digits after the decimal point. This is
  30. important for ranges such as 0..39: the scanner must produce
  31. INTEGER DOTDOT INTEGER, not REAL followed by a dot. *)
  32. Real = digit { digit } "." digit { digit }
  33. [ "E" [ "+" | "-" ] digit { digit } ].
  34. string = "'" { noQuote1 } "'"
  35. | '"' { noQuote2 } '"'.
  36. PRODUCTIONS
  37. GM2 = ( DefinitionModule | ImplementationOrProgramModule ).
  38. ProgramModule = "MODULE" Ident [ Priority ] ";" { Import } Block Ident ".".
  39. ImplementationModule = "IMPLEMENTATION" "MODULE" Ident [ Priority ] ";" { Import } Block Ident ".".
  40. ImplementationOrProgramModule = ImplementationModule | ProgramModule.
  41. Number = Integer | Real.
  42. Qualident = Ident { "." Ident }.
  43. ConstantDeclaration = Ident "=" ConstExpression.
  44. ConstExpression = SimpleConstExpr [ Relation SimpleConstExpr ].
  45. Relation = "=" | "#" | "<>" | "<" | "<=" | ">" | ">=" | "IN".
  46. SimpleConstExpr = UnaryOrConstTerm { AddOperator ConstTerm }.
  47. UnaryOrConstTerm = "+" ConstTerm | "-" ConstTerm | ConstTerm.
  48. AddOperator = "+" | "-" | "OR".
  49. ConstTerm = ConstFactor { MulOperator ConstFactor }.
  50. MulOperator = "*" | "/" | "DIV" | "MOD" | "REM" | "AND" | "&".
  51. ConstFactor = Number | ConstString | ConstSetOrQualidentOrFunction | "(" ConstExpression ")" | "NOT" ConstFactor | "~" ConstFactor | ConstAttribute.
  52. ConstString = string.
  53. ComponentElement = ConstExpression [ ".." ConstExpression ].
  54. ComponentValue = ComponentElement [ "BY" ConstExpression ].
  55. ArraySetRecordValue = ComponentValue { "," ComponentValue }.
  56. Constructor = "{" [ ArraySetRecordValue ] "}".
  57. ConstSetOrQualidentOrFunction = Constructor | Qualident [ Constructor | ConstActualParameters ].
  58. ConstActualParameters = "(" [ ExpList ] ")".
  59. ConstAttribute = "__ATTRIBUTE__" "__BUILTIN__" "(" "(" ConstAttributeExpression ")" ")".
  60. ConstAttributeExpression = Ident | "<" Qualident "," Ident ">".
  61. ByteAlignment = "<*" AttributeExpression "*>".
  62. Alignment = [ ByteAlignment ].
  63. TypeDeclaration = Ident "=" Type Alignment.
  64. Type = SimpleType | ArrayType | RecordType | SetType | PointerType | ProcedureType.
  65. SimpleType = Qualident [ SubrangeType ] | Enumeration | SubrangeType.
  66. Enumeration = "(" IdentList ")".
  67. IdentList = Ident { "," Ident }.
  68. SubrangeType = "[" ConstExpression ".." ConstExpression "]".
  69. ArrayType = "ARRAY" SimpleType { "," SimpleType } "OF" Type.
  70. RecordType = "RECORD" [ DefaultRecordAttributes ] FieldListSequence "END".
  71. DefaultRecordAttributes = "<*" AttributeExpression "*>".
  72. RecordFieldPragma = [ "<*" FieldPragmaExpression { "," FieldPragmaExpression } "*>" ].
  73. FieldPragmaExpression = Ident [ "(" ConstExpression ")" ].
  74. AttributeExpression = Ident "(" ConstExpression ")".
  75. FieldListSequence = FieldListStatement { ";" FieldListStatement }.
  76. FieldListStatement = [ FieldList ].
  77. FieldList = IdentList ":" Type RecordFieldPragma | "CASE" CaseTag "OF" Varient { "|" Varient } [ "ELSE" FieldListSequence ] "END".
  78. TagIdent = [ Ident ].
  79. CaseTag = TagIdent [ ":" Qualident ].
  80. Varient = [ VarientCaseLabelList ":" FieldListSequence ].
  81. VarientCaseLabelList = VarientCaseLabels { "," VarientCaseLabels }.
  82. VarientCaseLabels = ConstExpression [ ".." ConstExpression ].
  83. CaseLabelList = CaseLabels { "," CaseLabels }.
  84. CaseLabels = ConstExpression [ ".." ConstExpression ].
  85. SetType = ( "SET" | "PACKEDSET" ) "OF" SimpleType.
  86. PointerType = "POINTER" "TO" Type.
  87. ProcedureType = "PROCEDURE" [ FormalTypeList ].
  88. FormalTypeList = "(" ( ")" FormalReturn | ProcedureParameters ")" FormalReturn ).
  89. FormalReturn = [ ":" OptReturnType ].
  90. OptReturnType = "[" Qualident "]" | Qualident.
  91. ProcedureParameters = ProcedureParameter { "," ProcedureParameter }.
  92. ProcedureParameter = "..." | "VAR" FormalType | FormalType.
  93. VarIdent = Ident [ "[" ConstExpression "]" ].
  94. VariableDeclaration = VarIdentList ":" Type Alignment.
  95. VarIdentList = VarIdent { "," VarIdent }.
  96. Designator = Qualident { SubDesignator }.
  97. SubDesignator = "." Ident | "[" ExpList "]" | "^".
  98. ExpList = Expression { "," Expression }.
  99. Expression = SimpleExpression [ Relation SimpleExpression ].
  100. SimpleExpression = [ "+" | "-" ] Term { AddOperator Term }.
  101. Term = Factor { MulOperator Factor }.
  102. Factor = Number | string | SetOrDesignatorOrFunction | "(" Expression ")" | "NOT" Factor | "~" Factor | ConstAttribute.
  103. SetOrDesignatorOrFunction = ( Qualident [ Constructor | SimpleDes [ ActualParameters ] ] | Constructor ).
  104. SimpleDes = { "." Ident | "[" ExpList "]" | "^" }.
  105. ActualParameters = "(" [ ExpList ] ")".
  106. Statement = [ AssignmentOrProcedureCall | IfStatement | CaseStatement | WhileStatement | RepeatStatement | LoopStatement | ForStatement | WithStatement | AsmStatement | "EXIT" | "RETURN" [ Expression ] | RetryStatement ].
  107. RetryStatement = "RETRY".
  108. AssignmentOrProcedureCall = Designator ( ":=" Expression | ActualParameters | ).
  109. StatementSequence = Statement { ";" Statement }.
  110. IfStatement = "IF" Expression "THEN" StatementSequence { "ELSIF" Expression "THEN" StatementSequence } [ "ELSE" StatementSequence ] "END".
  111. CaseStatement = "CASE" Expression "OF" Case { "|" Case } [ "ELSE" StatementSequence ] "END".
  112. Case = [ CaseLabelList ":" StatementSequence ].
  113. WhileStatement = "WHILE" Expression "DO" StatementSequence "END".
  114. RepeatStatement = "REPEAT" StatementSequence "UNTIL" Expression.
  115. ForStatement = "FOR" Ident ":=" Expression "TO" Expression [ "BY" ConstExpression ] "DO" StatementSequence "END".
  116. LoopStatement = "LOOP" StatementSequence "END".
  117. WithStatement = "WITH" Designator "DO" StatementSequence "END".
  118. ProcedureDeclaration = ProcedureHeading ";" ( ProcedureBlock Ident ).
  119. DefineBuiltinProcedure = [ "__ATTRIBUTE__" "__BUILTIN__" "(" "(" Ident ")" ")" | "__INLINE__" ].
  120. ProcedureHeading = "PROCEDURE" DefineBuiltinProcedure ( Ident [ FormalParameters ] AttributeNoReturn ).
  121. AttributeNoReturn = [ "<*" Ident "*>" ].
  122. AttributeUnused = [ "<*" Ident "*>" ].
  123. Builtin = [ "__BUILTIN__" | "__INLINE__" ].
  124. DefProcedureHeading = "PROCEDURE" Builtin ( Ident [ DefFormalParameters ] AttributeNoReturn ).
  125. ProcedureBlock = { Declaration } [ "BEGIN" BlockBody ] "END".
  126. Block = { Declaration } InitialBlock FinalBlock "END".
  127. InitialBlock = [ "BEGIN" BlockBody ].
  128. FinalBlock = [ "FINALLY" BlockBody ].
  129. BlockBody = NormalPart [ "EXCEPT" ExceptionalPart ].
  130. NormalPart = StatementSequence.
  131. ExceptionalPart = StatementSequence.
  132. Declaration = "CONST" { ConstantDeclaration ";" } | "TYPE" { TypeDeclaration ";" } | "VAR" { VariableDeclaration ";" } | ProcedureDeclaration ";" | ModuleDeclaration ";".
  133. DefFormalParameters = "(" [ DefMultiFPSection ] ")" FormalReturn.
  134. DefMultiFPSection = DefExtendedFP | FPSection [ ";" DefMultiFPSection ].
  135. FormalParameters = "(" [ MultiFPSection ] ")" FormalReturn.
  136. MultiFPSection = ExtendedFP | FPSection [ ";" MultiFPSection ].
  137. FPSection = NonVarFPSection | VarFPSection.
  138. DefExtendedFP = DefOptArg | "...".
  139. ExtendedFP = OptArg | "...".
  140. VarFPSection = "VAR" IdentList ":" FormalType [ AttributeUnused ].
  141. NonVarFPSection = IdentList ":" FormalType [ AttributeUnused ].
  142. OptArg = "[" Ident ":" FormalType [ "=" ConstExpression ] "]".
  143. DefOptArg = "[" Ident ":" FormalType "=" ConstExpression "]".
  144. FormalType = { "ARRAY" "OF" } Qualident.
  145. ModuleDeclaration = "MODULE" Ident [ Priority ] ";" { Import } [ Export ] Block Ident.
  146. Priority = "[" ConstExpression "]".
  147. Export = "EXPORT" ( "QUALIFIED" IdentList | "UNQUALIFIED" IdentList | IdentList ) ";".
  148. Import = "FROM" Ident "IMPORT" IdentList ";" | "IMPORT" IdentList ";".
  149. DefinitionModule = "DEFINITION" "MODULE" [ "FOR" string ] Ident ";" { Import } [ Export ] { Definition } "END" Ident ".".
  150. Definition = "CONST" { ConstantDeclaration ";" } | "TYPE" { Ident ( ";" | "=" Type Alignment ";" ) } | "VAR" { VariableDeclaration ";" } | DefProcedureHeading ";".
  151. AsmStatement = "ASM" [ "VOLATILE" ] "(" AsmOperands ")".
  152. NamedOperand = "[" Ident "]".
  153. AsmOperandName = [ NamedOperand ].
  154. AsmOperands = string [ ":" AsmList [ ":" AsmList [ ":" TrashList ] ] ].
  155. AsmList = [ AsmElement ] { "," AsmElement }.
  156. AsmElement = AsmOperandName string "(" Expression ")".
  157. TrashList = [ string ] { "," string }.
  158. END GM2.