| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- Next: PIM and ISO library definitions, Previous: Contributing to GNU Modula-2, Up: Introduction [Contents][Index]
- 3 EBNF of GNU Modula-2
- This chapter contains the EBNF of GNU Modula-2. This grammar currently supports both PIM and ISO dialects. The rules here are automatically extracted from the crammer files in GNU Modula-2 and serve to document the syntax of the extensions described earlier and how they fit in with the base language.
- Note that the first six productions are built into the lexical analysis phase.
- Ident := is a builtin and checks for an identifier
- =:
- Integer := is a builtin and checks for an integer
- =:
- Real := is a builtin and checks for an real constant
- =:
- string := is a builtin and checks for an string constant
- =:
- FileUnit := ( DefinitionModule |
- ImplementationOrProgramModule )
- =:
- ProgramModule := 'MODULE' Ident [ Priority ] ';' {
- Import } Block Ident '.'
- =:
- ImplementationModule := 'IMPLEMENTATION' 'MODULE' Ident
- [ Priority ] ';' { Import
- } Block
- Ident '.'
- =:
- ImplementationOrProgramModule := ImplementationModule |
- ProgramModule
- =:
- Number := Integer | Real
- =:
- Qualident := Ident { '.' Ident }
- =:
- ConstantDeclaration := Ident '=' ConstExpression
- =:
- ConstExpression := SimpleConstExpr [ Relation SimpleConstExpr ]
- =:
- Relation := '=' | '#' | '<>' | '<' | '<=' |
- '>' | '>=' | 'IN'
- =:
- SimpleConstExpr := UnaryOrConstTerm { AddOperator
- ConstTerm }
- =:
- UnaryOrConstTerm := '+' ConstTerm |
- '-' ConstTerm |
- ConstTerm
- =:
- AddOperator := '+' | '-' | 'OR'
- =:
- ConstTerm := ConstFactor { MulOperator ConstFactor }
- =:
- MulOperator := '*' | '/' | 'DIV' | 'MOD' |
- 'REM' | 'AND' | '&'
- =:
- ConstFactor := Number | ConstString |
- ConstSetOrQualidentOrFunction |
- '(' ConstExpression ')' |
- 'NOT' ConstFactor |
- ConstAttribute
- =:
- ConstString := string
- =:
- ComponentElement := ConstExpression [ '..' ConstExpression ]
- =:
- ComponentValue := ComponentElement [ 'BY' ConstExpression ]
- =:
- ArraySetRecordValue := ComponentValue { ',' ComponentValue }
- =:
- Constructor := '{' [ ArraySetRecordValue ] '}'
- =:
- ConstSetOrQualidentOrFunction := Constructor |
- Qualident [ Constructor |
- ConstActualParameters ]
- =:
- ConstActualParameters := '(' [ ExpList ] ')'
- =:
- ConstAttribute := '__ATTRIBUTE__' '__BUILTIN__' '('
- '(' ConstAttributeExpression ')'
- ')'
- =:
- ConstAttributeExpression := Ident | '<' Qualident
- ',' Ident '>'
- =:
- ByteAlignment := '<*' AttributeExpression '*>'
- =:
- Alignment := [ ByteAlignment ]
- =:
- TypeDeclaration := Ident '=' Type Alignment
- =:
- Type := SimpleType | ArrayType | RecordType |
- SetType | PointerType | ProcedureType
- =:
- SimpleType := Qualident [ SubrangeType ] |
- Enumeration | SubrangeType
- =:
- Enumeration := '(' IdentList ')'
- =:
- IdentList := Ident { ',' Ident }
- =:
- SubrangeType := '[' ConstExpression '..' ConstExpression
- ']'
- =:
- ArrayType := 'ARRAY' SimpleType { ',' SimpleType }
- 'OF' Type
- =:
- RecordType := 'RECORD' [ DefaultRecordAttributes ]
- FieldListSequence 'END'
- =:
- DefaultRecordAttributes := '<*' AttributeExpression
- '*>'
- =:
- RecordFieldPragma := [ '<*' FieldPragmaExpression {
- ',' FieldPragmaExpression } '*>' ]
- =:
- FieldPragmaExpression := Ident [ '(' ConstExpression
- ')' ]
- =:
- AttributeExpression := Ident '(' ConstExpression ')'
- =:
- FieldListSequence := FieldListStatement { ';' FieldListStatement }
- =:
- FieldListStatement := [ FieldList ]
- =:
- FieldList := IdentList ':' Type RecordFieldPragma |
- 'CASE' CaseTag 'OF' Varient { '|' Varient }
- [ 'ELSE' FieldListSequence ] 'END'
- =:
- TagIdent := [ Ident ]
- =:
- CaseTag := TagIdent [ ':' Qualident ]
- =:
- Varient := [ VarientCaseLabelList ':' FieldListSequence ]
- =:
- VarientCaseLabelList := VarientCaseLabels { ',' VarientCaseLabels }
- =:
- VarientCaseLabels := ConstExpression [ '..' ConstExpression ]
- =:
- CaseLabelList := CaseLabels { ',' CaseLabels }
- =:
- CaseLabels := ConstExpression [ '..' ConstExpression ]
- =:
- SetType := ( 'SET' | 'PACKEDSET' ) 'OF' SimpleType
- =:
- PointerType := 'POINTER' 'TO' Type
- =:
- ProcedureType := 'PROCEDURE' [ FormalTypeList ]
- =:
- FormalTypeList := '(' ( ')' FormalReturn |
- ProcedureParameters ')' FormalReturn )
- =:
- FormalReturn := [ ':' OptReturnType ]
- =:
- OptReturnType := '[' Qualident ']' |
- Qualident
- =:
- ProcedureParameters := ProcedureParameter { ',' ProcedureParameter }
- =:
- ProcedureParameter := '...' | 'VAR' FormalType |
- FormalType
- =:
- VarIdent := Ident [ '[' ConstExpression ']' ]
- =:
- VariableDeclaration := VarIdentList ':' Type Alignment
- =:
- VarIdentList := VarIdent { ',' VarIdent }
- =:
- Designator := Qualident { SubDesignator }
- =:
- SubDesignator := '.' Ident | '[' ExpList ']' |
- '^'
- =:
- ExpList := Expression { ',' Expression }
- =:
- Expression := SimpleExpression [ Relation SimpleExpression ]
- =:
- SimpleExpression := [ '+' | '-' ] Term { AddOperator
- Term }
- =:
- Term := Factor { MulOperator Factor }
- =:
- Factor := Number | string | SetOrDesignatorOrFunction |
- '(' Expression ')' |
- 'NOT' Factor | ConstAttribute
- =:
- SetOrDesignatorOrFunction := ( Qualident [ Constructor |
- SimpleDes
- [ ActualParameters ] ] |
- Constructor )
- =:
- SimpleDes := { '.' Ident | '[' ExpList ']' |
- '^' }
- =:
- ActualParameters := '(' [ ExpList ] ')'
- =:
- Statement := [ AssignmentOrProcedureCall |
- IfStatement | CaseStatement |
- WhileStatement | RepeatStatement |
- LoopStatement | ForStatement |
- WithStatement | AsmStatement |
- 'EXIT' | 'RETURN' [ Expression ] |
- RetryStatement ]
- =:
- RetryStatement := 'RETRY'
- =:
- AssignmentOrProcedureCall := Designator ( ':=' Expression |
- ActualParameters |
- )
- =:
- StatementSequence := Statement { ';' Statement }
- =:
- IfStatement := 'IF' Expression 'THEN' StatementSequence
- { 'ELSIF' Expression 'THEN' StatementSequence }
- [ 'ELSE' StatementSequence ] 'END'
- =:
- CaseStatement := 'CASE' Expression 'OF' Case { '|'
- Case }
- [ 'ELSE' StatementSequence ] 'END'
- =:
- Case := [ CaseLabelList ':' StatementSequence ]
- =:
- WhileStatement := 'WHILE' Expression 'DO' StatementSequence
- 'END'
- =:
- RepeatStatement := 'REPEAT' StatementSequence 'UNTIL'
- Expression
- =:
- ForStatement := 'FOR' Ident ':=' Expression 'TO' Expression
- [ 'BY' ConstExpression ] 'DO' StatementSequence
- 'END'
- =:
- LoopStatement := 'LOOP' StatementSequence 'END'
- =:
- WithStatement := 'WITH' Designator 'DO' StatementSequence
- 'END'
- =:
- ProcedureDeclaration := ProcedureHeading ';' ( ProcedureBlock
- Ident
- )
- =:
- DefineBuiltinProcedure := [ '__ATTRIBUTE__' '__BUILTIN__'
- '(' '(' Ident ')' ')' |
- '__INLINE__' ]
- =:
- ProcedureHeading := 'PROCEDURE' DefineBuiltinProcedure
- ( Ident [ FormalParameters ] AttributeNoReturn )
- =:
- AttributeNoReturn := [ '<*' Ident '*>' ]
- =:
- AttributeUnused := [ '<*' Ident '*>' ]
- =:
- Builtin := [ '__BUILTIN__' | '__INLINE__' ]
- =:
- DefProcedureHeading := 'PROCEDURE' Builtin ( Ident
- [ DefFormalParameters ]
- AttributeNoReturn )
-
- =:
- ProcedureBlock := { Declaration } [ 'BEGIN' BlockBody ]
- 'END'
- =:
- Block := { Declaration } InitialBlock FinalBlock
- 'END'
- =:
- InitialBlock := [ 'BEGIN' BlockBody ]
- =:
- FinalBlock := [ 'FINALLY' BlockBody ]
- =:
- BlockBody := NormalPart [ 'EXCEPT' ExceptionalPart ]
- =:
- NormalPart := StatementSequence
- =:
- ExceptionalPart := StatementSequence
- =:
- Declaration := 'CONST' { ConstantDeclaration ';' } |
- 'TYPE' { TypeDeclaration ';' } |
- 'VAR' { VariableDeclaration ';' } |
- ProcedureDeclaration ';' |
- ModuleDeclaration ';'
- =:
- DefFormalParameters := '(' [ DefMultiFPSection ] ')'
- FormalReturn
- =:
- DefMultiFPSection := DefExtendedFP |
- FPSection [ ';' DefMultiFPSection ]
- =:
- FormalParameters := '(' [ MultiFPSection ] ')' FormalReturn
- =:
- MultiFPSection := ExtendedFP | FPSection [ ';' MultiFPSection ]
- =:
- FPSection := NonVarFPSection | VarFPSection
- =:
- DefExtendedFP := DefOptArg | '...'
- =:
- ExtendedFP := OptArg | '...'
- =:
- VarFPSection := 'VAR' IdentList ':' FormalType [ AttributeUnused ]
- =:
- NonVarFPSection := IdentList ':' FormalType [ AttributeUnused ]
- =:
- OptArg := '[' Ident ':' FormalType [ '=' ConstExpression ]
- ']'
- =:
- DefOptArg := '[' Ident ':' FormalType '=' ConstExpression
- ']'
- =:
- FormalType := { 'ARRAY' 'OF' } Qualident
- =:
- ModuleDeclaration := 'MODULE' Ident [ Priority ] ';'
- { Import } [ Export ] Block
- Ident
- =:
- Priority := '[' ConstExpression ']'
- =:
- Export := 'EXPORT' ( 'QUALIFIED' IdentList |
- 'UNQUALIFIED' IdentList |
- IdentList ) ';'
- =:
- Import := 'FROM' Ident 'IMPORT' IdentList ';' |
- 'IMPORT' IdentList ';'
- =:
- DefinitionModule := 'DEFINITION' 'MODULE' [ 'FOR' string
- ] Ident
- ';' { Import } [ Export ] {
- Definition } 'END' Ident '.'
- =:
- Definition := 'CONST' { ConstantDeclaration ';' } |
- 'TYPE' { Ident ( ';' | '=' Type Alignment
- ';' ) } |
- 'VAR' { VariableDeclaration ';' } |
- DefProcedureHeading ';'
- =:
- AsmStatement := 'ASM' [ 'VOLATILE' ] '(' AsmOperands
- ')'
- =:
- NamedOperand := '[' Ident ']'
- =:
- AsmOperandName := [ NamedOperand ]
- =:
- AsmOperands := string [ ':' AsmList [ ':' AsmList [
- ':' TrashList ] ] ]
- =:
- AsmList := [ AsmElement ] { ',' AsmElement }
- =:
- AsmElement := AsmOperandName string '(' Expression
- ')'
- =:
- TrashList := [ string ] { ',' string }
- =:
- Next: PIM and ISO library definitions, Previous: Contributing to GNU Modula-2, Up: Introduction [Contents][Index]
|