* [1]Home * [2]News * [3]Development * [4]Community The GNU Modula-2 front end to GCC [5]Prev [6]Next * [7]Overview * [8]Using * [9]Obtaining * [10]Licence * [11]GNU Project * [12]Contributing * [13]Internals * [14]EBNF * [15]Libraries * [16]Indices EBNF of GNU Modula-2 This chapter contains the EBNF of GNU Modula-2. This grammer currently supports both PIM and ISO dialects. The rules here are automatically extracted from the grammer 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 | ConstActualParameter s ] =: ConstActualParameters := '(' [ ExpList ] ')' =: ConstAttribute := '__ATTRIBUTE__' '__BUILTIN__' '(' '(' ConstAttributeExpression ')' ')' =: ConstAttributeExpression := Ident | '<' Qualident ',' Ident '>' =: Element := ConstExpression [ '..' ConstExpression ] =: ByteAlignment := '<*' AttributeExpression '*>' =: OptAlignmentExpression := [ AlignmentExpression ] =: AlignmentExpression := '(' ConstExpression ')' =: 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 ] ) =: Builtin := [ '__BUILTIN__' | '__INLINE__' ] =: DefProcedureHeading := 'PROCEDURE' Builtin ( Ident [ DefFormalParameters ] ) =: 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 =: NonVarFPSection := IdentList ':' FormalType =: 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 } =: References 1. https://www.nongnu.org/gm2/homepage.html 2. https://www.nongnu.org/gm2/news.html 3. https://www.nongnu.org/gm2/development.html 4. https://www.nongnu.org/gm2/community.html 5. https://www.nongnu.org/gm2/internals.html 6. https://www.nongnu.org/gm2/libraries.html 7. https://www.nongnu.org/gm2/overview.html 8. https://www.nongnu.org/gm2/using.html 9. https://www.nongnu.org/gm2/obtaining.html 10. https://www.nongnu.org/gm2/licence.html 11. https://www.nongnu.org/gm2/gnu_project.html 12. https://www.nongnu.org/gm2/contributing.html 13. https://www.nongnu.org/gm2/internals.html 14. https://www.nongnu.org/gm2/ebnf.html 15. https://www.nongnu.org/gm2/libraries.html 16. https://www.nongnu.org/gm2/indices.html