m2pim4_LL1.g 14 KB

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