m2pim3.g 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /* ANTLR v3 Grammar for Modula-2 (PIM3)
  2. *
  3. * Documentation version, derived from 3rd Edition of Programming in Modula-2
  4. *
  5. * This grammar follows the naming conventions and structure of the syntax
  6. * of Modula-2 as provided in the third edition of "Programming in Modula-2",
  7. * by N. Wirth. It is important to note that the syntax in Wirth's book does
  8. * not satisfy LL(1) constraints, and consequently ANTLR will not validate
  9. * the grammar in this form. An equivalent LL(1) version of this grammar is
  10. * also available in ANTLR v3 format as a separate file.
  11. *
  12. *
  13. * Copyright (C) 2009, Benjamin Kowarsch. All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * (1) Hosting of this file, or any parts thereof, on websites which contain
  19. * advertising is expressly forbidden and requires specific prior written
  20. * permission. However, the ANTLR project website and university websites
  21. * are exempt from this restriction. Exemption may be withdrawn if abused.
  22. *
  23. * (2) Redistributions of source code must retain the above copyright notice,
  24. * this list of conditions and the following disclaimer.
  25. *
  26. * (3) Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in the
  28. * documentation and other materials provided with the distribution.
  29. *
  30. * (4) Neither the author's name nor the names of any contributors may be used
  31. * to endorse or promote products derived from this software without
  32. * specific prior written permission.
  33. *
  34. * (5) Where this list of conditions or the following disclaimer, in part or
  35. * as a whole is overruled or nullified by applicable law, no permission
  36. * is granted to use the software.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  39. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  41. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  42. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  43. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  44. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  45. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  46. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  47. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  48. * POSSIBILITY OF SUCH DAMAGE.
  49. *
  50. */
  51. grammar m2pim3; // Modula-2 PIM3 standard
  52. // file version 1.01, May 19, 2009
  53. tokens {
  54. // Reserved Words
  55. AND = 'AND';
  56. ARRAY = 'ARRAY';
  57. BEGIN = 'BEGIN';
  58. BY = 'BY';
  59. CASE = 'CASE';
  60. CONST = 'CONST';
  61. DEFINITION = 'DEFINITION';
  62. DIV = 'DIV';
  63. DO = 'DO';
  64. ELSE = 'ELSE';
  65. ELSIF = 'ELSIF';
  66. END = 'END';
  67. EXIT = 'EXIT';
  68. EXPORT = 'EXPORT';
  69. FOR = 'FOR';
  70. FROM = 'FROM';
  71. IF = 'IF';
  72. IMPLEMENTATION = 'IMPLEMENTATION';
  73. IMPORT = 'IMPORT';
  74. IN = 'IN';
  75. LOOP = 'LOOP';
  76. MOD = 'MOD';
  77. MODULE = 'MODULE';
  78. NOT = 'NOT';
  79. OF = 'OF';
  80. OR = 'OR';
  81. POINTER = 'POINTER';
  82. PROCEDURE = 'PROCEDURE';
  83. QUALIFIED = 'QUALIFIED';
  84. RECORD = 'RECORD';
  85. REPEAT = 'REPEAT';
  86. RETURN = 'RETURN';
  87. SET = 'SET';
  88. THEN = 'THEN';
  89. TO = 'TO';
  90. TYPE = 'TYPE';
  91. UNTIL = 'UNTIL';
  92. VAR = 'VAR';
  93. WHILE = 'WHILE';
  94. WITH = 'WITH';
  95. // Operators
  96. ASSIGN_OP = ':=';
  97. LOGICAL_AND_OP = '&';
  98. LOGICAL_NOT_OP = '~';
  99. EQUAL_OP = '=';
  100. NOT_EQUAL_OP = '#';
  101. PASCAL_NOT_EQUAL_OP = '<>';
  102. GREATER_OP = '>';
  103. GREATER_OR_EQUAL_OP = '>=';
  104. LESS_OP = '<';
  105. LESS_OR_EQUAL_OP = '<=';
  106. PLUS_OP = '+';
  107. MINUS_OP = '-';
  108. MULTIPLY_OP = '*';
  109. DIVIDE_OP = '/';
  110. POINTER_OP = '^';
  111. // Punctuation
  112. DOT = '.';
  113. DOTDOT = '..';
  114. }
  115. // ---------------------------------------------------------------------------
  116. // P A R S E R G R A M M A R
  117. // ---------------------------------------------------------------------------
  118. // PIM 3 Appendix 1 line 1
  119. ident : IDENT ; // see lexer
  120. // PIM 3 Appendix 1 line 2
  121. number : INTEGER | REAL ; // see lexer
  122. // PIM 3 Appendix 1 lines 3-4
  123. integer : INTEGER ; // see lexer
  124. // PIM 3 Appendix 1 line 5
  125. real : REAL ; // see lexer
  126. // PIM 3 Appendix 1 line 6
  127. scaleFactor : SCALE_FACTOR ; // see lexer
  128. // PIM 3 Appendix 1 line 7
  129. hexDigit : HEX_DIGIT ; // see lexer
  130. // PIM 3 Appendix 1 line 8
  131. digit : DIGIT ; // see lexer
  132. // PIM 3 Appendix 1 line 9
  133. octalDigit : OCTAL_DIGIT ; // see lexer
  134. // PIM 3 Appendix 1 line 10
  135. string : STRING ; // see lexer
  136. // PIM 3 Appendix 1 line 11
  137. qualident :
  138. ident ( '.' ident )*
  139. ;
  140. // PIM 3 Appendix 1 line 12
  141. constantDeclaration :
  142. ident '=' constExpression
  143. ;
  144. // PIM 3 Appendix 1 line 13
  145. constExpression :
  146. simpleConstExpr ( relation simpleConstExpr )?
  147. ;
  148. // PIM 3 Appendix 1 line 14
  149. relation :
  150. '=' | '#' | '<>' | '<' | '<=' | '>' | '>=' | 'IN'
  151. ;
  152. // PIM 3 Appendix 1 line 15
  153. simpleConstExpr :
  154. ( '+' | '-' )? constTerm ( addOperator constTerm )*
  155. ;
  156. // PIM 3 Appendix 1 line 16
  157. addOperator :
  158. '+' | '-' | 'OR'
  159. ;
  160. // PIM 3 Appendix 1 line 17
  161. constTerm :
  162. constFactor ( mulOperator constFactor )*
  163. ;
  164. // PIM 3 Appendix 1 line 18
  165. mulOperator :
  166. '*' | '/' | 'DIV' | 'MOD' | 'AND' | '&'
  167. ;
  168. // PIM 3 Appendix 1 lines 19-20
  169. constFactor :
  170. qualident | number | string | constSet |
  171. '(' constExpression ')' | ( 'NOT' | '~' ) constFactor
  172. ;
  173. // PIM 3 Appendix 1 line 21
  174. constSet :
  175. ( qualident )? '{' ( constElement ( ',' constElement )* )? '}'
  176. ;
  177. // PIM 3 Appendix 1 line 22
  178. constElement :
  179. constExpression ( '..' constExpression )?
  180. ;
  181. // PIM 3 Appendix 1 line 23
  182. typeDeclaration :
  183. ident '=' type
  184. ;
  185. // PIM 3 Appendix 1 lines 24-25
  186. type :
  187. simpleType | arrayType | recordType | setType | pointerType | procedureType
  188. ;
  189. // PIM 3 Appendix 1 line 26
  190. simpleType :
  191. qualident | enumeration | subrangeType
  192. ;
  193. // PIM 3 Appendix 1 line 27
  194. enumeration :
  195. '(' identList ')'
  196. ;
  197. // PIM 3 Appendix 1 line 28
  198. identList :
  199. ident ( ',' ident )*
  200. ;
  201. // PIM 3 Appendix 1 line 29
  202. subrangeType :
  203. ( ident )? '[' constExpression '..' constExpression ']'
  204. ;
  205. // PIM 3 Appendix 1 line 30
  206. arrayType :
  207. 'ARRAY' simpleType ( ',' simpleType )* 'OF' type
  208. ;
  209. // PIM 3 Appendix 1 line 31
  210. recordType :
  211. 'RECORD' fieldListSequence 'END'
  212. ;
  213. // PIM 3 Appendix 1 line 32
  214. fieldListSequence :
  215. fieldList ( ';' fieldList )*
  216. ;
  217. // PIM 3 Appendix 1 lines 33-35
  218. fieldList :
  219. identList ':' type |
  220. 'CASE' ( ident )? ':' qualident 'OF' variant ( '|' variant )*
  221. ( 'ELSE' fieldListSequence )?
  222. 'END'
  223. ;
  224. // PIM 3 Appendix 1 line 36
  225. variant :
  226. ( caseLabelList ':' fieldListSequence )?
  227. ;
  228. // PIM 3 Appendix 1 line 37
  229. caseLabelList :
  230. caseLabels ( ',' caseLabels )*
  231. ;
  232. // PIM 3 Appendix 1 line 38
  233. caseLabels :
  234. constExpression ( '..' constExpression )?
  235. ;
  236. // PIM 3 Appendix 1 line 39
  237. setType :
  238. 'SET' 'OF' simpleType
  239. ;
  240. // PIM 3 Appendix 1 line 40
  241. pointerType :
  242. 'POINTER' 'TO' type
  243. ;
  244. // PIM 3 Appendix 1 line 41
  245. procedureType :
  246. 'PROCEDURE' ( formalTypeList )?
  247. ;
  248. // PIM 3 Appendix 1 lines 42-43
  249. formalTypeList :
  250. '(' ( ( 'VAR' )? formalType ( ',' ( 'VAR' )? formalType )* )? ')'
  251. ( ':' qualident )?
  252. ;
  253. // PIM 3 Appendix 1 line 44
  254. variableDeclaration :
  255. identList ':' type
  256. ;
  257. // PIM 3 Appendix 1 line 45
  258. designator :
  259. qualident ( '.' ident | '[' expList ']' | '^' )*
  260. ;
  261. // PIM 3 Appendix 1 line 46
  262. expList :
  263. expression ( ',' expression )*
  264. ;
  265. // PIM 3 Appendix 1 line 47
  266. expression :
  267. simpleExpression ( relation simpleExpression )?
  268. ;
  269. // PIM 3 Appendix 1 line 48
  270. simpleExpression :
  271. ( '+' | '-' )? term ( addOperator term )*
  272. ;
  273. // PIM 3 Appendix 1 line 49
  274. term :
  275. factor ( mulOperator factor )*
  276. ;
  277. // PIM 3 Appendix 1 lines 50-51
  278. factor :
  279. number | string | set | designator ( actualParameters )? |
  280. '(' expression ')' | 'NOT' factor
  281. ;
  282. // PIM 3 Appendix 1 line 52
  283. set :
  284. ( qualident )? '{' ( element ( ',' element )* )? '}'
  285. ;
  286. // PIM 3 Appendix 1 line 53
  287. element :
  288. expression ( '..' expression )?
  289. ;
  290. // PIM 3 Appendix 1 line 54
  291. actualParameters :
  292. '(' ( expList )? ')'
  293. ;
  294. // PIM 3 Appendix 1 lines 55-58
  295. statement :
  296. ( assignment | procedureCall | ifStatement | caseStatement |
  297. whileStatement | repeatStatement | loopStatement | forStatement |
  298. withStatement | 'EXIT' | 'RETURN' ( expression )? )?
  299. ;
  300. // PIM 3 Appendix 1 line 59
  301. assignment :
  302. designator ':=' expression
  303. ;
  304. // PIM 3 Appendix 1 line 60
  305. procedureCall :
  306. designator ( actualParameters )?
  307. ;
  308. // PIM 3 Appendix 1 line 61
  309. statementSequence :
  310. statement ( ';' statement )*
  311. ;
  312. // PIM 3 Appendix 1 lines 62-64
  313. ifStatement :
  314. 'IF' expression 'THEN' statementSequence
  315. ( 'ELSIF' expression 'THEN' statementSequence )*
  316. ( 'ELSE' statementSequence )?
  317. 'END'
  318. ;
  319. // PIM 3 Appendix 1 lines 65-66
  320. caseStatement :
  321. 'CASE' expression 'OF' case ( '|' case )*
  322. ( 'ELSE' statementSequence )?
  323. 'END'
  324. ;
  325. // PIM 3 Appendix 1 line 67
  326. case :
  327. ( caseLabelList ':' statementSequence )?
  328. ;
  329. // PIM 3 Appendix 1 line 68
  330. whileStatement :
  331. 'WHILE' expression 'DO' statementSequence 'END'
  332. ;
  333. // PIM 3 Appendix 1 line 69
  334. repeatStatement :
  335. 'REPEAT' statementSequence 'UNTIL' expression
  336. ;
  337. // PIM 3 Appendix 1 lines 70-71
  338. forStatement :
  339. 'FOR' ident ':=' expression 'TO' expression ( 'BY' constExpression )?
  340. 'DO' statementSequence 'END'
  341. ;
  342. // PIM 3 Appendix 1 line 72
  343. loopStatement :
  344. 'LOOP' statementSequence 'END'
  345. ;
  346. // PIM 3 Appendix 1 line 73
  347. withStatement :
  348. 'WITH' designator 'DO' statementSequence 'END'
  349. ;
  350. // PIM 3 Appendix 1 line 74
  351. procedureDeclaration :
  352. procedureHeading ';' block ident
  353. ;
  354. // PIM 3 Appendix 1 line 75
  355. procedureHeading :
  356. 'PROCEDURE' ident ( formalParameters )?
  357. ;
  358. // PIM 3 Appendix 1 line 76
  359. block :
  360. ( declaration )*
  361. ( 'BEGIN' statementSequence )? 'END'
  362. ;
  363. // PIM 3 Appendix 1 lines 78-80
  364. declaration :
  365. 'CONST' ( constantDeclaration ';' )* |
  366. 'TYPE' ( typeDeclaration ';' )* |
  367. 'VAR' ( variableDeclaration ';' )* |
  368. procedureDeclaration ';' |
  369. moduleDeclaration ';'
  370. ;
  371. // PIM 3 Appendix 1 lines 81-82
  372. formalParameters :
  373. '(' ( fpSection ( ';' fpSection )* )? ')' ( ':' qualident )?
  374. ;
  375. // PIM 3 Appendix 1 line 83
  376. fpSection :
  377. ( 'VAR' )? identList ':' formalType
  378. ;
  379. // PIM 3 Appendix 1 line 84
  380. formalType :
  381. ( 'ARRAY' 'OF' )? qualident
  382. ;
  383. // PIM 3 Appendix 1 lines 85-86
  384. moduleDeclaration :
  385. 'MODULE' ident ( priority )? ';'
  386. ( importList )* ( exportList )?
  387. block ident
  388. ;
  389. // PIM 3 Appendix 1 line87
  390. priority :
  391. '[' constExpression ']'
  392. ;
  393. // PIM 3 Appendix 1 line 88
  394. exportList :
  395. 'EXPORT' ( 'QUALIFIED' )? identList ';'
  396. ;
  397. // PIM 3 Appendix 1 line 89
  398. importList :
  399. ( 'FROM' ident )? 'IMPORT' identList ';'
  400. ;
  401. // PIM 3 Appendix 1 lines 90-91
  402. definitionModule :
  403. ( importList )*
  404. ( definition )*
  405. 'END' ident '.'
  406. ;
  407. // PIM 3 Appendix 1 lines 92-95
  408. definition :
  409. 'CONST' ( constantDeclaration ';' )* |
  410. 'TYPE' ( typeDeclaration ';' )* |
  411. 'VAR' ( variableDeclaration ';')* |
  412. procedureHeading ';'
  413. ;
  414. // PIM 3 Appendix 1 lines 96-97
  415. programModule :
  416. 'MODULE' ident ( priority )? ';'
  417. ( importList )* block ident '.'
  418. ;
  419. // PIM 3 Appendix 1 lines 98-99
  420. compilationUnit :
  421. definitionModule | ( 'IMPLEMENTATION' )? programModule
  422. ;
  423. // ---------------------------------------------------------------------------
  424. // L E X E R G R A M M A R
  425. // ---------------------------------------------------------------------------
  426. // PIM 3 Appendix 1 line 1
  427. IDENT : LETTER ( LETTER | DIGIT )* ;
  428. // PIM 3 Appendix 1 lines 3-4
  429. INTEGER :
  430. DIGIT ( DIGIT )* |
  431. OCTAL_DIGIT ( OCTAL_DIGIT )* ( 'B' | 'C' ) |
  432. DIGIT ( HEX_DIGIT )* 'H'
  433. ;
  434. // PIM 3 Appendix 1 line 5
  435. REAL :
  436. DIGIT ( DIGIT )* '.' ( DIGIT )* ( SCALE_FACTOR )?
  437. ;
  438. // PIM 3 Appendix 1 line 10
  439. STRING :
  440. '\'' ( CHARACTER )* '\'' | '"' ( CHARACTER )* '"'
  441. ;
  442. // letter remains undefined in PIM 3
  443. fragment
  444. LETTER : 'A' .. 'Z' | 'a' .. 'z' ;
  445. // PIM 3 Appendix 1 line 11
  446. fragment
  447. DIGIT : OCTAL_DIGIT | '8' | '9' ;
  448. // PIM 3 Appendix 1 line 9
  449. fragment
  450. OCTAL_DIGIT : '0' .. '7' ;
  451. // PIM 3 Appendix 1 line 7
  452. fragment
  453. HEX_DIGIT : DIGIT | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' ;
  454. // PIM 3 Appendix 1 line 6
  455. fragment
  456. SCALE_FACTOR :
  457. 'E' ( '+' | '-' )? DIGIT ( DIGIT )*
  458. ;
  459. // character remains undefined in PIM 3
  460. fragment
  461. CHARACTER :
  462. // any printable characters other than single and double quote
  463. ' ' | '!' | '#' .. '&' | '(' .. '~'
  464. ;
  465. // END OF FILE