| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644 |
- /* ANTLR v3 Grammar for Modula-2 (PIM3)
- *
- * Documentation version, derived from 3rd Edition of Programming in Modula-2
- *
- * This grammar follows the naming conventions and structure of the syntax
- * of Modula-2 as provided in the third edition of "Programming in Modula-2",
- * by N. Wirth. It is important to note that the syntax in Wirth's book does
- * not satisfy LL(1) constraints, and consequently ANTLR will not validate
- * the grammar in this form. An equivalent LL(1) version of this grammar is
- * also available in ANTLR v3 format as a separate file.
- *
- *
- * Copyright (C) 2009, Benjamin Kowarsch. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * (1) Hosting of this file, or any parts thereof, on websites which contain
- * advertising is expressly forbidden and requires specific prior written
- * permission. However, the ANTLR project website and university websites
- * are exempt from this restriction. Exemption may be withdrawn if abused.
- *
- * (2) Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * (3) Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and other materials provided with the distribution.
- *
- * (4) Neither the author's name nor the names of any contributors may be used
- * to endorse or promote products derived from this software without
- * specific prior written permission.
- *
- * (5) Where this list of conditions or the following disclaimer, in part or
- * as a whole is overruled or nullified by applicable law, no permission
- * is granted to use the software.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- */
- grammar m2pim3; // Modula-2 PIM3 standard
- // file version 1.01, May 19, 2009
- tokens {
-
- // Reserved Words
- AND = 'AND';
- ARRAY = 'ARRAY';
- BEGIN = 'BEGIN';
- BY = 'BY';
- CASE = 'CASE';
- CONST = 'CONST';
- DEFINITION = 'DEFINITION';
- DIV = 'DIV';
- DO = 'DO';
- ELSE = 'ELSE';
- ELSIF = 'ELSIF';
- END = 'END';
- EXIT = 'EXIT';
- EXPORT = 'EXPORT';
- FOR = 'FOR';
- FROM = 'FROM';
- IF = 'IF';
- IMPLEMENTATION = 'IMPLEMENTATION';
- IMPORT = 'IMPORT';
- IN = 'IN';
- LOOP = 'LOOP';
- MOD = 'MOD';
- MODULE = 'MODULE';
- NOT = 'NOT';
- OF = 'OF';
- OR = 'OR';
- POINTER = 'POINTER';
- PROCEDURE = 'PROCEDURE';
- QUALIFIED = 'QUALIFIED';
- RECORD = 'RECORD';
- REPEAT = 'REPEAT';
- RETURN = 'RETURN';
- SET = 'SET';
- THEN = 'THEN';
- TO = 'TO';
- TYPE = 'TYPE';
- UNTIL = 'UNTIL';
- VAR = 'VAR';
- WHILE = 'WHILE';
- WITH = 'WITH';
- // Operators
-
- ASSIGN_OP = ':=';
- LOGICAL_AND_OP = '&';
- LOGICAL_NOT_OP = '~';
- EQUAL_OP = '=';
- NOT_EQUAL_OP = '#';
- PASCAL_NOT_EQUAL_OP = '<>';
- GREATER_OP = '>';
- GREATER_OR_EQUAL_OP = '>=';
- LESS_OP = '<';
- LESS_OR_EQUAL_OP = '<=';
- PLUS_OP = '+';
- MINUS_OP = '-';
- MULTIPLY_OP = '*';
- DIVIDE_OP = '/';
- POINTER_OP = '^';
-
- // Punctuation
-
- DOT = '.';
- DOTDOT = '..';
- }
- // ---------------------------------------------------------------------------
- // P A R S E R G R A M M A R
- // ---------------------------------------------------------------------------
- // PIM 3 Appendix 1 line 1
- ident : IDENT ; // see lexer
- // PIM 3 Appendix 1 line 2
- number : INTEGER | REAL ; // see lexer
- // PIM 3 Appendix 1 lines 3-4
- integer : INTEGER ; // see lexer
- // PIM 3 Appendix 1 line 5
- real : REAL ; // see lexer
- // PIM 3 Appendix 1 line 6
- scaleFactor : SCALE_FACTOR ; // see lexer
- // PIM 3 Appendix 1 line 7
- hexDigit : HEX_DIGIT ; // see lexer
- // PIM 3 Appendix 1 line 8
- digit : DIGIT ; // see lexer
- // PIM 3 Appendix 1 line 9
- octalDigit : OCTAL_DIGIT ; // see lexer
- // PIM 3 Appendix 1 line 10
- string : STRING ; // see lexer
- // PIM 3 Appendix 1 line 11
- qualident :
- ident ( '.' ident )*
- ;
- // PIM 3 Appendix 1 line 12
- constantDeclaration :
- ident '=' constExpression
- ;
- // PIM 3 Appendix 1 line 13
- constExpression :
- simpleConstExpr ( relation simpleConstExpr )?
- ;
- // PIM 3 Appendix 1 line 14
- relation :
- '=' | '#' | '<>' | '<' | '<=' | '>' | '>=' | 'IN'
- ;
- // PIM 3 Appendix 1 line 15
- simpleConstExpr :
- ( '+' | '-' )? constTerm ( addOperator constTerm )*
- ;
- // PIM 3 Appendix 1 line 16
- addOperator :
- '+' | '-' | 'OR'
- ;
- // PIM 3 Appendix 1 line 17
- constTerm :
- constFactor ( mulOperator constFactor )*
- ;
- // PIM 3 Appendix 1 line 18
- mulOperator :
- '*' | '/' | 'DIV' | 'MOD' | 'AND' | '&'
- ;
- // PIM 3 Appendix 1 lines 19-20
- constFactor :
- qualident | number | string | constSet |
- '(' constExpression ')' | ( 'NOT' | '~' ) constFactor
- ;
- // PIM 3 Appendix 1 line 21
- constSet :
- ( qualident )? '{' ( constElement ( ',' constElement )* )? '}'
- ;
- // PIM 3 Appendix 1 line 22
- constElement :
- constExpression ( '..' constExpression )?
- ;
- // PIM 3 Appendix 1 line 23
- typeDeclaration :
- ident '=' type
- ;
- // PIM 3 Appendix 1 lines 24-25
- type :
- simpleType | arrayType | recordType | setType | pointerType | procedureType
- ;
- // PIM 3 Appendix 1 line 26
- simpleType :
- qualident | enumeration | subrangeType
- ;
- // PIM 3 Appendix 1 line 27
- enumeration :
- '(' identList ')'
- ;
- // PIM 3 Appendix 1 line 28
- identList :
- ident ( ',' ident )*
- ;
- // PIM 3 Appendix 1 line 29
- subrangeType :
- ( ident )? '[' constExpression '..' constExpression ']'
- ;
- // PIM 3 Appendix 1 line 30
- arrayType :
- 'ARRAY' simpleType ( ',' simpleType )* 'OF' type
- ;
- // PIM 3 Appendix 1 line 31
- recordType :
- 'RECORD' fieldListSequence 'END'
- ;
- // PIM 3 Appendix 1 line 32
- fieldListSequence :
- fieldList ( ';' fieldList )*
- ;
- // PIM 3 Appendix 1 lines 33-35
- fieldList :
- identList ':' type |
- 'CASE' ( ident )? ':' qualident 'OF' variant ( '|' variant )*
- ( 'ELSE' fieldListSequence )?
- 'END'
- ;
- // PIM 3 Appendix 1 line 36
- variant :
- ( caseLabelList ':' fieldListSequence )?
- ;
- // PIM 3 Appendix 1 line 37
- caseLabelList :
- caseLabels ( ',' caseLabels )*
- ;
- // PIM 3 Appendix 1 line 38
- caseLabels :
- constExpression ( '..' constExpression )?
- ;
- // PIM 3 Appendix 1 line 39
- setType :
- 'SET' 'OF' simpleType
- ;
- // PIM 3 Appendix 1 line 40
- pointerType :
- 'POINTER' 'TO' type
- ;
- // PIM 3 Appendix 1 line 41
- procedureType :
- 'PROCEDURE' ( formalTypeList )?
- ;
- // PIM 3 Appendix 1 lines 42-43
- formalTypeList :
- '(' ( ( 'VAR' )? formalType ( ',' ( 'VAR' )? formalType )* )? ')'
- ( ':' qualident )?
- ;
- // PIM 3 Appendix 1 line 44
- variableDeclaration :
- identList ':' type
- ;
- // PIM 3 Appendix 1 line 45
- designator :
- qualident ( '.' ident | '[' expList ']' | '^' )*
- ;
- // PIM 3 Appendix 1 line 46
- expList :
- expression ( ',' expression )*
- ;
- // PIM 3 Appendix 1 line 47
- expression :
- simpleExpression ( relation simpleExpression )?
- ;
- // PIM 3 Appendix 1 line 48
- simpleExpression :
- ( '+' | '-' )? term ( addOperator term )*
- ;
- // PIM 3 Appendix 1 line 49
- term :
- factor ( mulOperator factor )*
- ;
- // PIM 3 Appendix 1 lines 50-51
- factor :
- number | string | set | designator ( actualParameters )? |
- '(' expression ')' | 'NOT' factor
- ;
- // PIM 3 Appendix 1 line 52
- set :
- ( qualident )? '{' ( element ( ',' element )* )? '}'
- ;
- // PIM 3 Appendix 1 line 53
- element :
- expression ( '..' expression )?
- ;
- // PIM 3 Appendix 1 line 54
- actualParameters :
- '(' ( expList )? ')'
- ;
- // PIM 3 Appendix 1 lines 55-58
- statement :
- ( assignment | procedureCall | ifStatement | caseStatement |
- whileStatement | repeatStatement | loopStatement | forStatement |
- withStatement | 'EXIT' | 'RETURN' ( expression )? )?
- ;
- // PIM 3 Appendix 1 line 59
- assignment :
- designator ':=' expression
- ;
- // PIM 3 Appendix 1 line 60
- procedureCall :
- designator ( actualParameters )?
- ;
- // PIM 3 Appendix 1 line 61
- statementSequence :
- statement ( ';' statement )*
- ;
- // PIM 3 Appendix 1 lines 62-64
- ifStatement :
- 'IF' expression 'THEN' statementSequence
- ( 'ELSIF' expression 'THEN' statementSequence )*
- ( 'ELSE' statementSequence )?
- 'END'
- ;
- // PIM 3 Appendix 1 lines 65-66
- caseStatement :
- 'CASE' expression 'OF' case ( '|' case )*
- ( 'ELSE' statementSequence )?
- 'END'
- ;
- // PIM 3 Appendix 1 line 67
- case :
- ( caseLabelList ':' statementSequence )?
- ;
- // PIM 3 Appendix 1 line 68
- whileStatement :
- 'WHILE' expression 'DO' statementSequence 'END'
- ;
- // PIM 3 Appendix 1 line 69
- repeatStatement :
- 'REPEAT' statementSequence 'UNTIL' expression
- ;
- // PIM 3 Appendix 1 lines 70-71
- forStatement :
- 'FOR' ident ':=' expression 'TO' expression ( 'BY' constExpression )?
- 'DO' statementSequence 'END'
- ;
- // PIM 3 Appendix 1 line 72
- loopStatement :
- 'LOOP' statementSequence 'END'
- ;
- // PIM 3 Appendix 1 line 73
- withStatement :
- 'WITH' designator 'DO' statementSequence 'END'
- ;
- // PIM 3 Appendix 1 line 74
- procedureDeclaration :
- procedureHeading ';' block ident
- ;
- // PIM 3 Appendix 1 line 75
- procedureHeading :
- 'PROCEDURE' ident ( formalParameters )?
- ;
- // PIM 3 Appendix 1 line 76
- block :
- ( declaration )*
- ( 'BEGIN' statementSequence )? 'END'
- ;
- // PIM 3 Appendix 1 lines 78-80
- declaration :
- 'CONST' ( constantDeclaration ';' )* |
- 'TYPE' ( typeDeclaration ';' )* |
- 'VAR' ( variableDeclaration ';' )* |
- procedureDeclaration ';' |
- moduleDeclaration ';'
- ;
- // PIM 3 Appendix 1 lines 81-82
- formalParameters :
- '(' ( fpSection ( ';' fpSection )* )? ')' ( ':' qualident )?
- ;
- // PIM 3 Appendix 1 line 83
- fpSection :
- ( 'VAR' )? identList ':' formalType
- ;
- // PIM 3 Appendix 1 line 84
- formalType :
- ( 'ARRAY' 'OF' )? qualident
- ;
- // PIM 3 Appendix 1 lines 85-86
- moduleDeclaration :
- 'MODULE' ident ( priority )? ';'
- ( importList )* ( exportList )?
- block ident
- ;
- // PIM 3 Appendix 1 line87
- priority :
- '[' constExpression ']'
- ;
- // PIM 3 Appendix 1 line 88
- exportList :
- 'EXPORT' ( 'QUALIFIED' )? identList ';'
- ;
- // PIM 3 Appendix 1 line 89
- importList :
- ( 'FROM' ident )? 'IMPORT' identList ';'
- ;
- // PIM 3 Appendix 1 lines 90-91
- definitionModule :
- ( importList )*
- ( definition )*
- 'END' ident '.'
- ;
- // PIM 3 Appendix 1 lines 92-95
- definition :
- 'CONST' ( constantDeclaration ';' )* |
- 'TYPE' ( typeDeclaration ';' )* |
- 'VAR' ( variableDeclaration ';')* |
- procedureHeading ';'
- ;
- // PIM 3 Appendix 1 lines 96-97
- programModule :
- 'MODULE' ident ( priority )? ';'
- ( importList )* block ident '.'
- ;
- // PIM 3 Appendix 1 lines 98-99
- compilationUnit :
- definitionModule | ( 'IMPLEMENTATION' )? programModule
- ;
- // ---------------------------------------------------------------------------
- // L E X E R G R A M M A R
- // ---------------------------------------------------------------------------
- // PIM 3 Appendix 1 line 1
- IDENT : LETTER ( LETTER | DIGIT )* ;
- // PIM 3 Appendix 1 lines 3-4
- INTEGER :
- DIGIT ( DIGIT )* |
- OCTAL_DIGIT ( OCTAL_DIGIT )* ( 'B' | 'C' ) |
- DIGIT ( HEX_DIGIT )* 'H'
- ;
- // PIM 3 Appendix 1 line 5
- REAL :
- DIGIT ( DIGIT )* '.' ( DIGIT )* ( SCALE_FACTOR )?
- ;
- // PIM 3 Appendix 1 line 10
- STRING :
- '\'' ( CHARACTER )* '\'' | '"' ( CHARACTER )* '"'
- ;
- // letter remains undefined in PIM 3
- fragment
- LETTER : 'A' .. 'Z' | 'a' .. 'z' ;
- // PIM 3 Appendix 1 line 11
- fragment
- DIGIT : OCTAL_DIGIT | '8' | '9' ;
- // PIM 3 Appendix 1 line 9
- fragment
- OCTAL_DIGIT : '0' .. '7' ;
- // PIM 3 Appendix 1 line 7
- fragment
- HEX_DIGIT : DIGIT | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' ;
- // PIM 3 Appendix 1 line 6
- fragment
- SCALE_FACTOR :
- 'E' ( '+' | '-' )? DIGIT ( DIGIT )*
- ;
- // character remains undefined in PIM 3
- fragment
- CHARACTER :
- // any printable characters other than single and double quote
- ' ' | '!' | '#' .. '&' | '(' .. '~'
- ;
- // END OF FILE
|