turbo_pascal3.atg 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. (* Tags: TURBO3, TURBOPASCAL, TP3, BORLAND *)
  2. COMPILER Pascal
  3. IGNORE CASE
  4. (* J & W Pascal - not Turbo Pascal *)
  5. (* This grammar is not LL(1) *)
  6. CHARACTERS
  7. eol = CHR(13) .
  8. letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
  9. digit = "0123456789".
  10. hexDigit = digit + "ABCDEF" .
  11. noQuote1 = ANY - "'" - eol .
  12. IGNORE CHR(9) .. CHR(13)
  13. COMMENTS FROM "(*" TO "*)"
  14. COMMENTS FROM "{" TO "}"
  15. TOKENS
  16. identifier = letter { letter | digit } .
  17. integer = digit { digit } | digit { digit } CONTEXT ("..")
  18. | "$" hexDigit { hexDigit } .
  19. real = digit { digit } "." digit { digit }
  20. [ "E" ["+" | "-"] digit { digit } ]
  21. | digit { digit } "E" ["+" | "-"] digit { digit } .
  22. string = "'" { noQuote1 | "''" } "'" .
  23. PRODUCTIONS
  24. Pascal = "program" NewIdent [ ExternalFiles ] ";"
  25. Block "." .
  26. ExternalFiles = "(" NewIdentList ")" .
  27. Block = DeclarationPart StatementPart .
  28. DeclarationPart = LabelDeclarations ConstDefinitions
  29. TypeDefinitions VarDeclarations
  30. { ProcDeclarations } .
  31. (* -------------------------------------------------------------------- *)
  32. LabelDeclarations = [ "label" Labels ";" ] .
  33. Labels = Label { "," Label } .
  34. Label = UnsignedInt .
  35. (* -------------------------------------------------------------------- *)
  36. ConstDefinitions = [ "const" ConstDef { ConstDef } ] .
  37. ConstDef = NewIdent "=" Constant ";" .
  38. Constant = [ "+" | "-" ] ( UnsignedNumber | ConstIdent ) | String .
  39. UnsignedNumber = UnsignedInt | UnsignedReal .
  40. ConstIdent = identifier .
  41. (* -------------------------------------------------------------------- *)
  42. TypeDefinitions = [ "type" TypeDef { TypeDef } ] .
  43. TypeDef = NewIdent "=" Type ";" .
  44. Type = SimpleType | [ "packed" ] StructType | "^" TypeIdent .
  45. SimpleType = TypeIdent | EnumerationType | SubrangeType .
  46. TypeIdent = identifier .
  47. EnumerationType = "(" NewIdentList ")" .
  48. SubrangeType = Constant ".." Constant .
  49. StructType = ArrayType | RecordType | SetType | FileType .
  50. ArrayType = "array" "[" IndexList "]" "of" Type .
  51. IndexList = SimpleType { "," SimpleType } .
  52. RecordType = "record" FieldList "end" .
  53. SetType = "set" "of" SimpleType .
  54. FileType = "file" "of" Type .
  55. FieldList = [ ( fixedPart [ ";" VariantPart ] | VariantPart ) [ ";" ] ] .
  56. fixedPart = RecordSection { ";" RecordSection } .
  57. RecordSection = NewIdentList ":" Type .
  58. VariantPart = "case" VariantSelector "of" Variant { ";" Variant } .
  59. VariantSelector = [ NewIdent ":" ] TypeIdent .
  60. Variant = CaseLabelList ":" "(" FieldList ")" .
  61. (* -------------------------------------------------------------------- *)
  62. VarDeclarations = [ "var" VarDecl { VarDecl } ] .
  63. VarDecl = NewIdentList ":" Type ";" .
  64. (* -------------------------------------------------------------------- *)
  65. ProcDeclarations = ( ProcHeading | FuncHeading ) ";" Body ";" .
  66. ProcHeading = "procedure" NewIdent [ FormalParams ] .
  67. FuncHeading = "function" NewIdent [ FormalParams ] ReturnType .
  68. ReturnType = [ (* empty if forward referenced *) ":" TypeIdent ] .
  69. Body = Block | "forward" .
  70. FormalParams = "(" FormalSection { ";" FormalSection } ")" .
  71. FormalSection = [ "var" ] ParamGroup | ProcHeading | FuncHeading .
  72. ParamGroup = NewIdentList ":" ParamType .
  73. ParamType = TypeIdent
  74. | "array" "[" IndexSpecList "]" "of" ParamType
  75. | "packed" "array" "[" IndexSpec "]" "of" TypeIdent .
  76. IndexSpecList = IndexSpec { ";" IndexSpec } .
  77. IndexSpec = NewIdent ".." NewIdent ":" TypeIdent .
  78. (* -------------------------------------------------------------------- *)
  79. StatementPart = CompoundStatement .
  80. CompoundStatement = "begin" StatementSequence "end" .
  81. StatementSequence = Statement { ";" Statement } .
  82. Statement = [ Label ":" ]
  83. [ AssignmentOrCall
  84. | CompoundStatement
  85. | GotoStatement
  86. | WhileStatement
  87. | RepeatStatement
  88. | IfStatement
  89. | CaseStatement
  90. | ForStatement
  91. | WithStatement ] .
  92. AssignmentOrCall = Designator ( ":=" Expression | [ ActualParams ] ) .
  93. ActualParams = "(" ActualParameter { "," ActualParameter } ")" .
  94. ActualParameter = Expression [ FieldWidth (* only in i/o *) ] .
  95. FieldWidth = ":" IntegerExpression [ ":" IntegerExpression ] .
  96. GotoStatement = "goto" Label .
  97. WhileStatement = "while" BooleanExpression "do" Statement .
  98. RepeatStatement = "repeat" StatementSequence "until" BooleanExpression .
  99. IfStatement = "if" BooleanExpression "then" Statement
  100. [ "else" Statement ] .
  101. CaseStatement = "case" OrdinalExpression "of" CaseList "end" .
  102. CaseList = OneCase { ";" OneCase } [ ";" ] [ "ELSE" Statement ] .
  103. OneCase = CaseLabelList ":" Statement .
  104. CaseLabelList = CaseLabel { "," CaseLabel } .
  105. CaseLabel = Constant .
  106. ForStatement = "for" ControlVariable ":=" OrdinalExpression
  107. ( "to" | "downto" ) OrdinalExpression "do" Statement .
  108. ControlVariable = identifier .
  109. WithStatement = "with" RecVarList "do" Statement .
  110. RecVarList = Designator { "," Designator } .
  111. (* -------------------------------------------------------------------- *)
  112. IntegerExpression = Expression .
  113. BooleanExpression = Expression .
  114. OrdinalExpression = Expression .
  115. Expression = SimpleExpression [ RelOp SimpleExpression ] .
  116. RelOp = "=" | "<" | ">" | "<=" | ">=" | "<>" | "in" .
  117. SimpleExpression = ( "+" Term | "-" Term | Term ) { AddOp Term } .
  118. AddOp = "+" | "-" | "or" .
  119. Term = Factor { MulOp Factor } .
  120. MulOp = "*" | "/" | "div" | "mod" | "and" .
  121. Factor = Designator [ ActualParams ]
  122. | UnsignedLiteral
  123. | SetConstructor
  124. | "(" Expression ")"
  125. | "not" Factor .
  126. Designator = identifier { "." identifier | "[" ExpList "]" | "^" } .
  127. ExpList = Expression { "," Expression } .
  128. UnsignedLiteral = UnsignedNumber | "nil" | String .
  129. SetConstructor = "[" Member { "," Member } "]" .
  130. Member = Expression [ ".." Expression ] .
  131. (* -------------------------------------------------------------------- *)
  132. NewIdentList = NewIdent { "," NewIdent } .
  133. NewIdent = identifier .
  134. UnsignedInt = integer .
  135. UnsignedReal = real .
  136. String = string .
  137. END Pascal.