| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- (*
- GNU Modula-2 grammar converted from GCC's gm2-ebnf.texi
- for testing with the GNU Modula-2 version of Coco/R.
- IMPORTANT:
- The GCC EBNF deliberately leaves the lexical productions Ident,
- Integer, Real and string as built-ins. Therefore the lexical rules
- below are a TEST lexer, not a claim that they exactly reproduce every
- GNU Modula-2 lexical extension. The syntactic productions are a
- mechanical conversion of the supplied GCC EBNF.
- Source: GNU Modula-2 gm2-ebnf.texi (GCC, 2000-2026).
- *)
- COMPILER GM2
- CHARACTERS
- letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".
- octDigit = "01234567".
- digit = octDigit + '89'.
- hexDigit = digit + 'ABCDEF'.
- eol = CHR(13).
- noQuote1 = ANY - "'" - eol.
- noQuote2 = ANY - '"' - eol.
- IGNORE CHR(9) .. CHR(13)
- COMMENTS FROM "(*" TO "*)" NESTED
- TOKENS
- Ident = letter { letter | digit | "_" }.
- Integer = digit { digit }
- | digit { digit } CONTEXT ("..")
- | octDigit { octDigit } ("B" | "C")
- | digit { hexDigit } "H".
- (* A real literal requires digits after the decimal point. This is
- important for ranges such as 0..39: the scanner must produce
- INTEGER DOTDOT INTEGER, not REAL followed by a dot. *)
- Real = digit { digit } "." digit { digit }
- [ "E" [ "+" | "-" ] digit { digit } ].
- string = "'" { noQuote1 } "'"
- | '"' { noQuote2 } '"'.
- PRODUCTIONS
- GM2 = ( 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 | "~" 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 | "~" 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 }.
- END GM2.
|