m2pim3_LL1.g 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /* ANTLR v3 Grammar for Modula-2 (PIM3)
  2. *
  3. * LL(1) version, derived from 3rd Edition of Programming in Modula-2
  4. *
  5. * This grammar is a refactored LL(1) version of the Modula-2 syntax provided
  6. * in appendix 1 of "Programming in Modula-2", Third, Corrected Edition 1985,
  7. * by Niklaus Wirth, Springer Verlag, ISBN 0-387-15078-1. It follows the
  8. * naming conventions and structure of Wirth's grammar but has been refactored
  9. * to satisfy LL(1) constraints. The original, unmodified grammar by Wirth 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_LL1; // Modula-2 PIM3 standard
  52. // file version 1.01, June 22, 2009
  53. options {
  54. backtrack = false;
  55. k=1; /* LL(1) */
  56. }
  57. tokens { // Reserved Words
  58. AND = 'AND';
  59. ARRAY = 'ARRAY';
  60. BEGIN = 'BEGIN';
  61. BY = 'BY';
  62. CASE = 'CASE';
  63. CONST = 'CONST';
  64. DEFINITION = 'DEFINITION';
  65. DIV = 'DIV';
  66. DO = 'DO';
  67. ELSE = 'ELSE';
  68. ELSIF = 'ELSIF';
  69. END = 'END';
  70. EXIT = 'EXIT';
  71. EXPORT = 'EXPORT';
  72. FOR = 'FOR';
  73. FROM = 'FROM';
  74. IF = 'IF';
  75. IMPLEMENTATION = 'IMPLEMENTATION';
  76. IMPORT = 'IMPORT';
  77. IN = 'IN';
  78. LOOP = 'LOOP';
  79. MOD = 'MOD';
  80. MODULE = 'MODULE';
  81. NOT = 'NOT';
  82. OF = 'OF';
  83. OR = 'OR';
  84. POINTER = 'POINTER';
  85. PROCEDURE = 'PROCEDURE';
  86. QUALIFIED = 'QUALIFIED';
  87. RECORD = 'RECORD';
  88. REPEAT = 'REPEAT';
  89. RETURN = 'RETURN';
  90. SET = 'SET';
  91. THEN = 'THEN';
  92. TO = 'TO';
  93. TYPE = 'TYPE';
  94. UNTIL = 'UNTIL';
  95. VAR = 'VAR';
  96. WHILE = 'WHILE';
  97. WITH = 'WITH';
  98. }
  99. // ---------------------------------------------------------------------------
  100. // P A R S E R G R A M M A R
  101. // ---------------------------------------------------------------------------
  102. // ***** PIM 3 Appendix 1 line 1 *****
  103. ident : IDENT ; // see lexer
  104. // ***** PIM 3 Appendix 1 line 2 *****
  105. number : INTEGER | REAL ; // see lexer
  106. // ***** PIM 3 Appendix 1 lines 3-4 *****
  107. integer : INTEGER ; // see lexer
  108. // ***** PIM 3 Appendix 1 line 5 *****
  109. real : REAL ; // see lexer
  110. // ***** PIM 3 Appendix 1 line 6 *****
  111. scaleFactor : SCALE_FACTOR ; // see lexer
  112. // ***** PIM 3 Appendix 1 line 7 *****
  113. hexDigit : HEX_DIGIT ; // see lexer
  114. // ***** PIM 3 Appendix 1 line 8 *****
  115. digit : DIGIT ; // see lexer
  116. // ***** PIM 3 Appendix 1 line 9 *****
  117. octalDigit : OCTAL_DIGIT ; // see lexer
  118. // ***** PIM 3 Appendix 1 line 10 *****
  119. string : STRING ; // see lexer
  120. // ***** PIM 3 Appendix 1 line 11 *****
  121. qualident :
  122. ident ( '.' ident )*
  123. ;
  124. // ***** PIM 3 Appendix 1 line 12 *****
  125. constantDeclaration :
  126. ident '=' constExpression
  127. ;
  128. // ***** PIM 3 Appendix 1 line 13 *****
  129. constExpression :
  130. expression
  131. ;
  132. // ***** PIM 3 Appendix 1 line 14 *****
  133. typeDeclaration :
  134. ident '=' type
  135. ;
  136. // ***** PIM 3 Appendix 1 lines 15-16 *****
  137. type :
  138. simpleType | arrayType | recordType | setType | pointerType | procedureType
  139. ;
  140. // ***** PIM 3 Appendix 1 line 17 *****
  141. // refactored for LL(1)
  142. simpleType :
  143. subrangeTypeOrQualident |
  144. enumeration
  145. ;
  146. // new for for LL(1)
  147. subrangeTypeOrQualident :
  148. range |
  149. qualident /* <= factored out */ range?
  150. ;
  151. // new for LL1(1)
  152. range :
  153. '[' constExpression '..' constExpression ']'
  154. ;
  155. // ***** PIM 3 Appendix 1 line 18 *****
  156. enumeration :
  157. '(' identList ')'
  158. ;
  159. // ***** PIM 3 Appendix 1 line 19 *****
  160. identList :
  161. ident ( ',' ident )*
  162. ;
  163. // ***** PIM 3 Appendix 1 line 20 *****
  164. // replaced for LL(1), see subrangeTypeOrQualident
  165. // ***** PIM 3 Appendix 1 line 21 *****
  166. arrayType :
  167. ARRAY simpleType ( ',' simpleType )* OF type
  168. ;
  169. // ***** PIM 3 Appendix 1 line 22 *****
  170. recordType :
  171. RECORD fieldListSequence END
  172. ;
  173. // ***** PIM 3 Appendix 1 line 23 *****
  174. fieldListSequence :
  175. fieldList ( ';' fieldList )*
  176. ;
  177. // ***** PIM 3 Appendix 1 lines 24-26 *****
  178. fieldList :
  179. ( identList ':' type |
  180. CASE ident? ':' qualident OF variant ( '|' variant )*
  181. ( ELSE fieldListSequence )?
  182. END )?
  183. ;
  184. // ***** PIM 3 Appendix 1 line 27 *****
  185. variant :
  186. ( caseLabelList ':' fieldListSequence )?
  187. ;
  188. // ***** PIM 3 Appendix 1 line 28 *****
  189. caseLabelList :
  190. caseLabels ( ',' caseLabels )*
  191. ;
  192. // ***** PIM 3 Appendix 1 line 29 *****
  193. caseLabels :
  194. constExpression ( '..' constExpression )?
  195. ;
  196. // ***** PIM 3 Appendix 1 line 30 *****
  197. setType :
  198. SET OF simpleType
  199. ;
  200. // ***** PIM 3 Appendix 1 line 31 *****
  201. pointerType :
  202. POINTER TO type
  203. ;
  204. // ***** PIM 3 Appendix 1 line 32 *****
  205. procedureType :
  206. PROCEDURE formalTypeList?
  207. ;
  208. // ***** PIM 3 Appendix 1 lines 33 *****
  209. formalTypeList :
  210. '(' ( VAR? formalType ( ',' VAR? formalType )* )? ')'
  211. ( ':' qualident )?
  212. ;
  213. // ***** PIM 3 Appendix 1 line 35 *****
  214. variableDeclaration :
  215. identList ':' type
  216. ;
  217. // ***** PIM 3 Appendix 1 line 36 *****
  218. // refactored for LL(1)
  219. designator :
  220. qualident ( designatorTail )?
  221. ;
  222. // new for LL(1)
  223. designatorTail :
  224. ( ( '[' expList ']' | '^' ) ( '.' ident )* )+
  225. ;
  226. // new for LL(1)
  227. setOrDesignator :
  228. qualident /* <= factored out */
  229. ( set | designatorTail )?
  230. ;
  231. // ***** PIM 3 Appendix 1 line 37 *****
  232. expList :
  233. expression ( ',' expression )*
  234. ;
  235. // ***** PIM 3 Appendix 1 line 38 *****
  236. expression :
  237. simpleExpression ( relation simpleExpression )?
  238. ;
  239. // ***** PIM 3 Appendix 1 line 39 *****
  240. relation :
  241. '=' | '#' | '<' | '<=' | '>' | '>=' | 'IN'
  242. {} /* make ANTLRworks display separate branches */
  243. ;
  244. // ***** PIM 3 Appendix 1 line 40 *****
  245. simpleExpression :
  246. ( '+' | '-' {})? term ( addOperator term )*
  247. ;
  248. // ***** PIM 3 Appendix 1 line 41 *****
  249. addOperator :
  250. '+' | '-' | 'OR'
  251. {} /* make ANTLRworks display separate branches */
  252. ;
  253. // ***** PIM 3 Appendix 1 line 42
  254. term :
  255. factor ( mulOperator factor )*
  256. ;
  257. // ***** PIM 3 Appendix 1 line 43 *****
  258. // Note: PIM3 text says '&' is a synonym for 'AND'
  259. // but the grammar does not actually show it
  260. mulOperator :
  261. '*' | '/' | DIV | MOD | AND | '&'
  262. {} /* make ANTLRworks display separate branches */
  263. ;
  264. // ***** PIM 3 Appendix 1 lines 44-45 *****
  265. // refactored for LL(1)
  266. //
  267. // Note: PIM3 text says '~' is a synonym for 'NOT'
  268. // but the grammar does not actually show it
  269. factor :
  270. number |
  271. string |
  272. set |
  273. qualident ( set | designatorTail? actualParameters? ) |
  274. '(' expression ')' | ( NOT | '~' {}) factor
  275. ;
  276. // ***** PIM 3 Appendix 1 line 46 *****
  277. // refactored for LL(1)
  278. set :
  279. /* qualident has been factored out */
  280. '{' ( element ( ',' element )* )? '}'
  281. ;
  282. // ***** PIM 3 Appendix 1 line 47 *****
  283. element :
  284. expression ( '..' expression )?
  285. ;
  286. // ***** PIM 3 Appendix 1 line 48 *****
  287. actualParameters :
  288. '(' expList? ')'
  289. ;
  290. // ***** PIM 3 Appendix 1 lines 49-52 *****
  291. // refactored for LL(1)
  292. statement :
  293. ( assignmentOrProcCall | ifStatement | caseStatement |
  294. whileStatement | repeatStatement | loopStatement |
  295. forStatement | withStatement | EXIT | RETURN expression? )?
  296. ;
  297. // ***** PIM 3 Appendix 1 line 53 *****
  298. // and
  299. // ***** PIM 3 Appendix 1 line 54 *****
  300. // both replaced by
  301. // new for LL(1)
  302. assignmentOrProcCall :
  303. designator /* has been factored out */
  304. ( ':=' expression | actualParameters? )
  305. ;
  306. // ***** PIM 3 Appendix 1 line 55 *****
  307. statementSequence :
  308. statement ( ';' statement )*
  309. ;
  310. // ***** PIM 3 Appendix 1 lines 56-58 *****
  311. ifStatement :
  312. IF expression THEN statementSequence
  313. ( ELSIF expression THEN statementSequence )*
  314. ( ELSE statementSequence )?
  315. END
  316. ;
  317. // ***** PIM 3 Appendix 1 lines 59-60 *****
  318. caseStatement :
  319. CASE expression OF case ( '|' case )*
  320. ( ELSE statementSequence )?
  321. END
  322. ;
  323. // ***** PIM 3 Appendix 1 line 61 *****
  324. case :
  325. ( caseLabelList ':' statementSequence )?
  326. ;
  327. // ***** PIM 3 Appendix 1 line 62 *****
  328. whileStatement :
  329. WHILE expression DO statementSequence END
  330. ;
  331. // ***** PIM 3 Appendix 1 line 63 *****
  332. repeatStatement :
  333. REPEAT statementSequence UNTIL expression
  334. ;
  335. // ***** PIM 3 Appendix 1 lines 64-65 *****
  336. forStatement :
  337. FOR ident ':=' expression TO expression ( BY constExpression )?
  338. DO statementSequence END
  339. ;
  340. // ***** PIM 3 Appendix 1 line 66 *****
  341. loopStatement :
  342. LOOP statementSequence END
  343. ;
  344. // ***** PIM 3 Appendix 1 line 67 *****
  345. withStatement :
  346. WITH designator DO statementSequence END
  347. ;
  348. // ***** PIM 3 Appendix 1 line 68 *****
  349. procedureDeclaration :
  350. procedureHeading ';' block ident
  351. ;
  352. // ***** PIM 3 Appendix 1 line 69 *****
  353. procedureHeading :
  354. PROCEDURE ident formalParameters?
  355. ;
  356. // ***** PIM 3 Appendix 1 line 70 *****
  357. block :
  358. declaration*
  359. ( BEGIN statementSequence )? END
  360. ;
  361. // ***** PIM 3 Appendix 1 lines 71-74 *****
  362. declaration :
  363. CONST ( constantDeclaration ';' )* |
  364. TYPE ( typeDeclaration ';' )* |
  365. VAR ( variableDeclaration ';' )* |
  366. procedureDeclaration ';' |
  367. moduleDeclaration ';'
  368. ;
  369. // ***** PIM 3 Appendix 1 lines 75-76 *****
  370. formalParameters :
  371. '(' ( fpSection ( ';' fpSection )* )? ')' ( ':' qualident )?
  372. ;
  373. // ***** PIM 3 Appendix 1 line 77 *****
  374. fpSection :
  375. VAR? identList ':' formalType
  376. ;
  377. // ***** PIM 3 Appendix 1 line 78 *****
  378. formalType :
  379. ( ARRAY OF )? qualident
  380. ;
  381. // ***** PIM 3 Appendix 1 lines 79-80 *****
  382. moduleDeclaration :
  383. MODULE ident priority? ';'
  384. importList* exportList?
  385. block ident
  386. ;
  387. // ***** PIM 3 Appendix 1 line 81 *****
  388. priority :
  389. '[' constExpression ']'
  390. ;
  391. // ***** PIM 3 Appendix 1 line 82 *****
  392. // Note, ANTLR treats 'export' as a reserved word
  393. // => rule name changed to 'exportList'
  394. exportList :
  395. EXPORT QUALIFIED? identList ';'
  396. ;
  397. // ***** PIM 3 Appendix 1 line 83 *****
  398. // Note, ANTLR treats 'import' as a reserved word
  399. // => rule name changed to 'importList'
  400. importList :
  401. ( FROM ident )? IMPORT identList ';'
  402. ;
  403. // ***** PIM 3 Appendix 1 lines 84-85 *****
  404. definitionModule :
  405. DEFINITION MODULE ident ';'
  406. importList*
  407. definition*
  408. END ident '.'
  409. ;
  410. // ***** PIM 3 Appendix 1 lines 86-89 *****
  411. definition :
  412. CONST ( constantDeclaration ';' )* |
  413. TYPE ( ident ( '=' type )? ';' )* |
  414. VAR ( variableDeclaration ';' )* |
  415. procedureHeading ';'
  416. ;
  417. // ***** PIM 3 Appendix 1 line 90 *****
  418. programModule :
  419. MODULE ident priority? ';'
  420. importList* block ident '.'
  421. ;
  422. // ***** PIM 3 Appendix 1 lines 91 *****
  423. compilationUnit :
  424. definitionModule | IMPLEMENTATION? programModule
  425. ;
  426. // ---------------------------------------------------------------------------
  427. // L E X E R G R A M M A R
  428. // ---------------------------------------------------------------------------
  429. // ***** PIM 3 Appendix 1 line 1 *****
  430. IDENT : LETTER ( LETTER | DIGIT )* ;
  431. // ***** PIM 3 Appendix 1 lines 3-4 *****
  432. INTEGER :
  433. DIGIT+ |
  434. OCTAL_DIGIT+ ( 'B' | 'C' {}) |
  435. DIGIT HEX_DIGIT* 'H'
  436. ;
  437. // ***** PIM 3 Appendix 1 line 5 *****
  438. REAL :
  439. DIGIT+ '.' DIGIT* SCALE_FACTOR?
  440. ;
  441. // ***** PIM 3 Appendix 1 line 6 *****
  442. fragment
  443. SCALE_FACTOR :
  444. 'E' ( '+' | '-' {})? DIGIT+
  445. ;
  446. // ***** PIM 3 Appendix 1 line 7 *****
  447. fragment
  448. HEX_DIGIT :
  449. DIGIT | 'A' | 'B' | 'C' | 'D' | 'E' | 'F'
  450. {} /* make ANTLRworks display separate branches */
  451. ;
  452. // ***** PIM 3 Appendix 1 line 8 *****
  453. fragment
  454. DIGIT :
  455. OCTAL_DIGIT | '8' | '9'
  456. {} /* make ANTLRworks display separate branches */
  457. ;
  458. // ***** PIM 3 Appendix 1 line 9 *****
  459. fragment
  460. OCTAL_DIGIT :
  461. '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7'
  462. {} /* make ANTLRworks display separate branches */
  463. ;
  464. // ***** PIM 3 Appendix 1 line 10 *****
  465. // Nore, the formal definition of string in PIM3 does not match
  466. // the plain English description of string in the text.
  467. // => changed to match textual description
  468. STRING :
  469. '\'' ( CHARACTER | '"' )* '\'' | '"' ( CHARACTER | '\'' )* '"'
  470. ;
  471. // ***** PIM3 provides no formal definition for letter *****
  472. fragment
  473. LETTER : 'A' .. 'Z' | 'a' .. 'z' ;
  474. // ***** PIM3 provides no formal definition for character *****
  475. fragment
  476. CHARACTER :
  477. // any printable characters other than single and double quote
  478. ' ' | '!' | '#' | '$' | '%' | '&' | '(' | ')' | '*' | '+' |
  479. ',' | '-' | '.' | ':' | ';' | '<' | '=' | '>' | '?' | '@' |
  480. '[' | '\\' | ']' | '^' | '_' | '`' | '{' | '|' | '}' | '~' |
  481. DIGIT | LETTER
  482. ;
  483. // ***** PIM3 provides no formal definition for comment *****
  484. COMMENT :
  485. '(*' ( options { greedy = false; } : . )* COMMENT '*)'
  486. { channel = hidden; }
  487. ;
  488. // ***** PIM3 provides no formal definition for whitespace *****
  489. WHITESPACE :
  490. ( ' ' | '\t' | '\u000C' | '\r' | '\n')
  491. { channel = hidden; }
  492. ;
  493. // END OF FILE