turbo_pascal3.lst 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. Coco/R - Compiler-Compiler V1.53
  2. Released by Pat Terry 17 September 2002
  3. Source file: turbo_pascal3.atg
  4. Listing:
  5. 1 (* Tags: TURBO3, TURBOPASCAL, TP3, BORLAND *)
  6. 2 COMPILER Pascal
  7. 3 (* J & W Pascal - not Turbo Pascal *)
  8. 4 (* This grammar is not LL(1) *)
  9. 5
  10. 6 CHARACTERS
  11. 7 eol = CHR(13) .
  12. 8 letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
  13. 9 digit = "0123456789".
  14. 10 noQuote1 = ANY - "'" - eol .
  15. 11
  16. 12 IGNORE CHR(9) .. CHR(13)
  17. 13 IGNORE CASE
  18. ***** ^ IGNORE CASE must appear earlier
  19. 14
  20. 15 COMMENTS FROM "(*" TO "*)"
  21. 16 COMMENTS FROM "{" TO "}"
  22. 17
  23. 18 TOKENS
  24. 19 identifier = letter { letter | digit } .
  25. 20 integer = digit { digit } | digit { digit } CONTEXT ("..") .
  26. 21 real = digit { digit } "." digit { digit }
  27. 22 [ "E" ["+" | "-"] digit { digit } ]
  28. 23 | digit { digit } "E" ["+" | "-"] digit { digit } .
  29. 24 string = "'" { noQuote1 | "''" } "'" .
  30. 25
  31. 26 PRODUCTIONS
  32. ***** ^ could not make deterministic automaton
  33. 27
  34. 28 Pascal = "program" NewIdent [ ExternalFiles ] ";"
  35. 29 Block "." .
  36. 30
  37. 31 ExternalFiles = "(" NewIdentList ")" .
  38. 32
  39. 33 Block = DeclarationPart StatementPart .
  40. 34
  41. 35 DeclarationPart = LabelDeclarations ConstDefinitions
  42. 36 TypeDefinitions VarDeclarations
  43. 37 { ProcDeclarations } .
  44. 38
  45. 39 (* -------------------------------------------------------------------- *)
  46. 40
  47. 41 LabelDeclarations = [ "label" Labels ";" ] .
  48. 42
  49. 43 Labels = Label { "," Label } .
  50. 44
  51. 45 Label = UnsignedInt .
  52. 46
  53. 47 (* -------------------------------------------------------------------- *)
  54. 48
  55. 49 ConstDefinitions = [ "const" ConstDef { ConstDef } ] .
  56. 50
  57. 51 ConstDef = NewIdent "=" Constant ";" .
  58. 52
  59. 53 Constant = [ "+" | "-" ] ( UnsignedNumber | ConstIdent ) | String .
  60. 54
  61. 55 UnsignedNumber = UnsignedInt | UnsignedReal .
  62. 56
  63. 57 ConstIdent = identifier .
  64. 58
  65. 59 (* -------------------------------------------------------------------- *)
  66. 60
  67. 61 TypeDefinitions = [ "type" TypeDef { TypeDef } ] .
  68. 62
  69. 63 TypeDef = NewIdent "=" Type ";" .
  70. 64
  71. 65 Type = SimpleType | [ "packed" ] StructType | "^" TypeIdent .
  72. 66
  73. 67 SimpleType = TypeIdent | EnumerationType | SubrangeType .
  74. 68
  75. 69 TypeIdent = identifier .
  76. 70
  77. 71 EnumerationType = "(" NewIdentList ")" .
  78. 72
  79. 73 SubrangeType = Constant ".." Constant .
  80. 74
  81. 75 StructType = ArrayType | RecordType | SetType | FileType .
  82. 76
  83. 77 ArrayType = "array" "[" IndexList "]" "of" Type .
  84. 78
  85. 79 IndexList = SimpleType { "," SimpleType } .
  86. 80
  87. 81 RecordType = "record" FieldList "end" .
  88. 82
  89. 83 SetType = "set" "of" SimpleType .
  90. 84
  91. 85 FileType = "file" "of" Type .
  92. 86
  93. 87 FieldList = [ ( fixedPart [ ";" VariantPart ] | VariantPart ) [ ";" ] ] .
  94. 88
  95. 89 fixedPart = RecordSection { ";" RecordSection } .
  96. 90
  97. 91 RecordSection = NewIdentList ":" Type .
  98. 92
  99. 93 VariantPart = "case" VariantSelector "of" Variant { ";" Variant } .
  100. 94
  101. 95 VariantSelector = [ NewIdent ":" ] TypeIdent .
  102. 96
  103. 97 Variant = CaseLabelList ":" "(" FieldList ")" .
  104. 98
  105. 99 (* -------------------------------------------------------------------- *)
  106. 100
  107. 101 VarDeclarations = [ "var" VarDecl { VarDecl } ] .
  108. 102
  109. 103 VarDecl = NewIdentList ":" Type ";" .
  110. 104
  111. 105 (* -------------------------------------------------------------------- *)
  112. 106
  113. 107 ProcDeclarations = ( ProcHeading | FuncHeading ) ";" Body ";" .
  114. 108
  115. 109 ProcHeading = "procedure" NewIdent [ FormalParams ] .
  116. 110
  117. 111 FuncHeading = "function" NewIdent [ FormalParams ] ReturnType .
  118. 112
  119. 113 ReturnType = [ (* empty if forward referenced *) ":" TypeIdent ] .
  120. 114
  121. 115 Body = Block | "forward" .
  122. 116
  123. 117 FormalParams = "(" FormalSection { ";" FormalSection } ")" .
  124. 118
  125. 119 FormalSection = [ "var" ] ParamGroup | ProcHeading | FuncHeading .
  126. 120
  127. 121 ParamGroup = NewIdentList ":" ParamType .
  128. 122
  129. 123 ParamType = TypeIdent
  130. 124 | "array" "[" IndexSpecList "]" "of" ParamType
  131. 125 | "packed" "array" "[" IndexSpec "]" "of" TypeIdent .
  132. 126
  133. 127 IndexSpecList = IndexSpec { ";" IndexSpec } .
  134. 128
  135. 129 IndexSpec = NewIdent ".." NewIdent ":" TypeIdent .
  136. 130
  137. 131 (* -------------------------------------------------------------------- *)
  138. 132
  139. 133 StatementPart = CompoundStatement .
  140. 134
  141. 135 CompoundStatement = "begin" StatementSequence "end" .
  142. 136
  143. 137 StatementSequence = Statement { ";" Statement } .
  144. 138
  145. 139 Statement = [ Label ":" ]
  146. 140 [ AssignmentOrCall
  147. 141 | CompoundStatement
  148. 142 | GotoStatement
  149. 143 | WhileStatement
  150. 144 | RepeatStatement
  151. 145 | IfStatement
  152. 146 | CaseStatement
  153. 147 | ForStatement
  154. 148 | WithStatement ] .
  155. 149
  156. 150 AssignmentOrCall = Designator ( ":=" Expression | [ ActualParams ] ) .
  157. 151
  158. 152 ActualParams = "(" ActualParameter { "," ActualParameter } ")" .
  159. 153
  160. 154 ActualParameter = Expression [ FieldWidth (* only in i/o *) ] .
  161. 155
  162. 156 FieldWidth = ":" IntegerExpression [ ":" IntegerExpression ] .
  163. 157
  164. 158 GotoStatement = "goto" Label .
  165. 159
  166. 160 WhileStatement = "while" BooleanExpression "do" Statement .
  167. 161
  168. 162 RepeatStatement = "repeat" StatementSequence "until" BooleanExpression .
  169. 163
  170. 164 IfStatement = "if" BooleanExpression "then" Statement
  171. 165 [ "else" Statement ] .
  172. 166
  173. 167 CaseStatement = "case" OrdinalExpression "of" CaseList "end" .
  174. 168
  175. 169 CaseList = OneCase { ";" OneCase } [ ";" ] .
  176. 170
  177. 171 OneCase = CaseLabelList ":" Statement .
  178. 172
  179. 173 CaseLabelList = CaseLabel { "," CaseLabel } .
  180. 174
  181. 175 CaseLabel = Constant .
  182. 176
  183. 177 ForStatement = "for" ControlVariable ":=" OrdinalExpression
  184. 178 ( "to" | "downto" ) OrdinalExpression "do" Statement .
  185. 179
  186. 180 ControlVariable = identifier .
  187. 181
  188. 182 WithStatement = "with" RecVarList "do" Statement .
  189. 183
  190. 184 RecVarList = Designator { "," Designator } .
  191. 185
  192. 186 (* -------------------------------------------------------------------- *)
  193. 187
  194. 188 IntegerExpression = Expression .
  195. 189
  196. 190 BooleanExpression = Expression .
  197. 191
  198. 192 OrdinalExpression = Expression .
  199. 193
  200. 194 Expression = SimpleExpression [ RelOp SimpleExpression ] .
  201. 195
  202. 196 RelOp = "=" | "<" | ">" | "<=" | ">=" | "<>" | "in" .
  203. 197
  204. 198 SimpleExpression = ( "+" Term | "-" Term | Term ) { AddOp Term } .
  205. 199
  206. 200 AddOp = "+" | "-" | "or" .
  207. 201
  208. 202 Term = Factor { MulOp Factor } .
  209. 203
  210. 204 MulOp = "*" | "/" | "div" | "mod" | "and" .
  211. 205
  212. 206 Factor = Designator [ ActualParams ]
  213. 207 | UnsignedLiteral
  214. 208 | SetConstructor
  215. 209 | "(" Expression ")"
  216. 210 | "not" Factor .
  217. 211
  218. 212 Designator = identifier { "." identifier | "[" ExpList "]" | "^" } .
  219. 213
  220. 214 ExpList = Expression { "," Expression } .
  221. 215
  222. 216 UnsignedLiteral = UnsignedNumber | "nil" | String .
  223. 217
  224. 218 SetConstructor = "[" Member { "," Member } "]" .
  225. 219
  226. 220 Member = Expression [ ".." Expression ] .
  227. 221
  228. 222 (* -------------------------------------------------------------------- *)
  229. 223
  230. 224 NewIdentList = NewIdent { "," NewIdent } .
  231. 225
  232. 226 NewIdent = identifier .
  233. 227
  234. 228 UnsignedInt = integer .
  235. 229
  236. 230 UnsignedReal = real .
  237. 231
  238. 232 String = string .
  239. 233
  240. 234 END Pascal.
  241. 2 errors