m2iso.g 19 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /* ANTLR v3 Grammar for ISO-Modula-2
  2. *
  3. * Documentation version, derived from the ISO/IEC IS 10514 base standard
  4. *
  5. * This grammar follows the naming conventions and structure of the concrete
  6. * syntax of ISO Modula-2 as provided in the ISO/IEC IS 10514 base standard.
  7. * It is important to note that the concrete syntax in the standard has been
  8. * written for the purpose of documenting the language, not for the purpose
  9. * of constructing a compiler. Therefore, this grammar does not satsify the
  10. * constraints for parser construction, and consequently ANTLR will not
  11. * validate the grammar in this form. It can however be used for generating
  12. * syntax graphs using ANTLRworks, or as a starting point for refactoring.
  13. *
  14. * As time permits, the author will do the necessary refactoring to convert
  15. * the grammar into an equivalent LL(1) grammar, suitable for building a
  16. * compiler with ANTLR, which shall be published as a separate file.
  17. *
  18. *
  19. * Copyright (C) 2009, Benjamin Kowarsch. All rights reserved.
  20. *
  21. * Redistribution and use in source and binary forms, with or without
  22. * modification, are permitted provided that the following conditions are met:
  23. *
  24. * (1) Hosting of this file, or any parts thereof, on websites which contain
  25. * advertising is expressly forbidden and requires specific prior written
  26. * permission. However, the ANTLR project website and university websites
  27. * are exempt from this restriction. Exemption may be withdrawn if abused.
  28. *
  29. * (2) Redistributions of source code must retain the above copyright notice,
  30. * this list of conditions and the following disclaimer.
  31. *
  32. * (3) Redistributions in binary form must reproduce the above copyright
  33. * notice, this list of conditions and the following disclaimer in the
  34. * documentation and other materials provided with the distribution.
  35. *
  36. * (4) Neither the author's name nor the names of any contributors may be used
  37. * to endorse or promote products derived from this software without
  38. * specific prior written permission.
  39. *
  40. * (5) Where this list of conditions or the following disclaimer, in part or
  41. * as a whole is overruled or nullified by applicable law, no permission
  42. * is granted to use the software.
  43. *
  44. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  45. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  47. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  48. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  49. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  50. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  51. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  52. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  53. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  54. * POSSIBILITY OF SUCH DAMAGE.
  55. *
  56. */
  57. grammar m2iso; // Modula-2 ISO/IEC IS 10514 base standard
  58. // file version 1.00, May 16, 2009
  59. tokens {
  60. // Reserved Words
  61. AND = 'AND';
  62. ARRAY = 'ARRAY';
  63. BEGIN = 'BEGIN';
  64. BY = 'BY';
  65. CASE = 'CASE';
  66. CONST = 'CONST';
  67. DEFINITION = 'DEFINITION';
  68. DIV = 'DIV';
  69. DO = 'DO';
  70. ELSE = 'ELSE';
  71. ELSIF = 'ELSIF';
  72. END = 'END';
  73. EXCEPT = 'EXCEPT';
  74. EXIT = 'EXIT';
  75. EXPORT = 'EXPORT';
  76. FINALLY = 'FINALLY';
  77. FOR = 'FOR';
  78. FORWARD = 'FORWARD';
  79. FROM = 'FROM';
  80. IF = 'IF';
  81. IMPLEMENTATION = 'IMPLEMENTATION';
  82. IMPORT = 'IMPORT';
  83. IN = 'IN';
  84. LOOP = 'LOOP';
  85. MOD = 'MOD';
  86. MODULE = 'MODULE';
  87. NOT = 'NOT';
  88. OF = 'OF';
  89. OR = 'OR';
  90. PACKEDSET = 'PACKEDSET';
  91. POINTER = 'POINTER';
  92. PROCEDURE = 'PROCEDURE';
  93. QUALIFIED = 'QUALIFIED';
  94. RECORD = 'RECORD';
  95. REM = 'REM';
  96. REPEAT = 'REPEAT';
  97. RETRY = 'RETRY';
  98. RETURN = 'RETURN';
  99. SET = 'SET';
  100. THEN = 'THEN';
  101. TO = 'TO';
  102. TYPE = 'TYPE';
  103. UNTIL = 'UNTIL';
  104. VAR = 'VAR';
  105. WHILE = 'WHILE';
  106. WITH = 'WITH';
  107. // Operators
  108. ASSIGN_OP = ':=';
  109. LOGICAL_AND_OP = '&';
  110. LOGICAL_NOT_OP = '~';
  111. EQUALS = '=';
  112. NOT_EQUAL_OP = '#';
  113. PASCAL_NOT_EQUAL_OP = '<>';
  114. GREATER_OP = '>';
  115. GREATER_OR_EQUAL_OP = '>=';
  116. LESS_OP = '<';
  117. LESS_OR_EQUAL_OP = '<=';
  118. PLUS_OP = '+';
  119. MINUS_OP = '-';
  120. MULTIPLY_OP = '*';
  121. DIVIDE_OP = '/';
  122. POINTER_OP = '^';
  123. // Punctuation
  124. PERIOD = '.';
  125. ELLIPSIS = '..';
  126. }
  127. // ---------------------------------------------------------------------------
  128. // P A R S E R G R A M M A R
  129. // ---------------------------------------------------------------------------
  130. // 1.1 Programs and Compilation Modules
  131. compilation_module :
  132. program_module | definition_module | implementation_module
  133. ;
  134. // 1.2 Program Modules
  135. program_module :
  136. 'MODULE' module_identifier ( protection )? ';'
  137. import_lists module_block module_identifier '.'
  138. ;
  139. module_identifier :
  140. identifier
  141. ;
  142. protection :
  143. '[' protection_expression ']'
  144. ;
  145. protection_expression :
  146. constant_expression
  147. ;
  148. // 1.3 Definition Module
  149. definition_module :
  150. 'DEFINITION' 'MODULE' module_identifier ';'
  151. import_lists definitions 'END' module_identifier '.'
  152. ;
  153. // 1.4 Implementation Module
  154. implementation_module :
  155. 'IMPLEMENTATION' 'MODULE' module_identifier ( protection )? ';'
  156. import_lists module_block module_identifier '.'
  157. ;
  158. // 1.5 Import Lists
  159. import_lists :
  160. ( import_list )*
  161. ;
  162. // 1.5.1 Import List
  163. import_list :
  164. simple_import | unqualified_import
  165. ;
  166. // 1.5.2 Simple Import
  167. simple_import :
  168. 'IMPORT' identifier_list ';'
  169. ;
  170. // 1.5.3 Unqualified Import
  171. unqualified_import :
  172. 'FROM' module_identifier 'IMPORT' identifier_list ';'
  173. ;
  174. // 1.6 Export Lists
  175. export_list :
  176. unqualified_export | qualified_export
  177. ;
  178. // 1.6.1 Unqualified Export
  179. unqualified_export :
  180. 'EXPORT' 'QUALIFIED' identifier_list ';'
  181. ;
  182. // 1.6.2 Qualified Export
  183. qualified_export :
  184. 'EXPORT' 'QUALIFIED' identifier_list ';'
  185. ;
  186. // 2.1 Qualified Identifiers
  187. qualified_identifier :
  188. ( module_identifier '.' )* identifier
  189. ;
  190. // 2.2 Definitions
  191. definitions :
  192. ( definition )*
  193. ;
  194. definition :
  195. 'CONST' ( constant_declaration ';' )* |
  196. 'TYPE' ( type_declaration ';' )* |
  197. 'VAR' ( variable_declaration ';' )* |
  198. procedure_heading ';'
  199. ;
  200. procedure_heading :
  201. proper_procedure_heading | function_procedure_heading
  202. ;
  203. // 2.2.1 Type Definitions
  204. type_definition :
  205. type_declaration | opaque_type_definition
  206. ;
  207. opaque_type_definition :
  208. identifier
  209. ;
  210. // 2.2.2 Proper Procedure Heading
  211. proper_procedure_heading :
  212. 'PROCEDURE' procedure_identifier ( formal_parameters )? ';'
  213. ;
  214. formal_parameters :
  215. '(' ( formal_parameter_list )? ')'
  216. ;
  217. formal_parameter_list :
  218. formal_parameter ( ';' formal_parameter )*
  219. ;
  220. // 2.2.3 Function Procedure Heading
  221. function_procedure_heading :
  222. 'PROCEDURE' procedure_identifier ( formal_parameters )?
  223. ':' function_result_type ';'
  224. ;
  225. function_result_type :
  226. type_identifier;
  227. // 2.2.4 Formal Parameter
  228. formal_parameter :
  229. value_parameter_specification | variable_parameter_specification
  230. ;
  231. // 2.2.4.1 Value Parameter
  232. value_parameter_specification :
  233. identifier_list ':' formal_type
  234. ;
  235. // 2.2.4.2 Variable Parameter
  236. variable_parameter_specification :
  237. 'VAR' identifier_list ':' formal_type
  238. ;
  239. // 2.3 Declarations
  240. declarations :
  241. ( declaration )*
  242. ;
  243. declaration :
  244. 'CONST' ( constant_declaration ';' )* |
  245. 'TYPE' ( type_declaration ';' )* |
  246. 'VAR' ( variable_declaration ';' )* |
  247. procedure_declaration ';' |
  248. local_module_declaration ';'
  249. ;
  250. // 2.4 Constant Declaration
  251. constant_declaration :
  252. identifier '=' constant_expression
  253. ;
  254. // 2.5 Type Declaration
  255. type_declaration :
  256. identifier '=' type_denoter
  257. ;
  258. // 2.6 Variable Declaration
  259. variable_declaration :
  260. variable_identifier_list ':' type_denoter
  261. ;
  262. variable_identifier_list :
  263. identifier ( machine_address )? ( ',' identifier ( machine_address )? )*
  264. ;
  265. machine_address :
  266. '[' value_of_address_type ']'
  267. ;
  268. value_of_address_type :
  269. constant_expression
  270. ;
  271. // 2.7 Procedure Declaration
  272. procedure_declaration :
  273. proper_procedure_declaration | function_procedure_declaration
  274. ;
  275. // 2.8 Proper Procedure Declaration
  276. proper_procedure_declaration :
  277. proper_procedure_heading ';'
  278. ( proper_procedure_block procedure_identifier | 'FORWARD' )
  279. ;
  280. procedure_identifier :
  281. identifier
  282. ;
  283. // 2.8.1 Function Procedure Declaration
  284. function_procedure_declaration :
  285. function_procedure_heading ';'
  286. ( function_procedure_block procedure_identifier | 'FORWARD' )
  287. ;
  288. // 2.9 Local Module Declaration
  289. local_module_declaration :
  290. 'MODULE' module_identifier ( protection )? ';'
  291. import_lists ( export_list )? module_block module_identifier
  292. ;
  293. // 3 Types
  294. type_denoter :
  295. type_identifier | new_type
  296. ;
  297. ordinal_type_denoter :
  298. ordinal_type_identifier | new_ordinal_type
  299. ;
  300. // 3.1 Type Identifier
  301. type_identifier :
  302. qualified_identifier
  303. ;
  304. ordinal_type_identifier :
  305. type_identifier
  306. ;
  307. // 3.2 New Type
  308. new_type :
  309. new_ordinal_type | set_type | packedset_type | pointer_type |
  310. procedure_type | array_type | record_type
  311. ;
  312. new_ordinal_type :
  313. enumeration_type | subrange_type
  314. ;
  315. // 3.2.1 Enumeration Type
  316. enumeration_type :
  317. '(' identifier_list ')'
  318. ;
  319. identifier_list :
  320. identifier ( ',' identifier )*
  321. ;
  322. // 3.2.2 Subrange Type
  323. subrange_type :
  324. ( range_type )? '[' constant_expression '..' constant_expression ']'
  325. ;
  326. range_type :
  327. ordinal_type_identifier
  328. ;
  329. // 3.2.3 Set Type
  330. set_type :
  331. 'SET' 'OF' base_type
  332. ;
  333. base_type :
  334. ordinal_type_denoter
  335. ;
  336. // 3.2.4 Packedset Type
  337. packedset_type :
  338. 'PACKEDSET' 'OF' base_type
  339. ;
  340. // 3.2.5 Pointer Type
  341. pointer_type :
  342. 'POINTER' 'TO' bound_type
  343. ;
  344. bound_type :
  345. type_denoter
  346. ;
  347. // 3.2.6 Procedure Type
  348. procedure_type :
  349. proper_procedure_type | function_procedure_type
  350. ;
  351. fragment
  352. proper_procedure_type :
  353. 'PROCEDURE' '(' ( formal_parameter_type_list )? ')'
  354. ;
  355. function_procedure_type :
  356. 'PROCEDURE' '(' ( formal_parameter_type_list )? ')'
  357. ':' function_result_type
  358. ;
  359. formal_parameter_type_list :
  360. formal_parameter_type ( ',' formal_parameter_type )*
  361. ;
  362. formal_parameter_type :
  363. variable_formal_type | value_formal_type
  364. ;
  365. variable_formal_type :
  366. 'VAR' formal_type
  367. ;
  368. value_formal_type :
  369. formal_type
  370. ;
  371. // 3.2.7 Formal Type
  372. formal_type :
  373. type_identifier | open_array_formal_type
  374. ;
  375. open_array_formal_type :
  376. 'ARRAY' 'OF' ( 'ARRAY' 'OF' )* type_identifier
  377. ;
  378. // 3.2.8 Array Type
  379. array_type :
  380. 'ARRAY' index_type ( ',' index_type )* 'OF' component_type
  381. ;
  382. index_type :
  383. ordinal_type_denoter
  384. ;
  385. component_type :
  386. type_denoter
  387. ;
  388. // 3.2.9 Record Type
  389. record_type :
  390. 'RECORD' field_list 'END'
  391. ;
  392. field_list :
  393. fields ( ';' fields )*
  394. ;
  395. fields :
  396. ( fixed_fields | variant_fields )?
  397. ;
  398. fixed_fields :
  399. identifier_list ':' field_type
  400. ;
  401. field_type :
  402. type_denoter
  403. ;
  404. variant_fields :
  405. 'CASE' ( tag_identifier )? ':' tag_type 'OF' variant_list 'END'
  406. ;
  407. tag_identifier :
  408. identifier
  409. ;
  410. tag_type :
  411. ordinal_type_identifier
  412. ;
  413. variant_list :
  414. variant ( '|' variant )* ( variant_else_part )?
  415. ;
  416. variant_else_part :
  417. 'ELSE' field_list
  418. ;
  419. variant :
  420. ( variant_label_list ':' field_list )?
  421. ;
  422. variant_label_list :
  423. variant_label ( '.' variant_label )*
  424. ;
  425. variant_label :
  426. constant_expression ( '..' constant_expression )?
  427. ;
  428. // 4 Blocks
  429. // 4.1 Proper Procedure Block
  430. proper_procedure_block :
  431. declarations ( procedure_body )? 'END'
  432. ;
  433. procedure_body :
  434. 'BEGIN' block_body
  435. ;
  436. // 4.2 Function Procedure Block
  437. function_procedure_block :
  438. declarations function_body 'END'
  439. ;
  440. function_body :
  441. 'BEGIN' block_body
  442. ;
  443. // 4.3 Module Block
  444. module_block :
  445. declarations ( module_body )? 'END'
  446. ;
  447. module_body :
  448. initialization_body ( finalization_body )?
  449. ;
  450. initialization_body :
  451. 'BEGIN' block_body
  452. ;
  453. finalization_body :
  454. 'FINALLY' block_body
  455. ;
  456. // 4.4 Block Bodies and Exception Handling
  457. block_body :
  458. normal_part ( 'EXCEPT' exceptional_part )?
  459. ;
  460. normal_part :
  461. statement_sequence
  462. ;
  463. exceptional_part :
  464. statement_sequence
  465. ;
  466. // 5 Statements
  467. statement :
  468. empty_statement | assignment_statement | procedure_call |
  469. return_statement | retry_statement | with_statement |
  470. if_statement | case_statement | while_statement |
  471. repeat_statement | loop_statement | exit_statement |
  472. for_statement
  473. ;
  474. // 5.1 Statement Sequence
  475. statement_sequence :
  476. statement ( ';' statement )*
  477. ;
  478. // 5.2 Empty Statement
  479. empty_statement :
  480. ';'
  481. ;
  482. // 5.3 Assigmment Statement
  483. assignment_statement :
  484. variable_designator ':=' expression
  485. ;
  486. // 5.4 Procedure Call
  487. procedure_call :
  488. procedure_designator ( actual_parameters )?
  489. ;
  490. procedure_designator :
  491. value_designator
  492. ;
  493. // 5.5 Return Statement
  494. return_statement :
  495. simple_return_statement | function_return_statement
  496. ;
  497. // 5.5.1 Simple Return Statement
  498. simple_return_statement :
  499. 'RETURN'
  500. ;
  501. // 5.5.3 Function Return Statement
  502. function_return_statement :
  503. 'RETURN' expression
  504. ;
  505. // 5.6 Retry Statement
  506. retry_statement :
  507. 'RETRY'
  508. ;
  509. // 5.7 With Statement
  510. with_statement :
  511. 'WITH' record_designator 'DO' statement_sequence 'END'
  512. ;
  513. record_designator :
  514. variable_designator | value_designator
  515. ;
  516. // 5.8 If Statement
  517. if_statement :
  518. guarded_statements ( if_else_part )? 'END'
  519. ;
  520. guarded_statements :
  521. 'IF' boolean_expression 'THEN' statement_sequence
  522. ( 'ELSIF' boolean_expression 'THEN' statement_sequence )*
  523. ;
  524. if_else_part :
  525. 'ELSE' statement_sequence
  526. ;
  527. boolean_expression :
  528. expression
  529. ;
  530. // 5.9 Case Statement
  531. case_statement :
  532. 'CASE' case_selector 'OF' case_list 'END'
  533. ;
  534. case_selector :
  535. ordinal_expression
  536. ;
  537. case_list :
  538. case_alternative ( '|' case_alternative )*
  539. ;
  540. case_else_part :
  541. 'ELSE' statement_sequence
  542. ;
  543. // 5.9.1 Case Alternative
  544. case_alternative :
  545. ( case_label_list )? ':' statement_sequence
  546. ;
  547. case_label_list :
  548. case_label ( ',' case_label )*
  549. ;
  550. case_label :
  551. constant_expression ( '..' constant_expression )?
  552. ;
  553. // 5.10 While Statement
  554. while_statement :
  555. 'WHILE' boolean_expression 'DO' statement_sequence 'END'
  556. ;
  557. // 5.11 Repeat Statement
  558. repeat_statement :
  559. 'REPEAT' statement_sequence 'UNTIL' boolean_expression
  560. ;
  561. // 5.12 Loop Statement
  562. loop_statement :
  563. 'LOOP' statement_sequence 'END'
  564. ;
  565. // 5.13 Exit Statement
  566. exit_statement :
  567. 'EXIT'
  568. ;
  569. // 5.14 For Statement
  570. for_statement :
  571. 'FOR' control_variable_identifier ':=' initial_value 'TO' final_value
  572. ( 'BY' step_size )? 'DO' statement_sequence 'END'
  573. ;
  574. control_variable_identifier :
  575. identifier
  576. ;
  577. initial_value :
  578. ordinal_expression
  579. ;
  580. final_value :
  581. ordinal_expression
  582. ;
  583. step_size :
  584. constant_expression
  585. ;
  586. // 6 Variable Designator
  587. variable_designator :
  588. entire_designator | indexed_designator | selected_designator |
  589. dereferenced_designator
  590. ;
  591. // 6.1 Entire Designator
  592. entire_designator :
  593. qualified_identifier
  594. ;
  595. // 6.2 Indexed Designator
  596. indexed_designator :
  597. array_variable_designator '[' index_expression ( ',' index_expression )* ']'
  598. ;
  599. array_variable_designator :
  600. variable_designator
  601. ;
  602. index_expression :
  603. ordinal_expression
  604. ;
  605. // 6.3 Selected Designator
  606. selected_designator :
  607. record_variable_designator '.' field_identifier
  608. ;
  609. record_variable_designator :
  610. variable_designator;
  611. field_identifier :
  612. identifier
  613. ;
  614. // 6.4 Dereferenced Designator
  615. dereferenced_designator :
  616. pointer_variable_designator '^'
  617. ;
  618. pointer_variable_designator :
  619. variable_designator
  620. ;
  621. // 7 Expressions
  622. expression :
  623. simple_expression ( relational_operator simple_expression )?
  624. ;
  625. simple_expression :
  626. ( '+' | '-' )? term ( term_operator term )*
  627. ;
  628. term :
  629. factor ( factor_operator factor )*
  630. ;
  631. factor :
  632. '(' expression ')' |
  633. 'NOT' factor |
  634. value_designator |
  635. function_call |
  636. value_constructor |
  637. constant_literal
  638. ;
  639. ordinal_expression :
  640. expression
  641. ;
  642. // 7.1 Infix Expressions
  643. relational_operator :
  644. '=' | '#' | '<' | '<=' | '>' | '>=' | 'IN' |
  645. ;
  646. term_operator :
  647. '+' | '-' | 'OR'
  648. ;
  649. factor_operator :
  650. '*' | '/' | 'REM' | 'DIV' | 'MOD' | 'AND'
  651. ;
  652. // 7.2 Value Designator
  653. value_designator :
  654. entire_value | indexed_value | selected_value | dereferenced_value
  655. ;
  656. // 7.2.1 Entire Value
  657. entire_value :
  658. qualified_identifier
  659. ;
  660. // 7.2.2 Indexed Value
  661. indexed_value :
  662. array_value '[' index_expression ( ',' index_expression )* ']'
  663. ;
  664. array_value :
  665. value_designator
  666. ;
  667. // 7.2.3 Selected Value
  668. selected_value :
  669. record_value '.' field_identifier
  670. ;
  671. record_value :
  672. value_designator
  673. ;
  674. // 7.2.4 Dereferenced Value
  675. dereferenced_value :
  676. pointer_value '^'
  677. ;
  678. pointer_value :
  679. value_designator
  680. ;
  681. // 7.3 Function Call
  682. function_call :
  683. function_designator actual_parameters
  684. ;
  685. function_designator :
  686. value_designator
  687. ;
  688. // 7.4 Value Constructors
  689. value_constructor :
  690. array_constructor | record_constructor | set_constructor
  691. ;
  692. // 7.4.1 Array Constructor
  693. array_constructor :
  694. array_type_identifier array_constructed_value
  695. ;
  696. array_type_identifier :
  697. type_identifier;
  698. array_constructed_value :
  699. '{' repeated_structure_component ( ',' repeated_structure_component )* '}'
  700. ;
  701. repeated_structure_component :
  702. structure_component ( 'BY' repetition_factor )?
  703. ;
  704. repetition_factor :
  705. constant_expression
  706. ;
  707. structure_component :
  708. expression | array_constructed_value | record_constructed_value |
  709. set_constructed_value
  710. ;
  711. // 7.4.2 Record Constructor
  712. record_constructor :
  713. record_type_identifier record_constructed_value
  714. ;
  715. record_type_identifier :
  716. type_identifier
  717. ;
  718. record_constructed_value :
  719. '{' ( structure_component )? ( ',' structure_component )* '}'
  720. ;
  721. // 7.4.3 Set Constructor
  722. set_constructor :
  723. set_type_identifier set_constructed_value
  724. ;
  725. set_type_identifier :
  726. type_identifier
  727. ;
  728. set_constructed_value :
  729. '{' ( member ( ',' member )* )? '}'
  730. ;
  731. member :
  732. interval | singleton
  733. ;
  734. interval :
  735. ordinal_expression '..' ordinal_expression
  736. ;
  737. singleton :
  738. ordinal_expression
  739. ;
  740. // 7.5 Constant Literal
  741. constant_literal :
  742. whole_number_literal | real_literal | string_literal | pointer_literal
  743. ;
  744. // 7.6 Constant Expression
  745. constant_expression :
  746. expression
  747. ;
  748. // 8 Parameter and Argument Binding
  749. // 8.1 Actual Parameters
  750. actual_parameters :
  751. '(' ( actual_parameter_list )? ')'
  752. ;
  753. actual_parameter_list :
  754. actual_parameter ( ',' actual_parameter )*
  755. ;
  756. actual_parameter :
  757. variable_designator | expression | type_parameter
  758. ;
  759. // 8.2 Special Parameters
  760. // 8.2.1 Type Parameter
  761. type_parameter :
  762. type_identifier
  763. ;
  764. // mapping of lowercase names to terminals
  765. identifier :
  766. IDENTIFIER ;
  767. whole_number_literal :
  768. WHOLE_NUMBER_LITERAL
  769. ;
  770. real_literal :
  771. REAL_LITERAL
  772. ;
  773. string_literal :
  774. STRING_LITERAL
  775. ;
  776. pointer_literal :
  777. POINTER_LITERAL
  778. ;
  779. // ---------------------------------------------------------------------------
  780. // L E X E R G R A M M A R
  781. // ---------------------------------------------------------------------------
  782. IDENTIFIER :
  783. LETTER ( LETTER | DIGIT )*
  784. ;
  785. WHOLE_NUMBER_LITERAL :
  786. DIGIT ( DIGIT )* ( OCTAL_DIGIT ( OCTAL_DIGIT )* ( 'B' | 'C' ) )?
  787. | DIGIT ( HEX_DIGIT )* 'H'
  788. ;
  789. REAL_LITERAL :
  790. DIGIT ( DIGIT )* '.' ( DIGIT )* ( SCALE_FACTOR )?
  791. ;
  792. STRING_LITERAL :
  793. '\'' ( CHARACTER )* '\'' | '"' ( CHARACTER )* '"'
  794. ;
  795. POINTER_LITERAL :
  796. ;
  797. COMMENT :
  798. '(*' ( : . )* ( COMMENT )* '*)'
  799. ;
  800. fragment
  801. LETTER : '_' | 'A' .. 'Z' | 'a' .. 'z' ;
  802. fragment
  803. DIGIT : OCTAL_DIGIT | '8' | '9' ;
  804. fragment
  805. OCTAL_DIGIT : '0' .. '7' ;
  806. fragment
  807. HEX_DIGIT : DIGIT | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' ;
  808. fragment
  809. SCALE_FACTOR :
  810. 'E' ( '+' | '-' )? DIGIT ( DIGIT )*
  811. ;
  812. fragment
  813. CHARACTER :
  814. // any printable characters other than single and double quote
  815. ~( '\u0000' .. '\u001f' | '\'' | '"' )
  816. ;
  817. // END OF FILE