pascal.lst 8.6 KB

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