m2pim4.g 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /* ANTLR v3 Grammar for Modula-2 (PIM4)
  2. *
  3. * Documentation version, derived from 4th 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 fourth 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 m2pim4; // Modula-2 PIM4 standard
  52. // file version 1.00, July 10, 2009
  53. // Note: an empty semantic action {} is used in lists of alternative terminals
  54. // in order to make ANTLRworks display the alternatives as separate branches.
  55. tokens {
  56. // Reserved Words
  57. AND = 'AND';
  58. ARRAY = 'ARRAY';
  59. BEGIN = 'BEGIN';
  60. BY = 'BY';
  61. CASE = 'CASE';
  62. CONST = 'CONST';
  63. DEFINITION = 'DEFINITION';
  64. DIV = 'DIV';
  65. DO = 'DO';
  66. ELSE = 'ELSE';
  67. ELSIF = 'ELSIF';
  68. END = 'END';
  69. EXIT = 'EXIT';
  70. EXPORT = 'EXPORT';
  71. FOR = 'FOR';
  72. FROM = 'FROM';
  73. IF = 'IF';
  74. IMPLEMENTATION = 'IMPLEMENTATION';
  75. IMPORT = 'IMPORT';
  76. IN = 'IN';
  77. LOOP = 'LOOP';
  78. MOD = 'MOD';
  79. MODULE = 'MODULE';
  80. NOT = 'NOT';
  81. OF = 'OF';
  82. OR = 'OR';
  83. POINTER = 'POINTER';
  84. PROCEDURE = 'PROCEDURE';
  85. QUALIFIED = 'QUALIFIED';
  86. RECORD = 'RECORD';
  87. REPEAT = 'REPEAT';
  88. RETURN = 'RETURN';
  89. SET = 'SET';
  90. THEN = 'THEN';
  91. TO = 'TO';
  92. TYPE = 'TYPE';
  93. UNTIL = 'UNTIL';
  94. VAR = 'VAR';
  95. WHILE = 'WHILE';
  96. WITH = 'WITH';
  97. }
  98. // ---------------------------------------------------------------------------
  99. // P A R S E R G R A M M A R
  100. // ---------------------------------------------------------------------------
  101. // PIM 4 Appendix 1 line 1
  102. ident : IDENT ; // see lexer
  103. // PIM 4 Appendix 1 line 2
  104. number : INTEGER | REAL ; // see lexer
  105. // PIM 4 Appendix 1 lines 3-4
  106. integer : INTEGER ; // see lexer
  107. // PIM 4 Appendix 1 line 5
  108. real : REAL ; // see lexer
  109. // PIM 4 Appendix 1 line 6
  110. scaleFactor : SCALE_FACTOR ; // see lexer
  111. // PIM 4 Appendix 1 line 7
  112. hexDigit : HEX_DIGIT ; // see lexer
  113. // PIM 4 Appendix 1 line 8
  114. digit : DIGIT ; // see lexer
  115. // PIM 4 Appendix 1 line 9
  116. octalDigit : OCTAL_DIGIT ; // see lexer
  117. // PIM 4 Appendix 1 line 10
  118. string : STRING ; // see lexer
  119. // PIM 4 Appendix 1 line 11
  120. qualident :
  121. ident ( '.' ident )*
  122. ;
  123. // PIM 4 Appendix 1 line 12
  124. constantDeclaration :
  125. ident '=' constExpression
  126. ;
  127. // PIM 4 Appendix 1 line 13
  128. constExpression :
  129. simpleConstExpr ( relation simpleConstExpr )?
  130. ;
  131. // PIM 4 Appendix 1 line 14
  132. relation :
  133. '=' | '#' | '<>' | '<' | '<=' | '>' | '>=' | 'IN' {}
  134. ;
  135. // PIM 4 Appendix 1 line 15
  136. simpleConstExpr :
  137. ( '+' | '-' {})? constTerm ( addOperator constTerm )*
  138. ;
  139. // PIM 4 Appendix 1 line 16
  140. addOperator :
  141. '+' | '-' | OR {}
  142. ;
  143. // PIM 4 Appendix 1 line 17
  144. constTerm :
  145. constFactor ( mulOperator constFactor )*
  146. ;
  147. // PIM 4 Appendix 1 line 18
  148. mulOperator :
  149. '*' | '/' | DIV | MOD | AND | '&' {}
  150. ;
  151. // PIM 4 Appendix 1 lines 19-20
  152. constFactor :
  153. qualident | number | string | set |
  154. '(' constExpression ')' | ( NOT | '~' {}) constFactor
  155. ;
  156. // PIM 4 Appendix 1 line 21
  157. set :
  158. qualident? '{' ( element ( ',' element )* )? '}'
  159. ;
  160. // PIM 4 Appendix 1 line 22
  161. element :
  162. constExpression ( '..' constExpression )?
  163. ;
  164. // PIM 4 Appendix 1 line 23
  165. typeDeclaration :
  166. ident '=' type
  167. ;
  168. // PIM 4 Appendix 1 lines 24-25
  169. type :
  170. simpleType | arrayType | recordType | setType | pointerType | procedureType
  171. ;
  172. // PIM 4 Appendix 1 line 26
  173. simpleType :
  174. qualident | enumeration | subrangeType
  175. ;
  176. // PIM 4 Appendix 1 line 27
  177. enumeration :
  178. '(' identList ')'
  179. ;
  180. // PIM 4 Appendix 1 line 28
  181. identList :
  182. ident ( ',' ident )*
  183. ;
  184. // PIM 4 Appendix 1 line 29
  185. subrangeType :
  186. '[' constExpression '..' constExpression ']'
  187. ;
  188. // PIM 4 Appendix 1 line 30
  189. arrayType :
  190. ARRAY simpleType ( ',' simpleType )* OF type
  191. ;
  192. // PIM 4 Appendix 1 line 31
  193. recordType :
  194. RECORD fieldListSequence END
  195. ;
  196. // PIM 4 Appendix 1 line 32
  197. fieldListSequence :
  198. fieldList ( ';' fieldList )*
  199. ;
  200. // PIM 4 Appendix 1 lines 33-35
  201. fieldList :
  202. ( identList ':' type |
  203. CASE ( ident ':' )? qualident OF variant ( '|' variant )*
  204. ( ELSE fieldListSequence )?
  205. END )?
  206. ;
  207. // PIM 4 Appendix 1 line 36
  208. variant :
  209. caseLabelList ':' fieldListSequence
  210. ;
  211. // PIM 4 Appendix 1 line 37
  212. caseLabelList :
  213. caseLabels ( ',' caseLabels )*
  214. ;
  215. // PIM 4 Appendix 1 line 38
  216. caseLabels :
  217. constExpression ( '..' constExpression )?
  218. ;
  219. // PIM 4 Appendix 1 line 39
  220. setType :
  221. SET OF simpleType
  222. ;
  223. // PIM 4 Appendix 1 line 40
  224. pointerType :
  225. POINTER TO type
  226. ;
  227. // PIM 4 Appendix 1 line 41
  228. procedureType :
  229. PROCEDURE formalTypeList?
  230. ;
  231. // PIM 4 Appendix 1 lines 42-43
  232. formalTypeList :
  233. '(' ( VAR? formalType ( ',' VAR? formalType )* )? ')'
  234. ( ':' qualident )?
  235. ;
  236. // PIM 4 Appendix 1 line 44
  237. variableDeclaration :
  238. identList ':' type
  239. ;
  240. // PIM 4 Appendix 1 line 45
  241. designator :
  242. qualident ( '.' ident | '[' expList ']' | '^' )*
  243. ;
  244. // PIM 4 Appendix 1 line 46
  245. expList :
  246. expression ( ',' expression )*
  247. ;
  248. // PIM 4 Appendix 1 line 47
  249. expression :
  250. simpleExpression ( relation simpleExpression )?
  251. ;
  252. // PIM 4 Appendix 1 line 48
  253. simpleExpression :
  254. ( '+' | '-' {})? term ( addOperator term )*
  255. ;
  256. // PIM 4 Appendix 1 line 49
  257. term :
  258. factor ( mulOperator factor )*
  259. ;
  260. // PIM 4 Appendix 1 lines 50-51
  261. factor :
  262. number | string | set | designator ( actualParameters )? |
  263. '(' expression ')' | ( NOT | '~' {}) factor
  264. ;
  265. // PIM 4 Appendix 1 line 52
  266. actualParameters :
  267. '(' expList? ')'
  268. ;
  269. // PIM 4 Appendix 1 lines 53-56
  270. statement :
  271. ( assignment | procedureCall | ifStatement | caseStatement |
  272. whileStatement | repeatStatement | loopStatement | forStatement |
  273. withStatement | EXIT | RETURN expression? )?
  274. ;
  275. // PIM 4 Appendix 1 line 57
  276. assignment :
  277. designator ':=' expression
  278. ;
  279. // PIM 4 Appendix 1 line 58
  280. procedureCall :
  281. designator ( actualParameters )?
  282. ;
  283. // PIM 4 Appendix 1 line 59
  284. statementSequence :
  285. statement ( ';' statement )*
  286. ;
  287. // PIM 4 Appendix 1 lines 60-62
  288. ifStatement :
  289. IF expression THEN statementSequence
  290. ( ELSIF expression THEN statementSequence )*
  291. ( ELSE statementSequence )?
  292. END
  293. ;
  294. // PIM 4 Appendix 1 lines 63-64
  295. caseStatement :
  296. CASE expression OF case ( '|' case )*
  297. ( ELSE statementSequence )?
  298. END
  299. ;
  300. // PIM 4 Appendix 1 line 65
  301. case :
  302. caseLabelList ':' statementSequence
  303. ;
  304. // PIM 4 Appendix 1 line 66
  305. whileStatement :
  306. WHILE expression DO statementSequence END
  307. ;
  308. // PIM 4 Appendix 1 line 67
  309. repeatStatement :
  310. REPEAT statementSequence UNTIL expression
  311. ;
  312. // PIM 4 Appendix 1 lines 68-69
  313. forStatement :
  314. FOR ident ':=' expression TO expression ( BY constExpression )?
  315. DO statementSequence END
  316. ;
  317. // PIM 4 Appendix 1 line 70
  318. loopStatement :
  319. LOOP statementSequence END
  320. ;
  321. // PIM 4 Appendix 1 line 71
  322. withStatement :
  323. WITH designator DO statementSequence END
  324. ;
  325. // PIM 4 Appendix 1 line 72
  326. procedureDeclaration :
  327. procedureHeading ';' block ident
  328. ;
  329. // PIM 4 Appendix 1 line 73
  330. procedureHeading :
  331. PROCEDURE ident formalParameters?
  332. ;
  333. // PIM 4 Appendix 1 line 74
  334. block :
  335. declaration*
  336. ( BEGIN statementSequence )? END
  337. ;
  338. // PIM 4 Appendix 1 lines 75-78
  339. declaration :
  340. CONST ( constantDeclaration ';' )* |
  341. TYPE ( typeDeclaration ';' )* |
  342. VAR ( variableDeclaration ';' )* |
  343. procedureDeclaration ';' |
  344. moduleDeclaration ';'
  345. ;
  346. // PIM 4 Appendix 1 lines 79-80
  347. formalParameters :
  348. '(' ( fpSection ( ';' fpSection )* )? ')' ( ':' qualident )?
  349. ;
  350. // PIM 4 Appendix 1 line 81
  351. fpSection :
  352. VAR? identList ':' formalType
  353. ;
  354. // PIM 4 Appendix 1 line 82
  355. formalType :
  356. ( ARRAY OF )? qualident
  357. ;
  358. // PIM 4 Appendix 1 lines 83-84
  359. moduleDeclaration :
  360. MODULE ident priority? ';'
  361. importList* exportList?
  362. block ident
  363. ;
  364. // PIM 4 Appendix 1 line 85
  365. priority :
  366. '[' constExpression ']'
  367. ;
  368. // PIM 4 Appendix 1 line 86
  369. exportList :
  370. EXPORT QUALIFIED? identList ';'
  371. ;
  372. // PIM 4 Appendix 1 line 87
  373. importList :
  374. ( FROM ident )? IMPORT identList ';'
  375. ;
  376. // PIM 4 Appendix 1 lines 88-89
  377. definitionModule :
  378. DEFINITION MODULE ident ';'
  379. importList* exportList? definition*
  380. END ident '.'
  381. ;
  382. // PIM 4 Appendix 1 lines 90-93
  383. definition :
  384. CONST ( constantDeclaration ';' )* |
  385. TYPE ( ident ( '=' type )? ';' )* |
  386. VAR ( variableDeclaration ';' )* |
  387. procedureHeading ';'
  388. ;
  389. // PIM 4 Appendix 1 lines 94-95
  390. programModule :
  391. MODULE ident priority? ';'
  392. importList* block ident '.'
  393. ;
  394. // PIM 4 Appendix 1 lines 96-97
  395. compilationUnit :
  396. definitionModule | IMPLEMENTATION? programModule
  397. ;
  398. // ---------------------------------------------------------------------------
  399. // L E X E R G R A M M A R
  400. // ---------------------------------------------------------------------------
  401. // PIM 4 Appendix 1 line 1
  402. IDENT : LETTER ( LETTER | DIGIT )* ;
  403. // PIM 4 Appendix 1 lines 3-4
  404. INTEGER :
  405. DIGIT+ |
  406. OCTAL_DIGIT+ ( 'B' | 'C' {}) |
  407. DIGIT ( HEX_DIGIT )* 'H'
  408. ;
  409. // PIM 4 Appendix 1 line 5
  410. REAL :
  411. DIGIT+ '.' DIGIT* SCALE_FACTOR?
  412. ;
  413. // PIM 4 Appendix 1 line 10
  414. STRING :
  415. '\'' ( CHARACTER | '\"' )* '\'' | '"' (CHARACTER | '\'')* '"'
  416. ;
  417. // letter remains undefined in PIM 4
  418. fragment
  419. LETTER : 'A' .. 'Z' | 'a' .. 'z' {};
  420. // PIM 4 Appendix 1 line 11
  421. fragment
  422. DIGIT : OCTAL_DIGIT | '8' | '9' {};
  423. // PIM 4 Appendix 1 line 9
  424. fragment
  425. OCTAL_DIGIT : '0' .. '7' ;
  426. // PIM 4 Appendix 1 line 7
  427. fragment
  428. HEX_DIGIT : DIGIT | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' {};
  429. // PIM 4 Appendix 1 line 6
  430. fragment
  431. SCALE_FACTOR :
  432. 'E' ( '+' | '-' {})? DIGIT+
  433. ;
  434. // character remains undefined in PIM 4
  435. fragment
  436. CHARACTER :
  437. // any printable characters other than single and double quote
  438. ' ' | '!' | '#' .. '&' | '(' .. '~' {}
  439. ;
  440. // END OF FILE