Pascal2.atg 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. COMPILER Pascal
  2. IGNORECASE
  3. CHARACTERS
  4. letter = 'A' .. 'Z' + 'a' .. 'z' .
  5. digit = '0' .. '9' .
  6. cr = '\r' .
  7. lf = '\n' .
  8. tab = '\t' .
  9. printable = '\x20' .. '\x7e' .
  10. all = '\x00' .. '\xff' .
  11. stringCh = printable - "'" .
  12. sign = '+-' .
  13. TOKENS
  14. identifier = letter { letter | digit } .
  15. characterString = "'" { stringCh | "''" } "'" .
  16. unsignedInteger = digit { digit } CONTEXT ( '..' ) .
  17. unsignedReal = digit { digit }
  18. ( '.' digit { digit } [ 'e' [ sign ] digit { digit } ]
  19. | 'e' [ sign ] digit { digit } ) .
  20. COMMENTS FROM '{' TO '}'
  21. IGNORE cr + lf + tab
  22. PRODUCTIONS
  23. Pascal =
  24. ProgramHeading ';' Block '.' .
  25. ProgramHeading =
  26. 'program' identifier [ ProgramParameterList ] .
  27. ProgramParameterList =
  28. '(' IdentifierList ')' .
  29. /*-----------------------------------------------------------------------*/
  30. Block =
  31. [ LabelDeclarationPart ]
  32. [ ConstantDefinitionPart ]
  33. [ TypeDefinitionPart ]
  34. [ VariableDeclarationPart ]
  35. { ProcedureAndFunctionDeclarationPart }
  36. StatementPart
  37. .
  38. LabelDeclarationPart =
  39. 'label' unsignedInteger { ',' unsignedInteger } ';' .
  40. ConstantDefinitionPart =
  41. 'const' ConstantDefinition ';' { ConstantDefinition ';' } .
  42. TypeDefinitionPart =
  43. 'type' TypeDefinition ';' { TypeDefinition ';' } .
  44. VariableDeclarationPart =
  45. 'var' VariableDeclaration ';' { VariableDeclaration ';' } .
  46. ProcedureAndFunctionDeclarationPart =
  47. ( ProcedureDeclaration | FunctionDeclaration ) ';' .
  48. StatementPart =
  49. CompoundStatement .
  50. ConstantDefinition =
  51. identifier '=' Constant .
  52. TypeDefinition =
  53. identifier '=' Type .
  54. VariableDeclaration =
  55. IdentifierList ':' Type .
  56. ProcedureDeclaration =
  57. ProcedureHeading ';' Block
  58. | ProcedureHeading ';' Directive
  59. | ProcedureIdentification ';' Block
  60. .
  61. FunctionDeclaration =
  62. FunctionHeading ';' Block
  63. | FunctionHeading ';' Directive
  64. | FunctionIdentification ';' Block
  65. .
  66. ProcedureHeading =
  67. 'procedure' identifier [ FormalParameterList ] .
  68. ProcedureIdentification =
  69. 'procedure' ProcedureIdentifier .
  70. FunctionHeading =
  71. 'function' identifier [ FormalParameterList ] ':' ResultType .
  72. FunctionIdentification =
  73. 'function' FunctionIdentifier.
  74. FormalParameterList =
  75. '(' FormalParameterSection { ';' FormalParameterSection } ')' .
  76. FormalParameterSection =
  77. ValueParameterSpecification
  78. | VariableParameterSpecification
  79. | ProceduralParameterSpecification
  80. | FunctionalParameterSpecification
  81. .
  82. /*-----------------------------------------------------------------*/
  83. ValueParameterSpecification =
  84. IdentifierList ':' ( TypeIdentifier | ConformantArraySchema ) .
  85. VariableParameterSpecification =
  86. 'var' IdentifierList ':' ( TypeIdentifier | ConformantArraySchema ) .
  87. ProceduralParameterSpecification =
  88. ProcedureHeading.
  89. FunctionalParameterSpecification =
  90. FunctionHeading .
  91. ConformantArraySchema =
  92. PackedConformantArraySchema
  93. | UnpackedConformantArraySchema .
  94. PackedConformantArraySchema =
  95. 'packed' 'array' '[' IndexTypeSpecification ']' 'of' TypeIdentifier .
  96. UnpackedConformantArraySchema =
  97. 'array' '[' IndexTypeSpecification ']' 'of'
  98. ( TypeIdentifier | ConformantArraySchema ) .
  99. IndexTypeSpecification =
  100. identifier '..' identifier ':' OrdinalTypeIdentifier .
  101. /*-----------------------------------------------------------------*/
  102. CompoundStatement =
  103. 'begin'
  104. StatementSequence
  105. 'end'
  106. .
  107. StatementSequence =
  108. Statement
  109. { ';' Statement }
  110. .
  111. Statement =
  112. [ Label ':' ] ( SimpleStatement | StructuredStatement ) .
  113. SimpleStatement =
  114. EmptyStatement
  115. | AssignmentStatement
  116. | ProcedureStatement
  117. | GotoStatement
  118. .
  119. StructuredStatement =
  120. CompoundStatement
  121. | ConditionalStatement
  122. | RepetitiveStatement
  123. | WithStatement
  124. .
  125. ConditionalStatement =
  126. IfStatement | CaseStatement .
  127. RepetitiveStatement =
  128. WhileStatement | RepeatStatement | ForStatement .
  129. EmptyStatement = .
  130. AssignmentStatement =
  131. ( Variable | FunctionIdentifier ) ':=' Expression .
  132. ProcedureStatement =
  133. ProcedureIdentifier [ ActualParameterList | WriteParameterList ] .
  134. GotoStatement =
  135. 'goto' Label .
  136. IfStatement =
  137. 'if' BooleanExpression 'then' Statement
  138. [ 'else' Statement ]
  139. .
  140. CaseStatement =
  141. 'case' CaseIndex 'of'
  142. Case { ';' Case } [ ';' ]
  143. 'end'
  144. .
  145. RepeatStatement =
  146. 'repeat'
  147. StatementSequence
  148. 'until' BooleanExpression
  149. .
  150. WhileStatement =
  151. 'while' BooleanExpression 'do'
  152. Statement
  153. .
  154. ForStatement =
  155. 'for' ControlVariable ':=' InitialValue ( 'to' | 'downto' ) FinalValue 'do'
  156. Statement
  157. .
  158. WithStatement =
  159. 'with' RecordVariableList 'do'
  160. Statement
  161. .
  162. RecordVariableList =
  163. RecordVariable { '.' RecordVariable } .
  164. CaseIndex =
  165. OrdinalExpression .
  166. Case =
  167. Constant { ',' Constant } ':'
  168. Statement
  169. .
  170. ControlVariable =
  171. VariableIdentifier .
  172. InitialValue =
  173. OrdinalExpression .
  174. FinalValue =
  175. OrdinalExpression .
  176. /*-----------------------------------------------------------------------*/
  177. Type =
  178. SimpleType | StructuredType | PointerType .
  179. SimpleType =
  180. OrdinalType | RealTypeIdentifier .
  181. StructuredType =
  182. { 'packed' } UnpackedStructuredType | StructuredTypeIdentifier .
  183. PointerType =
  184. '^' DomainType | PointerTypeIdentifier .
  185. OrdinalType =
  186. EnumeratedType | SubrangeType | OrdinalTypeIdentifier .
  187. UnpackedStructuredType =
  188. ArrayType | RecordType | SetType | FileType .
  189. DomainType =
  190. TypeIdentifier .
  191. EnumeratedType =
  192. '(' IdentifierList ')' .
  193. SubrangeType =
  194. Constant '..' Constant .
  195. ArrayType =
  196. 'array' '[' IndexType { ',' IndexType } ']' 'of' ComponentType .
  197. RecordType =
  198. 'record'
  199. FieldList
  200. 'end'
  201. .
  202. SetType =
  203. 'set' 'of' BaseType .
  204. FileType =
  205. 'file' 'of' ComponentType .
  206. IndexType =
  207. OrdinalType .
  208. ComponentType =
  209. Type.
  210. BaseType =
  211. OrdinalType .
  212. ResultType =
  213. OrdinalTypeIdentifier | RealTypeIdentifier | PointerTypeIdentifier .
  214. FieldList =
  215. [ ( FixedPart [ ':' VariantPart ] | VariantPart ) [';'] ] .
  216. FixedPart =
  217. RecordSection { ';' RecordSection } .
  218. VariantPart =
  219. 'case' VariantSelector 'of'
  220. Variant
  221. { ';' Variant }
  222. .
  223. RecordSection =
  224. IdentifierList ':' Type .
  225. VariantSelector =
  226. [ TagField ':' ] TagType .
  227. Variant =
  228. Constant { ',' Constant } ';' '(' FieldList ')' .
  229. TagType =
  230. OrdinalTypeIdentifier .
  231. TagField =
  232. identifier .
  233. /*--------------------------------------------------------------------*/
  234. Constant =
  235. [ Sign ] ( UnsignedNumber | ConstantIdentifier )
  236. | characterString .
  237. /*-------------------------------------------------------------------*/
  238. Expression =
  239. SimpleExpression [ RelationOperator SimpleExpression ] .
  240. SimpleExpression =
  241. [ Sign ] Term { AddingOperator Term } .
  242. Term =
  243. Factor { MultiplyingOperator Factor } .
  244. Factor =
  245. UnsignedConstant
  246. | BoundIdentifier
  247. | Variable
  248. | SetConstructor
  249. | FunctionDesignator
  250. | 'not' Factor
  251. | '(' Expression ')'
  252. .
  253. RelationOperator =
  254. '=' | '<>' | '<' | '<=' | '>' | '>=' | 'in' .
  255. AddingOperator =
  256. '+' | '-' | 'or' .
  257. MultiplyingOperator =
  258. '*' | '/' | 'div' | 'mod' | 'and' .
  259. UnsignedConstant =
  260. UnsignedNumber | characterString | ConstantIdentifier | 'nil' .
  261. FunctionDesignator =
  262. FunctionIdentifier [ ActualParameterList ] .
  263. Variable =
  264. EntireVariable | ComponentVariable | IdentifierVariable |
  265. BufferVariable .
  266. EntireVariable =
  267. VariableIdentifier .
  268. ComponentVariable =
  269. IndexedVariable | FieldDesignator .
  270. IdentifierVariable =
  271. PointerVariable '^' .
  272. BufferVariable =
  273. FileVariable '^' .
  274. IndexedVariable =
  275. ArrayVariable '[' Index { ',' Index } ']' .
  276. FieldDesignator =
  277. [ RecordVariable '.' ] FieldIdentifier .
  278. SetConstructor =
  279. '[' [ ElementDescription { ',' ElementDescription } ] ']' .
  280. ElementDescription =
  281. OrdinalExpression [ '..' OrdinalExpression ].
  282. ActualParameterList =
  283. '(' ActualParameter { ',' ActualParameter } ')' .
  284. ActualParameter =
  285. Expression | Variable | ProcedureIdentifier | FunctionIdentifier .
  286. WriteParameterList =
  287. '(' ( FileVariable | WriteParameter ) { ',' WriteParameter } ')' .
  288. WriteParameter =
  289. Expression [ ':' IntegerExpression [ ':' IntegerExpression ] ] .
  290. ArrayVariable =
  291. Variable .
  292. RecordVariable =
  293. Variable .
  294. FileVariable =
  295. Variable .
  296. PointerVariable =
  297. Variable .
  298. IntegerExpression =
  299. OrdinalExpression .
  300. BooleanExpression =
  301. OrdinalExpression .
  302. Index =
  303. OrdinalExpression .
  304. OrdinalExpression =
  305. Expression .
  306. PointerTypeIdentifier =
  307. TypeIdentifier .
  308. StructuredTypeIdentifier =
  309. TypeIdentifier .
  310. OrdinalTypeIdentifier =
  311. TypeIdentifier .
  312. RealTypeIdentifier =
  313. TypeIdentifier .
  314. ConstantIdentifier =
  315. identifier .
  316. TypeIdentifier =
  317. identifier .
  318. VariableIdentifier =
  319. identifier .
  320. FieldIdentifier =
  321. identifier .
  322. ProcedureIdentifier =
  323. identifier .
  324. FunctionIdentifier =
  325. identifier .
  326. BoundIdentifier =
  327. identifier .
  328. UnsignedNumber =
  329. unsignedInteger | unsignedReal .
  330. Sign =
  331. '+' | '-' .
  332. IdentifierList =
  333. identifier { ',' identifier } .
  334. Directive =
  335. identifier .
  336. Label =
  337. unsignedInteger .
  338. END Pascal.