diff --git a/ITerminals.cs b/ITerminals.cs index 5227bec..8fbc95a 100644 --- a/ITerminals.cs +++ b/ITerminals.cs @@ -60,7 +60,7 @@ public enum Token { AND, ARGLIST, ARRAY, BEGIN, BY, CASE, CONST, DEFINITION, DIV, DO, ELSE, ELSIF, END, EXIT, EXPORT, FOR, FROM, IF, IMPLEMENTATION, IMPORT, IN, LOOP, - MOD, MODULE, NOT, OF, OR, POINTER, PROCEDURE, QUALIFIED, RECORD, REPEAT, + MOD, MODULE, NOT, OF, OPAQUE, OR, POINTER, PROCEDURE, QUALIFIED, RECORD, REPEAT, RETURN, SET, THEN, TO, TYPE, UNTIL, VAR, WHILE, WITH, /* Identifiers */ diff --git a/NonTerminals.cs b/NonTerminals.cs new file mode 100644 index 0000000..3620d28 --- /dev/null +++ b/NonTerminals.cs @@ -0,0 +1,461 @@ +/* M2Sharp -- Modula-2 to C# Translator & Compiler + * + * Copyright (c) 2016 The Modula-2 Software Foundation + * + * Author & Maintainer: Benjamin Kowarsch + * + * @synopsis + * + * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. + * It supports the dialects described in the 3rd and 4th editions of Niklaus + * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, + * and an extended mode with select features from the revised language by + * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). + * + * In translator mode, M2Sharp translates Modula-2 source to C# source files. + * In compiler mode, M2Sharp compiles Modula-2 source via C# source files + * to object code or executables using the host system's C# compiler. + * + * @repository + * + * https://github.com/m2sf/m2sharp + * + * @file + * + * NonTerminals.cs + * + * provides FIRST() and FOLLOW() sets for each non-terminal symbol //SAM CHANGE + * used by the parser class for syntax analysis + * + * @license + * + * M2Sharp is free software: you can redistribute and/or modify it under the + * terms of the GNU Lesser General Public License (LGPL) either version 2.1 + * or at your choice version 3 as published by the Free Software Foundation. + * However, you may not alter the copyright, author and license information. + * + * M2Sharp is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with M2Sharp. If not, see . + * + * NB: Components in the domain part of email addresses are in reverse order. + */ + +namespace org.m2sf.m2sharp { + + using System; + using System.Collections.Generic; + +public class NonTerminals : INonTerminals { + + public static Production firstConstParamDependent = Production.FormalType, + lastConstParamDependent = Production.AttribFormalParams, + firstNoVariantRecDependent = Production.TypeDeclarationTail, + lastNoVariantRecDependent = Production.TypeDeclarationTail, + firstOptionDependent = firstConstParamDependent, + lastOptionDependent = lastNoVariantRecDependent; + public static uint alternateSetOffset = (uint)lastOptionDependent - (uint)firstOptionDependent + 1; + + #region followSets + TokenSet[] followSets = { + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000400, /* counter: */ 1 ), /* definitionModule */ + + new TokenSet( /* bits: */ 0x108050C8, 0x00000050, 0x00000000, /* counter: */ 9 ), /* import */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* qualifiedImport */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* unqualifiedImport */ + + new TokenSet( /* bits: */ 0x00000000, 0x80000000, 0x00000021, /* counter: */ 3 ), /* identList */ + + new TokenSet( /* bits: */ 0x00001000, 0x00000000, 0x00000000, /* counter: */ 1 ), /* definition */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* constDefinition */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* typeDefinition */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* type */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* derivedOrSubrangeType */ + + new TokenSet( /* bits: */ 0x06501F12, 0x3FFC002C, 0x0000037B, /* counter: */ 34 ), /* qualident */ + + new TokenSet( /* bits: */ 0x02000000, 0x20000000, 0x00000001, /* counter: */ 3 ), /* range */ + + new TokenSet( /* bits: */ 0x02000000, 0x20000000, 0x00000001, /* counter: */ 3 ), /* enumType */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* setType */ + + new TokenSet( /* bits: */ 0x02000000, 0x20000000, 0x00000001, /* counter: */ 3 ), /* countableType */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* arrayType */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* extensibleRecordType */ + + new TokenSet( /* bits: */ 0x00001000, 0x00000040, 0x00000000, /* counter: */ 2 ), /* fieldListSequence */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* variantRecordType */ + + new TokenSet( /* bits: */ 0x00001400, 0x00000000, 0x00000008, /* counter: */ 3 ), /* variantFieldListSeq */ + + new TokenSet( /* bits: */ 0x00001400, 0x00000000, 0x00000009, /* counter: */ 4 ), /* variantFieldList */ + + new TokenSet( /* bits: */ 0x00001400, 0x00000000, 0x00000009, /* counter: */ 4 ), /* variantFields */ + + new TokenSet( /* bits: */ 0x00001400, 0x00000000, 0x00000008, /* counter: */ 3 ), /* variant */ + + new TokenSet( /* bits: */ 0x00000000, 0x80000000, 0x00000000, /* counter: */ 1 ), /* caseLabelList */ + + new TokenSet( /* bits: */ 0x00000000, 0xA0000000, 0x00000000, /* counter: */ 2 ), /* caseLabels */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* pointerType */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* procedureType */ + + new TokenSet( /* bits: */ 0x00000000, 0x20000000, 0x00000020, /* counter: */ 2 ), /* simpleFormalType */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* procedureHeader */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* procedureSignature */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000021, /* counter: */ 2 ), /* simpleFormalParams */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000400, /* counter: */ 1 ), /* implementationModule */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000400, /* counter: */ 1 ), /* programModule */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* modulePriority */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000200, 0x00000000, /* counter: */ 1 ), /* block */ + + new TokenSet( /* bits: */ 0x00001008, 0x00000000, 0x00000000, /* counter: */ 2 ), /* declaration */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* typeDeclaration */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* varSizeRecordType */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* variableDeclaration */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* procedureDeclaration */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* moduleDeclaration */ + + new TokenSet( /* bits: */ 0x10801048, 0x00000050, 0x00000000, /* counter: */ 7 ), /* export */ + + new TokenSet( /* bits: */ 0x00001C00, 0x00000020, 0x00000008, /* counter: */ 5 ), /* statementSequence */ + + new TokenSet( /* bits: */ 0x00001C00, 0x00000020, 0x00000009, /* counter: */ 6 ), /* statement */ + + new TokenSet( /* bits: */ 0x00001C00, 0x00000020, 0x00000009, /* counter: */ 6 ), /* assignmentOrProcCall */ + + new TokenSet( /* bits: */ 0x00001C00, 0x00000020, 0x00000009, /* counter: */ 6 ), /* actualParameters */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000020, /* counter: */ 1 ), /* expressionList */ + + new TokenSet( /* bits: */ 0x00001C00, 0x00000020, 0x00000009, /* counter: */ 6 ), /* returnStatement */ + + new TokenSet( /* bits: */ 0x00001C00, 0x00000020, 0x00000009, /* counter: */ 6 ), /* withStatement */ + + new TokenSet( /* bits: */ 0x00001C00, 0x00000020, 0x00000009, /* counter: */ 6 ), /* ifStatement */ + + new TokenSet( /* bits: */ 0x00001C00, 0x00000020, 0x00000009, /* counter: */ 6 ), /* caseStatement */ + + new TokenSet( /* bits: */ 0x00001400, 0x00000000, 0x00000008, /* counter: */ 3 ), /* case */ + + new TokenSet( /* bits: */ 0x00001C00, 0x00000020, 0x00000009, /* counter: */ 6 ), /* loopStatement */ + + new TokenSet( /* bits: */ 0x00001C00, 0x00000020, 0x00000009, /* counter: */ 6 ), /* whileStatement */ + + new TokenSet( /* bits: */ 0x00001C00, 0x00000020, 0x00000009, /* counter: */ 6 ), /* repeatStatement */ + + new TokenSet( /* bits: */ 0x00001C00, 0x00000020, 0x00000009, /* counter: */ 6 ), /* forStatement */ + + new TokenSet( /* bits: */ 0x06501F12, 0x3FFC002C, 0x0000033B, /* counter: */ 33 ), /* designator */ + + new TokenSet( /* bits: */ 0x06501F12, 0x3FFC022C, 0x0000033B, /* counter: */ 34 ), /* selector */ + + new TokenSet( /* bits: */ 0x02001E10, 0x2000002C, 0x0000022B, /* counter: */ 15 ), /* expression */ + + new TokenSet( /* bits: */ 0x02101E10, 0x23F0002C, 0x0000022B, /* counter: */ 22 ), /* simpleExpression */ + + new TokenSet( /* bits: */ 0x06101E10, 0x23FC002C, 0x0000022B, /* counter: */ 25 ), /* term */ + + new TokenSet( /* bits: */ 0x06501F12, 0x2FFC002C, 0x0000022B, /* counter: */ 30 ), /* simpleTerm */ + + new TokenSet( /* bits: */ 0x06501F12, 0x2FFC002C, 0x0000022B, /* counter: */ 30 ), /* factor */ + + new TokenSet( /* bits: */ 0x06501F12, 0x2FFC002C, 0x0000022B, /* counter: */ 30 ), /* designatorOrFuncCall */ + + new TokenSet( /* bits: */ 0x06501F12, 0x2FFC002C, 0x0000022B, /* counter: */ 30 ), /* setValue */ + + new TokenSet( /* bits: */ 0x00000000, 0x20000000, 0x00000200, /* counter: */ 2 ), /* element */ + + new TokenSet( /* bits: */ 0x00000000, 0x20000000, 0x00000020, /* counter: */ 2 ), /* formalType */ + + new TokenSet( /* bits: */ 0x00000000, 0x20000000, 0x00000020, /* counter: */ 2 ), /* attributedFormalType */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000020, /* counter: */ 1 ), /* formalParamList */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000021, /* counter: */ 2 ), /* formalParams */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000021, /* counter: */ 2 ), /* attribFormalParams */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000001, /* counter: */ 1 ), /* typeDeclarationTail */ + }; + + #endregion + + #region firstSets + TokenSet[] firstSets = { + new TokenSet( /* bits: */ 0x00000080, 0x00000000, 0x00000000, /* counter: */ 1 ), /* definitionModule */ + + new TokenSet( /* bits: */ 0x00090000, 0x00000000, 0x00000000, /* counter: */ 2 ), /* import */ + + new TokenSet( /* bits: */ 0x00080000, 0x00000000, 0x00000000, /* counter: */ 1 ), /* qualifiedImport */ + + new TokenSet( /* bits: */ 0x00010000, 0x00000000, 0x00000000, /* counter: */ 1 ), /* unqualifiedImport */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000200, 0x00000000, /* counter: */ 1 ), /* identList */ + + new TokenSet( /* bits: */ 0x10000040, 0x00000050, 0x00000000, /* counter: */ 4 ), /* definition */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000200, 0x00000000, /* counter: */ 1 ), /* constDefinition */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000200, 0x00000000, /* counter: */ 1 ), /* typeDefinition */ + + new TokenSet( /* bits: */ 0x58000004, 0x00000202, 0x00000050, /* counter: */ 8 ), /* type */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000200, 0x00000040, /* counter: */ 2 ), /* derivedOrSubrangeType */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000200, 0x00000000, /* counter: */ 1 ), /* qualident */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000040, /* counter: */ 1 ), /* range */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000010, /* counter: */ 1 ), /* enumType */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000002, 0x00000000, /* counter: */ 1 ), /* setType */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000200, 0x00000050, /* counter: */ 3 ), /* countableType */ + + new TokenSet( /* bits: */ 0x00000004, 0x00000000, 0x00000000, /* counter: */ 1 ), /* arrayType */ + + new TokenSet( /* bits: */ 0x40000000, 0x00000000, 0x00000000, /* counter: */ 1 ), /* extensibleRecordType */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000200, 0x00000000, /* counter: */ 1 ), /* fieldListSequence */ + + new TokenSet( /* bits: */ 0x40000000, 0x00000000, 0x00000000, /* counter: */ 1 ), /* variantRecordType */ + + new TokenSet( /* bits: */ 0x00000020, 0x00000200, 0x00000000, /* counter: */ 2 ), /* variantFieldListSeq */ + + new TokenSet( /* bits: */ 0x00000020, 0x00000200, 0x00000000, /* counter: */ 2 ), /* variantFieldList */ + + new TokenSet( /* bits: */ 0x00000020, 0x00000000, 0x00000000, /* counter: */ 1 ), /* variantFields */ + + new TokenSet( /* bits: */ 0x01000000, 0x000C3E00, 0x00000110, /* counter: */ 10 ), /* variant */ + + new TokenSet( /* bits: */ 0x01000000, 0x000C3E00, 0x00000110, /* counter: */ 10 ), /* caseLabelList */ + + new TokenSet( /* bits: */ 0x01000000, 0x000C3E00, 0x00000110, /* counter: */ 10 ), /* caseLabels */ + + new TokenSet( /* bits: */ 0x08000000, 0x00000000, 0x00000000, /* counter: */ 1 ), /* pointerType */ + + new TokenSet( /* bits: */ 0x10000000, 0x00000000, 0x00000000, /* counter: */ 1 ), /* procedureType */ + + new TokenSet( /* bits: */ 0x00000004, 0x00000200, 0x00000000, /* counter: */ 2 ), /* simpleFormalType */ + + new TokenSet( /* bits: */ 0x10000000, 0x00000000, 0x00000000, /* counter: */ 1 ), /* procedureHeader */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000200, 0x00000000, /* counter: */ 1 ), /* procedureSignature */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000200, 0x00000000, /* counter: */ 1 ), /* simpleFormalParams */ + + new TokenSet( /* bits: */ 0x00040000, 0x00000000, 0x00000000, /* counter: */ 1 ), /* implementationModule */ + + new TokenSet( /* bits: */ 0x00800000, 0x00000000, 0x00000000, /* counter: */ 1 ), /* programModule */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000040, /* counter: */ 1 ), /* modulePriority */ + + new TokenSet( /* bits: */ 0x10801048, 0x00000050, 0x00000000, /* counter: */ 7 ), /* block */ + + new TokenSet( /* bits: */ 0x10800040, 0x00000050, 0x00000000, /* counter: */ 5 ), /* declaration */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000200, 0x00000000, /* counter: */ 1 ), /* typeDeclaration */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000040, 0x00000000, /* counter: */ 1 ), /* varSizeRecordType */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000200, 0x00000000, /* counter: */ 1 ), /* variableDeclaration */ + + new TokenSet( /* bits: */ 0x10000000, 0x00000000, 0x00000000, /* counter: */ 1 ), /* procedureDeclaration */ + + new TokenSet( /* bits: */ 0x00800000, 0x00000000, 0x00000000, /* counter: */ 1 ), /* moduleDeclaration */ + + new TokenSet( /* bits: */ 0x00004000, 0x00000000, 0x00000000, /* counter: */ 1 ), /* export */ + + new TokenSet( /* bits: */ 0x8022A020, 0x00000381, 0x00000000, /* counter: */ 10 ), /* statementSequence */ + + new TokenSet( /* bits: */ 0x8022A020, 0x00000381, 0x00000000, /* counter: */ 10 ), /* statement */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000200, 0x00000000, /* counter: */ 1 ), /* assignmentOrProcCall */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000010, /* counter: */ 1 ), /* actualParameters */ + + new TokenSet( /* bits: */ 0x01000000, 0x000C3E00, 0x00000110, /* counter: */ 10 ), /* expressionList */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000001, 0x00000000, /* counter: */ 1 ), /* returnStatement */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000100, 0x00000000, /* counter: */ 1 ), /* withStatement */ + + new TokenSet( /* bits: */ 0x00020000, 0x00000000, 0x00000000, /* counter: */ 1 ), /* ifStatement */ + + new TokenSet( /* bits: */ 0x00000020, 0x00000000, 0x00000000, /* counter: */ 1 ), /* caseStatement */ + + new TokenSet( /* bits: */ 0x01000000, 0x000C3E00, 0x00000110, /* counter: */ 10 ), /* case */ + + new TokenSet( /* bits: */ 0x00200000, 0x00000000, 0x00000000, /* counter: */ 1 ), /* loopStatement */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000080, 0x00000000, /* counter: */ 1 ), /* whileStatement */ + + new TokenSet( /* bits: */ 0x80000000, 0x00000000, 0x00000000, /* counter: */ 1 ), /* repeatStatement */ + + new TokenSet( /* bits: */ 0x00008000, 0x00000000, 0x00000000, /* counter: */ 1 ), /* forStatement */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000200, 0x00000000, /* counter: */ 1 ), /* designator */ + + new TokenSet( /* bits: */ 0x00000000, 0x40000000, 0x00000044, /* counter: */ 3 ), /* selector */ + + new TokenSet( /* bits: */ 0x01000000, 0x000C3E00, 0x00000110, /* counter: */ 10 ), /* expression */ + + new TokenSet( /* bits: */ 0x01000000, 0x000C3E00, 0x00000110, /* counter: */ 10 ), /* simpleExpression */ + + new TokenSet( /* bits: */ 0x01000000, 0x00003E00, 0x00000110, /* counter: */ 8 ), /* term */ + + new TokenSet( /* bits: */ 0x01000000, 0x00003E00, 0x00000110, /* counter: */ 8 ), /* simpleTerm */ + + new TokenSet( /* bits: */ 0x00000000, 0x00003E00, 0x00000110, /* counter: */ 7 ), /* factor */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000200, 0x00000000, /* counter: */ 1 ), /* designatorOrFuncCall */ + + new TokenSet( /* bits: */ 0x00000000, 0x00000000, 0x00000100, /* counter: */ 1 ), /* setValue */ + + new TokenSet( /* bits: */ 0x01000000, 0x000C3E00, 0x00000110, /* counter: */ 10 ), /* element */ + + new TokenSet( /* bits: */ 0x00000044, 0x00000240, 0x00000000, /* counter: */ 4 ), /* formalType */ + + new TokenSet( /* bits: */ 0x00000040, 0x00000040, 0x00000000, /* counter: */ 2 ), /* attributedFormalType */ + + new TokenSet( /* bits: */ 0x00000040, 0x00000240, 0x00000000, /* counter: */ 3 ), /* formalParamList */ + + new TokenSet( /* bits: */ 0x00000040, 0x00000240, 0x00000000, /* counter: */ 3 ), /* formalParams */ + + new TokenSet( /* bits: */ 0x00000040, 0x00000040, 0x00000000, /* counter: */ 2 ), /* attribFormalParams */ + + new TokenSet( /* bits: */ 0x58000004, 0x00000242, 0x00000050, /* counter: */ 9 ), /* typeDeclarationTail */ + }; + #endregion + + /* -------------------------------------------------------------------------- + * method Count() -- Returns the number of productions + * ----------------------------------------------------------------------- */ //SAM CHANGE + + static uint Count() { + return (uint)Enum.GetNames(typeof(Production)).Length; + } /* end Count */ + + + /* -------------------------------------------------------------------------- + * method IsOptionDependent(p) + * -------------------------------------------------------------------------- + * Returns true if p is dependent on any compiler option, else false. + * ----------------------------------------------------------------------- */ //SAM CHANGE + + bool IsOptionDependent(Production p) { + return p >= firstOptionDependent; + } /* end IsOptionDependent */ + + + /* -------------------------------------------------------------------------- + * method IsConstParamDependent(p) + * -------------------------------------------------------------------------- + * Returns true if p is dependent on CONST parameter option, else false. + * ----------------------------------------------------------------------- */ //SAM CHANGE + + bool IsConstParamDependent(Production p) + { + return p >= firstConstParamDependent && p <= lastConstParamDependent; + } /* end IsConstParamDependent */ + + + /* -------------------------------------------------------------------------- + * method IsVariantRecordDependent(p) + * -------------------------------------------------------------------------- + * Returns true if p is dependent on variant record type option, else false. + * ----------------------------------------------------------------------- */ //SAM CHANGE + + bool IsVariantRecordDependent(Production p) { + return p >= firstNoVariantRecDependent && p <= lastNoVariantRecDependent; + } + + /* -------------------------------------------------------------------------- + * method FIRST(p) + * -------------------------------------------------------------------------- + * Returns a tokenset with the FIRST set of production p. + * ----------------------------------------------------------------------- */ + + TokenSet FIRST(Production p) { + TokenSet tokenset = null; + uint index = 0; + + if (IsConstParamDependent(p)) + { + index = Convert.ToUInt32(p) + alternateSetOffset; + } /* end if */ + if (IsVariantRecordDependent(p) && CompilerOptions.VariantRecords()) + { + index = Convert.ToUInt32(p) + alternateSetOffset; + } /* end if */ + + tokenset = firstSets[index]; + + return tokenset; + } /* end FIRST */ + + + /* -------------------------------------------------------------------------- + * method FOLLOW(p) + * -------------------------------------------------------------------------- + * Returns a tokenset with the FOLLOW set of production p. + * ----------------------------------------------------------------------- */ + + TokenSet FOLLOW(Production p) { + TokenSet tokenset = null; + uint index = 0; + + if (IsConstParamDependent(p)) { + index = Convert.ToUInt32(p) + alternateSetOffset; + } /* end if */ + if (IsVariantRecordDependent(p) && !CompilerOptions.VariantRecords()) { + index = Convert.ToUInt32(p) + alternateSetOffset; + } /* end if */ + + tokenset = followSets[index]; + + return tokenset; + } /* end FOLLOW */ + + + /* -------------------------------------------------------------------------- + * method NameForProduction(p) + * -------------------------------------------------------------------------- + * Returns a string with a human readable name for production p. + * ----------------------------------------------------------------------- */ + + string NameForProduction(Production p); + + + +} /* NonTerminals */ + +} /* namespace */ \ No newline at end of file diff --git a/Terminals.cs b/Terminals.cs new file mode 100644 index 0000000..70e015f --- /dev/null +++ b/Terminals.cs @@ -0,0 +1,530 @@ +/* M2Sharp -- Modula-2 to C# Translator & Compiler + * + * Copyright (c) 2016 The Modula-2 Software Foundation + * + * Author & Maintainer: Benjamin Kowarsch + * + * @synopsis + * + * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. + * It supports the dialects described in the 3rd and 4th editions of Niklaus + * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, + * and an extended mode with select features from the revised language by + * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). + * + * In translator mode, M2Sharp translates Modula-2 source to C# source files. + * In compiler mode, M2Sharp compiles Modula-2 source via C# source files + * to object code or executables using the host system's C# compiler. + * + * @repository + * + * https://github.com/m2sf/m2sharp + * + * @file + * + * Terminals.cs + * + * Terminal symbols' token and lexeme lookup. + * + * @license + * + * M2Sharp is free software: you can redistribute and/or modify it under the + * terms of the GNU Lesser General Public License (LGPL) either version 2.1 + * or at your choice version 3 as published by the Free Software Foundation. + * However, you may not alter the copyright, author and license information. + * + * M2Sharp is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with M2Sharp. If not, see . + * + * NB: Components in the domain part of email addresses are in reverse order. + */ + +namespace org.m2sf.m2sharp { + +public class Terminals : ITerminals { + +const Token FirstResWord = Token.AND; +const Token LastResWord = Token.WITH; + +const Token FirstLiteral = Token.StringLiteral; +const Token LastLiteral = Token.CharLiteral; + +const Token FirstMalformedLiteral = Token.MalformedString; +const Token LastMalformedLiteral = Token.MalformedReal; + +const Token FirstSpecialSymbol = Token.Plus; +const Token LastSpecialSymbol = Token.RightBrace; + + +/* --------------------------------------------------------------------------- + * method IsValid(token) + * --------------------------------------------------------------------------- + * Returns true if token represents a valid token, otherwise false. + * ------------------------------------------------------------------------ */ + +public static bool IsValid (Token token) { + return (token != Token.Unknown) && !IsMalformedLiteral(token); +} /* end IsValid */ + + +/* --------------------------------------------------------------------------- + * method IsResword(token) + * --------------------------------------------------------------------------- + * Returns true if token represents a reserved word, otherwise false. + * ------------------------------------------------------------------------ */ + +public static bool IsResword (Token token) { + return (token >= FirstResWord) && (token <= LastResWord); +} /* end IsResword */ + + +/* --------------------------------------------------------------------------- + * method IsLiteral(token) + * --------------------------------------------------------------------------- + * Returns true if token represents a literal, otherwise false. + * ------------------------------------------------------------------------ */ + +public static bool IsLiteral (Token token) { + return (token >= FirstLiteral) && (token <= LastLiteral); +} /* end IsLiteral */ + + +/* --------------------------------------------------------------------------- + * method IsMalformedLiteral(token) + * --------------------------------------------------------------------------- + * Returns true if token represents a malformed literal, otherwise false. + * ------------------------------------------------------------------------ */ + +public static bool IsMalformedLiteral (Token token) { + return (token >= FirstMalformedLiteral) && (token <= LastMalformedLiteral); +} /* end IsMalformedLiteral */ + + +/* --------------------------------------------------------------------------- + * method IsSpecialSymbol(token) + * --------------------------------------------------------------------------- + * Returns true if token represents a special symbol, otherwise false. + * ------------------------------------------------------------------------ */ + +public static bool IsSpecialSymbol (Token token) { + return (token >= FirstSpecialSymbol) && (token <= LastSpecialSymbol); +} /* end IsSpecialSymbol */ + + +/* --------------------------------------------------------------------------- + * method TokenForResword(lexeme) + * --------------------------------------------------------------------------- + * Tests if the given lexeme represents a reserved word and returns the + * corresponding token or Unknown if it does not match a reserved word. + * ------------------------------------------------------------------------ */ + +public static Token TokenForResword (string lexeme) { + int length; + + /* check pre-conditions */ + if (lexeme == null) { + return Token.Unknown; + } /* end if */ + + length = lexeme.Length; + + if ((length < 2) || (length > 14)) { + return Token.Unknown; + } /* end if */ + + switch (length) { + case /* length = 2 */ 2 : + switch (lexeme[0]) { + + case 'B' : + /* BY */ + if (lexeme[1] == 'Y') { + return Token.BY; + } /* end if */ + break; + + case 'D' : + /* DO */ + if (lexeme[1] == 'O') { + return Token.DO; + } /* end if */ + break; + + case 'I' : + /* IF */ + if (lexeme[1] == 'F') { + return Token.IF; + } + + /* IN */ + else if (lexeme[1] == 'N') { + return Token.IN; + } /* end if */ + break; + + case 'O' : + /* OF */ + if (lexeme[1] == 'F') { + return Token.OF; + } + + /* OR */ + else if (lexeme[1] == 'R') { + return Token.OR; + } /* end if */ + break; + + case 'T' : + /* TO */ + if (lexeme[1] == 'O') { + return Token.TO; + } /* end if */ + break; + + } /* end switch */ + break; + + case /* length = 3 */ 3 : + switch (lexeme[0]) { + + case 'A' : + /* AND */ + if (string.CompareOrdinal(lexeme, "AND") == 0) { + return Token.AND; + } /* end if */ + break; + + case 'D' : + /* DIV */ + if (string.CompareOrdinal(lexeme, "DIV") == 0) { + return Token.DIV; + } /* end if */ + break; + + case 'E' : + /* END */ + if (string.CompareOrdinal(lexeme, "END") == 0) { + return Token.END; + } /* end if */ + break; + + case 'F' : + /* FOR */ + if (string.CompareOrdinal(lexeme, "FOR") == 0) { + return Token.FOR; + } /* end if */ + break; + + case 'M' : + /* MOD */ + if (string.CompareOrdinal(lexeme, "MOD") == 0) { + return Token.MOD; + } /* end if */ + break; + + case 'N' : + /* NOT */ + if (string.CompareOrdinal(lexeme, "NOT") == 0) { + return Token.NOT; + } /* end if */ + break; + + case 'S' : + /* SET */ + if (string.CompareOrdinal(lexeme, "SET") == 0) { + return Token.SET; + } /* end if */ + break; + + case 'V' : + /* VAR */ + if (string.CompareOrdinal(lexeme, "VAR") == 0) { + return Token.VAR; + } /* end if */ + break; + + } /* end switch */ + break; + + case /* length = 4 */ 4 : + switch (lexeme[1]) { + + case 'A' : + /* CASE */ + if (string.CompareOrdinal(lexeme, "CASE") == 0) { + return Token.CASE; + } /* end if */ + break; + + case 'H' : + /* THEN */ + if (string.CompareOrdinal(lexeme, "THEN") == 0) { + return Token.THEN; + } /* end if */ + break; + + case 'I' : + /* WITH */ + if (string.CompareOrdinal(lexeme, "WITH") == 0) { + return Token.WITH; + } /* end if */ + break; + + case 'L' : + /* ELSE */ + if (string.CompareOrdinal(lexeme, "ELSE") == 0) { + return Token.ELSE; + } /* end if */ + break; + + case 'O' : + /* LOOP */ + if (string.CompareOrdinal(lexeme, "LOOP") == 0) { + return Token.LOOP; + } /* end if */ + break; + + case 'R' : + /* FROM */ + if (string.CompareOrdinal(lexeme, "FROM") == 0) { + return Token.FROM; + } /* end if */ + break; + + case 'X' : + /* EXIT */ + if (string.CompareOrdinal(lexeme, "EXIT") == 0) { + return Token.EXIT; + } /* end if */ + break; + + case 'Y' : + /* TYPE */ + if (string.CompareOrdinal(lexeme, "TYPE") == 0) { + return Token.TYPE; + } /* end if */ + break; + + } /* end switch */ + break; + + case /* length = 5 */ 5 : + switch (lexeme[0]) { + + case 'A' : + /* ARRAY */ + if (string.CompareOrdinal(lexeme, "ARRAY") == 0) { + return Token.ARRAY; + } /* end if */ + break; + + case 'B' : + /* BEGIN */ + if (string.CompareOrdinal(lexeme, "BEGIN") == 0) { + return Token.BEGIN; + } /* end if */ + break; + + case 'C' : + /* CONST */ + if (string.CompareOrdinal(lexeme, "CONST") == 0) { + return Token.CONST; + } /* end if */ + break; + + case 'E' : + /* ELSIF */ + if (string.CompareOrdinal(lexeme, "ELSIF") == 0) { + return Token.ELSIF; + } /* end if */ + break; + + case 'U' : + /* UNTIL */ + if (string.CompareOrdinal(lexeme, "UNTIL") == 0) { + return Token.UNTIL; + } /* end if */ + break; + + case 'W' : + /* WHILE */ + if (string.CompareOrdinal(lexeme, "WHILE") == 0) { + return Token.WHILE; + } /* end if */ + break; + + } /* end switch */ + break; + + case /* length 6 */ 6 : + switch (lexeme[2]) { + + case 'D' : + /* MODULE */ + if (string.CompareOrdinal(lexeme, "MODULE") == 0) { + return Token.MODULE; + } /* end if */ + break; + + case 'A' : + /* OPAQUE */ + if (string.CompareOrdinal(lexeme, "OPAQUE") == 0) { + return Token.OPAQUE; + } /* end if */ + break; + + case 'C' : + /* RECORD */ + if (string.CompareOrdinal(lexeme, "RECORD") == 0) { + return Token.RECORD; + } /* end if */ + break; + + case 'T' : + /* RETURN */ + if (string.CompareOrdinal(lexeme, "RETURN") == 0) { + return Token.RETURN; + } /* end if */ + break; + + case 'P' : + switch (lexeme[0]) { + + case 'E' : + /* EXPORT */ + if (string.CompareOrdinal(lexeme, "EXPORT") == 0) { + return Token.EXPORT; + } /* end if */ + break; + + case 'I' : + /* IMPORT */ + if (string.CompareOrdinal(lexeme, "IMPORT") == 0) { + return Token.IMPORT; + } /* end if */ + break; + + case 'R' : + /* REPEAT */ + if (string.CompareOrdinal(lexeme, "REPEAT") == 0) { + return Token.REPEAT; + } /* end if */ + break; + + } /* end switch */ + break; + + } /* end switch */ +break; + + case /* length = 7 */ 7 : + switch (lexeme[0]) { + + case 'A' : + /* ARGLIST */ + if (string.CompareOrdinal(lexeme, "ARGLIST") == 0) { + return Token.ARGLIST; + } /* end if */ + break; + + case 'P' : + /* POINTER */ + if (string.CompareOrdinal(lexeme, "POINTER") == 0) { + return Token.POINTER; + } /* end if */ + break; + + } /* end switch */ + break; + + case /* length = 9 */ 9 : + switch (lexeme[0]) { + + case 'P' : + /* PROCEDURE */ + if (string.CompareOrdinal(lexeme, "PROCEDURE") == 0) { + return Token.PROCEDURE; + } /* end if */ + break; + + case 'Q' : + /* QUALIFIED */ + if (string.CompareOrdinal(lexeme, "QUALIFIED") == 0) { + return Token.QUALIFIED; + } /* end if */ + break; + + } /* end switch */ + break; + + case /* length = 10 */ 10 : + /* DEFINITION */ + if (string.CompareOrdinal(lexeme, "DEFINITION") == 0) { + return Token.DEFINITION; + } /* end if */ + break; + + case /* length = 14 */ 14 : + /* IMPLEMENTATION */ + if (string.CompareOrdinal(lexeme, "IMPLEMENTATION") == 0) { + return Token.IMPLEMENTATION; + } /* end if */ + break; + + } /* end switch (length) */ + + return Token.Unknown; +} /* end TokenForResword */ + + +/* --------------------------------------------------------------------------- + * method LexemeForResword(token) + * --------------------------------------------------------------------------- + * Returns a string with the lexeme for the reserved word represented by + * token. Returns null if the token does not represent a reserved word. + * ------------------------------------------------------------------------ */ + +public static string LexemeForResword (Token token) { + if (IsResword(token) == false) { + return null; + } /* end if */ + return token.ToString(); +} /* end LexemeForResword */ + + +/* --------------------------------------------------------------------------- + * method LexemeForSpecialSymbol(token) + * --------------------------------------------------------------------------- + * Returns a string with the lexeme for the special symbol represented by + * token. Returns null if the token does not represent a special symbol. + * ------------------------------------------------------------------------ */ + +public static string LexemeForSpecialSymbol (Token token) { + if (IsSpecialSymbol(token) == false) { + return null; + } /* end if */ + return ""; // TO DO +} /* end LexemeForSpecialSymbol */ + + +/* --------------------------------------------------------------------------- + * method NameForToken(token) + * --------------------------------------------------------------------------- + * Returns a string with a human readable name for token. Returns null if + * token is not a valid token. + * ------------------------------------------------------------------------ */ + +public static string NameForToken (Token token) { + return token.ToString(); +} /* NameForToken */ + + +} /* Terminals */ + +} /* namespace */ + +/* END OF FILE */ \ No newline at end of file diff --git a/TokenSet.cs b/TokenSet.cs new file mode 100644 index 0000000..7490eb1 --- /dev/null +++ b/TokenSet.cs @@ -0,0 +1,574 @@ +/* M2Sharp -- Modula-2 to C# Translator & Compiler + * + * Copyright (c) 2016 The Modula-2 Software Foundation + * + * Author & Maintainer: Benjamin Kowarsch + * + * @synopsis + * + * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. + * It supports the dialects described in the 3rd and 4th editions of Niklaus + * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, + * and an extended mode with select features from the revised language by + * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). + * + * In translator mode, M2Sharp translates Modula-2 source to C# source files. + * In compiler mode, M2Sharp compiles Modula-2 source via C# source files + * to object code or executables using the host system's C# compiler. + * + * @repository + * + * https://github.com/m2sf/m2sharp + * + * @file + * + * TokenSet.cs + * + * A set of Token type variables which is used to pass First and Follow sets + * + * @license + * + * M2Sharp is free software: you can redistribute and/or modify it under the + * terms of the GNU Lesser General Public License (LGPL) either version 2.1 + * or at your choice version 3 as published by the Free Software Foundation. + * However, you may not alter the copyright, author and license information. + * + * M2Sharp is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with M2Sharp. If not, see . + * + * NB: Components in the domain part of email addresses are in reverse order. + */ + + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace org.m2sf.m2sharp +{ + + public class TokenSet : ITokenSet { + + /* Lexeme Table */ + public static string[] lexemeTable = { + /* Null Token */ + + "UNKNOWN", + + /* Reserved Words */ + + "AND", + "ARGLIST", + "ARRAY", + "BEGIN", + "BY", + "CASE", + "CONST", + "DEFINITION", + "DIV", + "DO", + "ELSE", + "ELSIF", + "END", + "EXIT", + "EXPORT", + "FOR", + "FROM", + "IF", + "IMPLEMENTATION", + "IMPORT", + "IN", + "LOOP", + "MOD", + "MODULE", + "NOT", + "OF", + "OR", + "POINTER", + "PROCEDURE", + "QUALIFIED", + "RECORD", + "REPEAT", + "RETURN", + "SET", + "THEN", + "TO", + "TYPE", + "UNTIL", + "VAR", + "WHILE", + "WITH", + + /* Identifiers */ + + "IDENTIFIER", + + /* Literals */ + + "STRING-LITERAL", + "INTEGER-LITERAL", + "REAL-LITERAL", + "CHAR-LITERAL", + + "MALFORMED-STRING", + "MALFORMED-INTEGER", + "MALFORMED-REAL", + + /* Pragmas */ + + "PRAGMA", + + /* Special Symbols */ + + "PLUS", + "MINUS", + "EQUAL", + "NOTEQUAL", + "LESS-THAN", + "LESS-OR-EQUAL", + "GREATER-THAN", + "GREATER-OR-EQUAL", + "ASTERISK", + "SOLIDUS", + "BACKSLASH", + "ASSIGNMENT", + "COMMA", + "PERIOD", + "COLON", + "SEMICOLON", + "RANGE", + "DEREF", + "VERTICAL-BAR", + "LEFT-PAREN", + "RIGHT-PAREN", + "LEFT-BRACKET", + "RIGHT-BRACKET", + "LEFT-BRACE", + "RIGHT-BRACE", + "END-OF-FILE" + +}; /* end m2c_token_name_table */ + + public static const int segmentCount = (Enum.GetNames(typeof(Token)).Length / 32) + 1; + + public struct TokenSetBits + { + public uint[] segments = new uint[segmentCount]; + public uint elemCount; + } /* end struct */ + + TokenSetBits dataStored; + + /* --------------------------------------------------------------------------- + * private constructor TokenSet () + * --------------------------------------------------------------------------- + * Prevents clients from invoking the default constructor. + * ------------------------------------------------------------------------ */ + + private TokenSet() + { + // no operation + } /* end TokenSet */ + + /* -------------------------------------------------------------------------- + * constructor newFromList(token, ...) + * -------------------------------------------------------------------------- + * Returns a newly allocated tokenset object that includes the tokens passed + * as arguments of a variadic argument list. + * ----------------------------------------------------------------------- */ + + public static TokenSet newFromList(params Token[] tokenList) + { + TokenSet newSet = new TokenSet(); + uint bit, segmentIndex; + Token token; + + /* allocate new set */ + newSet.dataStored.segments = new uint[segmentCount]; + + /* initialise */ + for (segmentIndex = 0; segmentIndex < segmentCount; segmentIndex++) + { + newSet.dataStored.segments[segmentIndex] = 0; + } /* end For */ + + /* store tokens from list */ + token = tokenList[0]; + for (int i = 1; token != Token.Unknown; i++) + { + + if (token <= Token.EndOfFile) + { + segmentIndex = (uint)token / 32; + bit = (uint)token % 32; + newSet.dataStored.segments[segmentIndex] = (newSet.dataStored.segments[segmentIndex] | (uint)(1 << (int)bit)); + } /* end if */ + + /* get next token in list */ + token = tokenList[i]; + } /* end while */ + + /* update element counter */ + newSet.dataStored.elemCount = CountBits(newSet); + + return newSet; + } /* end newFromList */ + + + /* -------------------------------------------------------------------------- + * constructor newFromUnion(set, ...) + * -------------------------------------------------------------------------- + * Returns a newly allocated tokenset object that represents the set union of + * the tokensets passed as arguments of a non-empty variadic argument list. + * ----------------------------------------------------------------------- */ + + public static TokenSet newFromUnion(params TokenSet[] setList) + { + uint segmentIndex; + TokenSet set, + newSet = new TokenSet(); + + + /* initialise */ + newSet.dataStored.segments = new uint[segmentCount]; + for (segmentIndex = 0; segmentIndex < segmentCount; segmentIndex++) + { + newSet.dataStored.segments[segmentIndex] = 0; + } /* end for */ + + set = setList[0]; + /* calculate union with each set in list */ + for (int i = 0; set != null; i++) + { + /* for each segment ... */ + while (segmentIndex < segmentCount) + { + /* ... store union of corresponding segments */ + newSet.dataStored.segments[segmentIndex] = + newSet.dataStored.segments[segmentIndex] | newSet.dataStored.segments[segmentIndex]; + + /* next segment */ + segmentIndex++; + } /* end while */ + + /*get next set in list */ + if (i < setList.Length) + set = setList[i]; + else + set = null; + + } /* end for */ + + /* update element counter */ + newSet.dataStored.elemCount = CountBits(newSet); + + return newSet; + } /* end newFromUnion + +/* -------------------------------------------------------------------------- + * method Count() + * -------------------------------------------------------------------------- + * Returns the number of elements in the receiver. + * ----------------------------------------------------------------------- */ + + public uint Count() + { + + return this.dataStored.elemCount; + } /* end IsElement */ + + + /* -------------------------------------------------------------------------- + * method IsElement(token) + * -------------------------------------------------------------------------- + * Returns true if token is an element of the receiver, else false. + * ----------------------------------------------------------------------- */ + + public bool IsElement(Token token) + { + int segmentIndex, bit; + + if (token > Token.EndOfFile) + { + return false; + } /* end if */ + + segmentIndex = (int)token / 32; + bit = (int)token % 32; + + return (this.dataStored.segments[segmentIndex] & (1 << bit)) != 0; + } /* end IsElement */ + + + /* -------------------------------------------------------------------------- + * method IsSubset(set) + * -------------------------------------------------------------------------- + * Returns true if each element in set is also in the receiver, else false. + * ----------------------------------------------------------------------- */ + + public bool IsSubset(TokenSet set) + { + + for (uint segmentIndex = 0; segmentIndex < segmentCount; segmentIndex++) + { + + if (((set.dataStored.segments[segmentIndex] & this.dataStored.segments[segmentIndex]) + ^ this.dataStored.segments[segmentIndex]) != 0) + { + + return false; + + } /* end if */ + + } /* end for */ + + return true; + + } /* end IsSubset */ + + + /* -------------------------------------------------------------------------- + * method IsDisjunct(set) + * -------------------------------------------------------------------------- + * Returns true if set has no common elements with the receiver, else false. + * ----------------------------------------------------------------------- */ + + public bool IsDisjunct(TokenSet set) + { + + for (uint segmentIndex = 0; segmentIndex < segmentCount; segmentIndex++) + { + + if ((set.dataStored.segments[segmentIndex] & this.dataStored.segments[segmentIndex]) != 0) + { + + return false; + + } /* end if */ + + } /* end for */ + + return true; + + } /* end IsDisjunct */ + + /* -------------------------------------------------------------------------- + * method ElementList() + * -------------------------------------------------------------------------- + * Returns a token list of all elements in the receiver. + * ----------------------------------------------------------------------- */ + //TODO + + public List ElementList() + { + List allElements = new List(); + uint segmentIndex, count; + int bit; + Token token; + + if (this.dataStored.elemCount == 0) + { + return null; + } /* end if */ + + token = 0; + count = 0; + + while ((count <= this.dataStored.elemCount) && (token <= Token.EndOfFile)) + { + segmentIndex = (uint)token / 32; + bit = (int)token % 32; + + if (((this.dataStored.segments[segmentIndex]) & (1 << (int)bit)) != 0) + { + count++; + allElements.Add(token); + } /* end if */ + token++; + } /* end while */ + + return allElements; + + } /* end ElementList */ + + + /* -------------------------------------------------------------------------- + * method PrintSet(label) + * -------------------------------------------------------------------------- + * Prints a human readable representation of the receiver. + * Format: label = { comma-separated list of tokens }; + * ----------------------------------------------------------------------- */ + + public void PrintSet(string label) + { + uint segmentIndex, count; + int bit; + Token token; + + Console.Write(label + " = {"); + if (this.dataStored.elemCount == 0) + { + Console.Write(" "); + } /* end if */ + + token = 0; + count = 0; + + while ((count <= this.dataStored.elemCount) && (token <= Token.EndOfFile)) + { + segmentIndex = (uint)token / 32; + bit = (int)token % 32; + + if (((this.dataStored.segments[segmentIndex]) & (1 << (int)bit)) != 0) + { + count++; + + if (count <= this.dataStored.elemCount) + { + + Console.Write("\n {0},", lexemeTable[(uint)token]); + } + else + { + Console.WriteLine("\n {0}", lexemeTable[(uint)token]); + } /* end if */ + } /* end if */ + token++; + } /* end while */ + + Console.WriteLine("};"); + } /* end PrintSet */ + + + /* -------------------------------------------------------------------------- + * method PrintList() + * -------------------------------------------------------------------------- + * Prints a human readable list of tokens in the receiver. + * Format: first, second, third, ..., secondToLast or last + * ----------------------------------------------------------------------- */ + + public void PrintList() + { + uint bit, segmentIndex, count; + Token token; + + if (this.dataStored.elemCount == 0) + { + Console.WriteLine("(nil)"); + } + + count = 0; + token = 0; + while ((count <= this.dataStored.elemCount) && (token <= Token.EndOfFile)) + { + segmentIndex = (uint)token / 32; + bit = (uint)token % 32; + + if (((this.dataStored.segments[segmentIndex] & (1 << (int)bit)) != 0)) + { + count++; + if (count > 1) + { + if (count <= this.dataStored.elemCount) + { + Console.Write(", "); + } + else + { + Console.Write(" or "); + } /* end if */ + } /* end if */ + + Console.Write(lexemeTable[(uint)token]); + } + token++; + } + + Console.WriteLine("."); + } /* end PrintList */ + + + /* -------------------------------------------------------------------------- + * method PrintLiteralStruct(ident) + * -------------------------------------------------------------------------- + * Prints a struct definition for a tokenset literal for the receiver. + * Format: struct ident { uint s0; uint s1; uint s2; ...; uint n }; + * ----------------------------------------------------------------------- */ + + public void PrintLiteralStruct(string ident) + { + uint segmentIndex; + + Console.Write("struct {0} {{ uint s0", ident); + + for (segmentIndex = 1; segmentIndex < segmentCount; segmentIndex++) + { + Console.Write("; s" + segmentIndex); + } /* end for */ + + Console.WriteLine("; n };"); + Console.WriteLine("typedef struct {0} {0};", ident); + + } /* end PrintLiteralStruct */ + + + /* -------------------------------------------------------------------------- + * method PrintLiteral() + * -------------------------------------------------------------------------- + * Prints a sequence of hex values representing the bit pattern of receiver. + * Format: ( 0xHHHHHHHH, 0xHHHHHHHH, ..., count ); + * ----------------------------------------------------------------------- */ + + public void PrintLiteral() + { + uint segmentIndex; + + Console.Write("( /* bits: */ 0x" + this.dataStored.segments[0].ToString("x08")); + + for (segmentIndex = 1; segmentIndex < segmentCount; segmentIndex++) + { + + Console.Write(", 0x" + this.dataStored.segments[segmentIndex].ToString("x08")); + } /* end for */ + + Console.WriteLine(", /* counter: */ " + this.dataStored.elemCount + " );"); + } /* end PrintLiteral */ + + /* -------------------------------------------------------------------------- + * method CountBits() + * -------------------------------------------------------------------------- + * Returns the number of set bits in set. + * ----------------------------------------------------------------------- */ + + static private uint CountBits(TokenSet set) + { + int bit; + uint segmentIndex, + bitCount = 0; + + for (segmentIndex = 0; segmentIndex < segmentCount; segmentIndex++) + { + + for (bit = 0; bit < 32; bit++) + { + + if ((set.dataStored.segments[segmentIndex] & (1 << bit)) != 0) + { + bitCount++; + } /* end if */ + + } /* end for */ + + } /* end for */ + + return bitCount; + } /* end CountBits */ + + } /* end TokenSet */ + +} /* end namespace */ diff --git a/utils/GenFirstSets.cs b/utils/GenFirstSets.cs new file mode 100644 index 0000000..24a2403 --- /dev/null +++ b/utils/GenFirstSets.cs @@ -0,0 +1,672 @@ +/* M2Sharp -- Modula-2 to C# Translator & Compiler + * + * Copyright (c) 2016 The Modula-2 Software Foundation + * + * Author & Maintainer: Benjamin Kowarsch + * + * @synopsis + * + * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. + * It supports the dialects described in the 3rd and 4th editions of Niklaus + * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, + * and an extended mode with select features from the revised language by + * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). + * + * In translator mode, M2Sharp translates Modula-2 source to C# source files. + * In compiler mode, M2Sharp compiles Modula-2 source via C# source files + * to object code or executables using the host system's C# compiler. + * + * @repository + * + * https://github.com/m2sf/m2sharp + * + * @file + * + * GenFirstSets.cs + * + * A class dedicated to producing representations of the first sets used by the parser. + * + * @license + * + * M2Sharp is free software: you can redistribute and/or modify it under the + * terms of the GNU Lesser General Public License (LGPL) either version 2.1 + * or at your choice version 3 as published by the Free Software Foundation. + * However, you may not alter the copyright, author and license information. + * + * M2Sharp is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with M2Sharp. If not, see . + * + * NB: Components in the domain part of email addresses are in reverse order. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace org.m2sf.m2sharp { + + class GenFirstSets { + + public static List genSets() { + + List lines = new List(); + TokenSet set; + + /* * * headline * * */ + + lines.Add("/* M2sharp FIRST set initialisers" + + " -- generated by GenFirstSets.cs */\n\n"); + + /* definitionModule */ + set = TokenSet.newFromList(Token.DEFINITION, 0); + lines.Add("Creating First Set for: DEFINITION MODULE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* import */ + set = TokenSet.newFromList(Token.IMPORT, Token.FROM, 0); + lines.Add("Creating First Set for: IMPORT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* qualfiedImport */ + set = TokenSet.newFromList(Token.IMPORT, 0); + lines.Add("Creating First Set for: QUALIFIED IMPORT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* unqualfiedImport */ + set = TokenSet.newFromList(Token.FROM, 0); + lines.Add("Creating First Set for: UNQUALIFIED IMPORT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* identList */ + set = TokenSet.newFromList(Token.Identifier, 0); + lines.Add("Creating First Set for: IDENT LIST \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* definition */ + set = TokenSet.newFromList + (Token.CONST, Token.TYPE, Token.VAR, Token.PROCEDURE, 0); + lines.Add("Creating First Set for: DEFINITION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* constDefinition */ + set = TokenSet.newFromList(Token.Identifier, 0); + lines.Add("Creating First Set for: CONST DEFINITION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* typeDefinition */ + set = TokenSet.newFromList(Token.Identifier, 0); + lines.Add("Creating First Set for: TYPE DEFINITION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* type */ + set = TokenSet.newFromList + (Token.Identifier, Token.LeftBracket, Token.LeftParen, + Token.SET, Token.ARRAY, Token.RECORD, Token.POINTER, Token.PROCEDURE, 0); + lines.Add("Creating First Set for: TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* derivedOrSubRangeType */ + set = TokenSet.newFromList(Token.Identifier, Token.LeftBracket, 0); + lines.Add("Creating First Set for: DERIVED OR SUBRange TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* qualident */ + set = TokenSet.newFromList(Token.Identifier, 0); + lines.Add("Creating First Set for: QUALIDENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Range */ + set = TokenSet.newFromList(Token.LeftBracket, 0); + lines.Add("Creating First Set for: Range \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* enumType */ + set = TokenSet.newFromList(Token.LeftParen, 0); + lines.Add("Creating First Set for: ENUM TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* setType */ + set = TokenSet.newFromList(Token.SET, 0); + lines.Add("Creating First Set for: SET TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* countableType */ + set = TokenSet.newFromList + (Token.LeftBracket, Token.LeftParen, Token.Identifier, 0); + lines.Add("Creating First Set for: COUNTABLE TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* arrayType */ + set = TokenSet.newFromList(Token.ARRAY, 0); + lines.Add("Creating First Set for: ARRAY TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* extensibleRecordType */ + set = TokenSet.newFromList(Token.RECORD, 0); + lines.Add("Creating First Set for: EXTENSIBLE RECORD TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* fieldListSequence */ + set = TokenSet.newFromList(Token.Identifier, 0); + lines.Add("Creating First Set for: FIELD LIST SEQUENCE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* variantRecordType */ + set = TokenSet.newFromList(Token.RECORD, 0); + lines.Add("Creating First Set for: VARIANT RECORD TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* variantFieldListSeq */ + set = TokenSet.newFromList(Token.Identifier, Token.CASE, 0); + lines.Add("Creating First Set for: VARIANT FIELD LIST SEQ \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* variantFieldList */ + set = TokenSet.newFromList(Token.Identifier, Token.CASE, 0); + lines.Add("Creating First Set for: VARIANT FIELD LIST \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* variantFields */ + set = TokenSet.newFromList(Token.CASE, 0); + lines.Add("Creating First Set for: VARIANT FIELDS \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* variant */ + set = TokenSet.newFromList + (Token.Plus, Token.Minus, Token.NOT, Token.IntLiteral, Token.RealLiteral, + Token.CharLiteral, Token.StringLiteral, Token.LeftBrace, Token.LeftParen, + Token.Identifier, 0); + lines.Add("Creating First Set for: VARIANT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* caseLabelList */ + set = TokenSet.newFromList + (Token.Plus, Token.Minus, Token.NOT, Token.IntLiteral, Token.RealLiteral, + Token.CharLiteral, Token.StringLiteral, Token.LeftBrace, Token.LeftParen, + Token.Identifier, 0); + lines.Add("Creating First Set for: CASE LABEL LIST \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* caseLabels */ + set = TokenSet.newFromList + (Token.Plus, Token.Minus, Token.NOT, Token.IntLiteral, Token.RealLiteral, + Token.CharLiteral, Token.StringLiteral, Token.LeftBrace, Token.LeftParen, + Token.Identifier, 0); + lines.Add("Creating First Set for: CASE LABELS \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* pointerType */ + set = TokenSet.newFromList(Token.POINTER, 0); + lines.Add("Creating First Set for: POINTER TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* procedureType */ + set = TokenSet.newFromList(Token.PROCEDURE, 0); + lines.Add("Creating First Set for: PROCEDURE TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* simpleFormalType */ + set = TokenSet.newFromList(Token.ARRAY, Token.Identifier, 0); + lines.Add("Creating First Set for: SIMPLE FORMAL TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* procedureHeader */ + set = TokenSet.newFromList(Token.PROCEDURE, 0); + lines.Add("Creating First Set for: PROCEDURE HEADER \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* procedureSignature */ + set = TokenSet.newFromList(Token.Identifier, 0); + lines.Add("Creating First Set for: PROCEDURE SIGNATURE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* simpleFormalParams */ + set = TokenSet.newFromList(Token.Identifier, 0); + lines.Add("Creating First Set for: SIMPLE FORMAL PARAMS \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* implementationModule */ + set = TokenSet.newFromList(Token.IMPLEMENTATION, 0); + lines.Add("Creating First Set for: IMPLEMENTATION MODULE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* programModule */ + set = TokenSet.newFromList(Token.MODULE, 0); + lines.Add("Creating First Set for: PROGRAM MODULE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* modulePriority */ + set = TokenSet.newFromList(Token.LeftBracket, 0); + lines.Add("Creating First Set for: MODULE PRIORITY \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* block */ + set = TokenSet.newFromList + (Token.CONST, Token.TYPE, Token.VAR, Token.PROCEDURE, Token.MODULE, + Token.BEGIN, Token.END, 0); + lines.Add("Creating First Set for: BLOCK \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* declaration */ + set = TokenSet.newFromList + (Token.CONST, Token.TYPE, Token.VAR, Token.PROCEDURE, Token.MODULE, 0); + lines.Add("Creating First Set for: DECLARATION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* typeDeclaration */ + set = TokenSet.newFromList(Token.Identifier, 0); + lines.Add("Creating First Set for: TYPE DECLARATION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* varSizeRecordType */ + set = TokenSet.newFromList(Token.VAR, 0); + lines.Add("Creating First Set for: VAR SIZE RECORD TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* variableDeclaration */ + set = TokenSet.newFromList(Token.Identifier, 0); + lines.Add("Creating First Set for: VARIABLE DECLARATION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* procedureDeclaration */ + set = TokenSet.newFromList(Token.PROCEDURE, 0); + lines.Add("Creating First Set for: PROCEDURE DECLARATION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* moduleDeclaration */ + set = TokenSet.newFromList(Token.MODULE, 0); + lines.Add("Creating First Set for: MODULE DECLARATION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* export */ + set = TokenSet.newFromList(Token.EXPORT, 0); + lines.Add("Creating First Set for: EXPORT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* statementSequence */ + set = TokenSet.newFromList + (Token.Identifier, Token.RETURN, Token.WITH, Token.IF, Token.CASE, + Token.LOOP, Token.WHILE, Token.REPEAT, Token.FOR, Token.EXIT, 0); + lines.Add("Creating First Set for: STATEMENT SEQUENCE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* statement */ + set = TokenSet.newFromList + (Token.Identifier, Token.RETURN, Token.WITH, Token.IF, Token.CASE, + Token.LOOP, Token.WHILE, Token.REPEAT, Token.FOR, Token.EXIT, 0); + lines.Add("Creating First Set for: STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* AssignmentOrProcCall */ + set = TokenSet.newFromList(Token.Identifier, 0); + lines.Add("Creating First Set for: AssignMENT OR PROC CALL \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* actualParameters */ + set = TokenSet.newFromList(Token.LeftParen, 0); + lines.Add("Creating First Set for: ACTUAL PARAMETERS \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* expressionList */ + set = TokenSet.newFromList + (Token.Plus, Token.Minus, Token.NOT, Token.IntLiteral, Token.RealLiteral, + Token.CharLiteral, Token.StringLiteral, Token.LeftBrace, Token.LeftParen, + Token.Identifier, 0); + lines.Add("Creating First Set for: EXPRESSION LIST \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* returnStatement */ + set = TokenSet.newFromList(Token.RETURN, 0); + lines.Add("Creating First Set for: RETURN STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* withStatement */ + set = TokenSet.newFromList(Token.WITH, 0); + lines.Add("Creating First Set for: WITH STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* ifStatement */ + set = TokenSet.newFromList(Token.IF, 0); + lines.Add("Creating First Set for: IF STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* caseStatement */ + set = TokenSet.newFromList(Token.CASE, 0); + lines.Add("Creating First Set for: CASE STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* case */ + set = TokenSet.newFromList + (Token.Plus, Token.Minus, Token.NOT, Token.IntLiteral, Token.RealLiteral, + Token.CharLiteral, Token.StringLiteral, Token.LeftBrace, Token.LeftParen, + Token.Identifier, 0); + lines.Add("Creating First Set for: CASE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* loopStatement */ + set = TokenSet.newFromList(Token.LOOP, 0); + lines.Add("Creating First Set for: LOOP STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* whileStatement */ + set = TokenSet.newFromList(Token.WHILE, 0); + lines.Add("Creating First Set for: WHILE STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* repeatStatement */ + set = TokenSet.newFromList(Token.REPEAT, 0); + lines.Add("Creating First Set for: REPEAT STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* forStatement */ + set = TokenSet.newFromList(Token.FOR, 0); + lines.Add("Creating First Set for: FOR STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* designator */ + set = TokenSet.newFromList(Token.Identifier, 0); + lines.Add("Creating First Set for: DESIGNATOR \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* selector */ + set = TokenSet.newFromList + (Token.Deref, Token.Period, Token.LeftBracket, 0); + lines.Add("Creating First Set for: SELECTOR \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* expression */ + set = TokenSet.newFromList + (Token.Plus, Token.Minus, Token.NOT, Token.IntLiteral, Token.RealLiteral, + Token.CharLiteral, Token.StringLiteral, Token.LeftBrace, Token.LeftParen, + Token.Identifier, 0); + lines.Add("Creating First Set for: EXPRESSION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* simpleExpression */ + set = TokenSet.newFromList + (Token.Plus, Token.Minus, Token.NOT, Token.IntLiteral, Token.RealLiteral, + Token.CharLiteral, Token.StringLiteral, Token.LeftBrace, Token.LeftParen, + Token.Identifier, 0); + lines.Add("Creating First Set for: SIMPLE EXPRESSION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* term */ + set = TokenSet.newFromList + (Token.NOT, Token.IntLiteral, Token.RealLiteral, Token.CharLiteral, Token.StringLiteral, + Token.LeftBrace, Token.LeftParen, Token.Identifier, 0); + lines.Add("Creating First Set for: TERM \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* simpleTerm */ + set = TokenSet.newFromList + (Token.NOT, Token.IntLiteral, Token.RealLiteral, Token.CharLiteral, Token.StringLiteral, + Token.LeftBrace, Token.LeftParen, Token.Identifier, 0); + lines.Add("Creating First Set for: SIMPLE TERM \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* factor */ + set = TokenSet.newFromList + (Token.IntLiteral, Token.RealLiteral, Token.CharLiteral, Token.StringLiteral, + Token.LeftBrace, Token.LeftParen, Token.Identifier, 0); + lines.Add("Creating First Set for: FACTOR \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* designatorOrFuncCall */ + set = TokenSet.newFromList(Token.Identifier, 0); + lines.Add("Creating First Set for: DESIGNATOR OR FUNC CALL \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* setValue */ + set = TokenSet.newFromList(Token.LeftBrace, 0); + lines.Add("Creating First Set for: SET VALUE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* element */ + set = TokenSet.newFromList + (Token.Plus, Token.Minus, Token.NOT, Token.IntLiteral, Token.RealLiteral, + Token.CharLiteral, Token.StringLiteral, Token.LeftBrace, Token.LeftParen, + Token.Identifier, 0); + lines.Add("Creating First Set for: ELEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Option dependent FIRST sets */ + + /* formalType */ + set = TokenSet.newFromList + (Token.ARRAY, Token.CONST, Token.VAR, Token.Identifier, 0); + lines.Add("Creating First Set for: FORMAL TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* attributedFormalType */ + set = TokenSet.newFromList(Token.CONST, Token.VAR, 0); + lines.Add("Creating First Set for: ATTRIBUTED FORMAL TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* formalParamList */ + set = TokenSet.newFromList + (Token.Identifier, Token.CONST, Token.VAR, 0); + lines.Add("Creating First Set for: FORMAL PARAM LIST \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* formalParams */ + set = TokenSet.newFromList + (Token.Identifier, Token.CONST, Token.VAR, 0); + lines.Add("Creating First Set for: FORMAL PARAMS \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* attribFormalParams */ + set = TokenSet.newFromList(Token.CONST, Token.VAR, 0); + lines.Add("Creating First Set for: ATTRIB FORMAL PARAMS \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* typeDeclarationTail */ + set = TokenSet.newFromList + (Token.VAR, Token.Identifier, Token.LeftBracket, Token.LeftParen, + Token.SET, Token.ARRAY, Token.RECORD, Token.POINTER, Token.PROCEDURE, 0); + lines.Add("Creating First Set for: TYPE DECLARATION TAIL \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Alternative FIRST sets */ + + /* Alternative formalType */ + set = TokenSet.newFromList + (Token.ARRAY, Token.VAR, Token.Identifier, 0); + lines.Add("Creating First Set for: ALT FORMAL TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Alternative attributedFormalType */ + set = TokenSet.newFromList(Token.VAR, 0); + lines.Add("Creating First Set for: ALT ATTRIB FORMAL TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Alternative formalParamList */ + set = TokenSet.newFromList(Token.Identifier, Token.VAR, 0); + lines.Add("Creating First Set for: ALT FORMAL PARAM LIST \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Alternative formalParams */ + set = TokenSet.newFromList(Token.Identifier, Token.VAR, 0); + lines.Add("Creating First Set for: ALT FORMAL PARAMS \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Alternative attribFormalParams */ + set = TokenSet.newFromList(Token.VAR, 0); + lines.Add("Creating First Set for: ALT ATTRIB FORMAL PARAMS \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Alternative typeDeclarationTail */ + set = TokenSet.newFromList + (Token.Identifier, Token.LeftBracket, Token.LeftParen, + Token.SET, Token.ARRAY, Token.RECORD, Token.POINTER, Token.PROCEDURE, 0); + lines.Add("Creating First Set for: ALT TYPE DECL TAIL \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + return lines; + + } /* end genSets */ + + } /* end GenFirstSets */ + +} /* end namespace */ diff --git a/utils/GenFollowSets.cs b/utils/GenFollowSets.cs new file mode 100644 index 0000000..2fcefcb --- /dev/null +++ b/utils/GenFollowSets.cs @@ -0,0 +1,740 @@ +/* M2Sharp -- Modula-2 to C# Translator & Compiler + * + * Copyright (c) 2016 The Modula-2 Software Foundation + * + * Author & Maintainer: Benjamin Kowarsch + * + * @synopsis + * + * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. + * It supports the dialects described in the 3rd and 4th editions of Niklaus + * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, + * and an extended mode with select features from the revised language by + * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). + * + * In translator mode, M2Sharp translates Modula-2 source to C# source files. + * In compiler mode, M2Sharp compiles Modula-2 source via C# source files + * to object code or executables using the host system's C# compiler. + * + * @repository + * + * https://github.com/m2sf/m2sharp + * + * @file + * + * GenFollowSets.cs + * + * A class dedicated to producing representations of the follow sets used by the parser. + * + * @license + * + * M2Sharp is free software: you can redistribute and/or modify it under the + * terms of the GNU Lesser General Public License (LGPL) either version 2.1 + * or at your choice version 3 as published by the Free Software Foundation. + * However, you may not alter the copyright, author and license information. + * + * M2Sharp is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with M2Sharp. If not, see . + * + * NB: Components in the domain part of email addresses are in reverse order. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace org.m2sf.m2sharp +{ + + class GenFollowSets { + + public static List genSets() { + + + List lines = new List(); + TokenSet set; + + lines.Add("/* M2sharp FOLLOW Set initialisers" + + " -- generated by GenFirstSets.cs */\n\n"); + + /* definitionModule */ + set = TokenSet.newFromList(Token.DEFINITION, 0); + lines.Add("Creating Follow Set for: DEFINITION MODULE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* import */ + set = TokenSet.newFromList + (Token.DEFINITION, Token.BEGIN, Token.CONST, Token.END, Token.MODULE, + Token.PROCEDURE, Token.TYPE, Token.VAR, Token.EXPORT, 0); + lines.Add("Creating Follow Set for: IMPORT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* qualfiedImport */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: QUALIFIED IMPORT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* unqualfiedImport */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: UNQUALIFIED IMPORT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* identList */ + set = TokenSet.newFromList + (Token.Semicolon, Token.Colon, Token.RightParen, 0); + lines.Add("Creating Follow Set for: IDENT LIST \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* definition */ + set = TokenSet.newFromList(Token.END, 0); + lines.Add("Creating Follow Set for: DEFINITION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* constDefinition */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: CONST DEFINITION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* typeDefinition */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: TYPE DEFINITION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* type */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* derivedOrSubRangeType */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: DERIVED OR SUBRange TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* qualident */ + set = TokenSet.newFromList + (Token.Semicolon, Token.LeftBracket, Token.Comma, Token.OF, + Token.RightParen, Token.Assign, Token.LeftParen, Token.END, + Token.ELSIF, Token.ELSE, Token.Bar, Token.UNTIL, Token.LeftBrace, + Token.Asterisk, Token.Solidus, Token.DIV, Token.MOD, Token.AND, + Token.Plus, Token.Minus, Token.OR, Token.Equal, Token.NotEqual, + Token.Less, Token.LessEqual, Token.Greater, Token.GreaterEqual, + Token.IN, Token.THEN, Token.DO, Token.TO, Token.BY, + Token.Range, Token.RightBrace, 0); + lines.Add("Creating Follow Set for: QUALIDENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Range */ + set = TokenSet.newFromList + (Token.Semicolon, Token.Comma, Token.OF, 0); + lines.Add("Creating Follow Set for: Range \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* enumType */ + set = TokenSet.newFromList + (Token.Semicolon, Token.Comma, Token.OF, 0); + lines.Add("Creating Follow Set for: ENUM TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* setType */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: SET TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* countableType */ + set = TokenSet.newFromList + (Token.Semicolon, Token.Comma, Token.OF, 0); + lines.Add("Creating Follow Set for: COUNTABLE TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* arrayType */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: ARRAY TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* extensibleRecordType */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: EXTENSIBLE RECORD TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* fieldListSequence */ + set = TokenSet.newFromList(Token.END, Token.VAR, 0); + lines.Add("Creating Follow Set for: FIELD LIST SEQUENCE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* variantRecordType */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: VARIANT RECORD TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* variantFieldListSeq */ + set = TokenSet.newFromList + (Token.Bar, Token.ELSE, Token.END, 0); + lines.Add("Creating Follow Set for: VARIANT FIELD LIST SEQ \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* variantFieldList */ + set = TokenSet.newFromList + (Token.Semicolon, Token.Bar, Token.ELSE, Token.END, 0); + lines.Add("Creating Follow Set for: VARIANT FIELD LIST \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* variantFields */ + set = TokenSet.newFromList + (Token.Semicolon, Token.Bar, Token.ELSE, Token.END, 0); + lines.Add("Creating Follow Set for: VARIANT FIELDS \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* variant */ + set = TokenSet.newFromList + (Token.Bar, Token.ELSE, Token.END, 0); + lines.Add("Creating Follow Set for: VARIANT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* caseLabelList */ + set = TokenSet.newFromList(Token.Colon, 0); + lines.Add("Creating Follow Set for: CASE LABEL LIST \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* caseLabels */ + set = TokenSet.newFromList + (Token.Comma, Token.Colon, 0); + lines.Add("Creating Follow Set for: CASE LABELS \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* pointerType */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: POINTER TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* procedureType */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: PROCEDURE TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* simpleFormalType */ + set = TokenSet.newFromList + (Token.Comma, Token.RightParen, 0); + lines.Add("Creating Follow Set for: SIMPLE FORMAL TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* procedureHeader */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: PROCEDURE HEADER \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* procedureSignature */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: PROCEDURE SIGNATURE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* simpleFormalParams */ + set = TokenSet.newFromList + (Token.Semicolon, Token.RightParen, 0); + lines.Add("Creating Follow Set for: SIMPLE FORMAL PARAMS \\\n "); + lines.Add(set.PrintLiteral()); + lines.Add("\n"); + + /* implementationModule */ + set = TokenSet.newFromList(Token.EndOfFile, 0); + lines.Add("Creating Follow Set for: IMPLEMENTATION MODULE \\\n "); + lines.Add(set.PrintLiteral()); + lines.Add("\n"); + + /* programModule */ + set = TokenSet.newFromList(Token.EndOfFile, 0); + lines.Add("Creating Follow Set for: PROGRAM MODULE \\\n "); + lines.Add(set.PrintLiteral()); + lines.Add("\n"); + + /* modulePriority */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: MODULE PRIORITY \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* block */ + set = TokenSet.newFromList(Token.Identifier, 0); + lines.Add("Creating Follow Set for: BLOCK \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* declaration */ + set = TokenSet.newFromList(Token.BEGIN, Token.END, 0); + lines.Add("Creating Follow Set for: DECLARATION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* typeDeclaration */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: TYPE DECLARATION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* varSizeRecordType */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: VAR SIZE RECORD TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* variableDeclaration */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: VARIABLE DECLARATION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* procedureDeclaration */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: PROCEDURE DECLARATION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* moduleDeclaration */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: MODULE DECLARATION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* export */ + set = TokenSet.newFromList + (Token.CONST, Token.TYPE, Token.VAR, Token.PROCEDURE, Token.MODULE, + Token.BEGIN, Token.END, 0); + lines.Add("Creating Follow Set for: EXPORT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* statementSequence */ + set = TokenSet.newFromList + (Token.END, Token.ELSIF, Token.ELSE, Token.Bar, Token.UNTIL, 0); + lines.Add("Creating Follow Set for: STATEMENT SEQUENCE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* statement */ + set = TokenSet.newFromList + (Token.Semicolon, + Token.END, Token.ELSIF, Token.ELSE, Token.Bar, Token.UNTIL, 0); + lines.Add("Creating Follow Set for: STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* AssignmentOrProcCall */ + set = TokenSet.newFromList + (Token.Semicolon, + Token.END, Token.ELSIF, Token.ELSE, Token.Bar, Token.UNTIL, 0); + lines.Add("Creating Follow Set for: AssignMENT OR PROC CALL \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* actualParameters */ + set = TokenSet.newFromList + (Token.Semicolon, + Token.END, Token.ELSIF, Token.ELSE, Token.Bar, Token.UNTIL, 0); + lines.Add("Creating Follow Set for: ACTUAL PARAMETERS \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* expressionList */ + set = TokenSet.newFromList(Token.RightParen, 0); + lines.Add("Creating Follow Set for: EXPRESSION LIST \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* returnStatement */ + set = TokenSet.newFromList + (Token.Semicolon, + Token.END, Token.ELSIF, Token.ELSE, Token.Bar, Token.UNTIL, 0); + lines.Add("Creating Follow Set for: RETURN STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* withStatement */ + set = TokenSet.newFromList + (Token.Semicolon, + Token.END, Token.ELSIF, Token.ELSE, Token.Bar, Token.UNTIL, 0); + lines.Add("Creating Follow Set for: WITH STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* ifStatement */ + set = TokenSet.newFromList + (Token.Semicolon, + Token.END, Token.ELSIF, Token.ELSE, Token.Bar, Token.UNTIL, 0); + lines.Add("Creating Follow Set for: IF STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* caseStatement */ + set = TokenSet.newFromList + (Token.Semicolon, + Token.END, Token.ELSIF, Token.ELSE, Token.Bar, Token.UNTIL, 0); + lines.Add("Creating Follow Set for: CASE STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* case */ + set = TokenSet.newFromList + (Token.Bar, Token.ELSE, Token.END, 0); + lines.Add("Creating Follow Set for: CASE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* loopStatement */ + set = TokenSet.newFromList + (Token.Semicolon, + Token.END, Token.ELSIF, Token.ELSE, Token.Bar, Token.UNTIL, 0); + lines.Add("Creating Follow Set for: LOOP STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* whileStatement */ + set = TokenSet.newFromList + (Token.Semicolon, + Token.END, Token.ELSIF, Token.ELSE, Token.Bar, Token.UNTIL, 0); + lines.Add("Creating Follow Set for: WHILE STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* repeatStatement */ + set = TokenSet.newFromList + (Token.Semicolon, + Token.END, Token.ELSIF, Token.ELSE, Token.Bar, Token.UNTIL, 0); + lines.Add("Creating Follow Set for: REPEAT STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* forStatement */ + set = TokenSet.newFromList + (Token.Semicolon, + Token.END, Token.ELSIF, Token.ELSE, Token.Bar, Token.UNTIL, 0); + lines.Add("Creating Follow Set for: FOR STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* designator */ + set = TokenSet.newFromList + (Token.Assign, Token.LeftParen, Token.LeftBrace, + Token.Semicolon, Token.END, Token.ELSIF, Token.ELSE, Token.Bar, + Token.UNTIL, Token.Asterisk, Token.Solidus, Token.DIV, Token.MOD, + Token.AND, Token.Plus, Token.Minus, Token.OR, Token.Equal, + Token.NotEqual, Token.Less, Token.LessEqual, Token.Greater, + Token.GreaterEqual, Token.IN, Token.Comma, Token.RightParen, + Token.THEN, Token.OF, Token.DO, Token.TO, Token.BY, Token.Range, + Token.RightBrace, 0); + lines.Add("Creating Follow Set for: DESIGNATOR \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* selector */ + set = TokenSet.newFromList + (Token.Identifier, + Token.Assign, Token.LeftParen, Token.LeftBrace, + Token.Semicolon, Token.END, Token.ELSIF, Token.ELSE, Token.Bar, + Token.UNTIL, Token.Asterisk, Token.Solidus, Token.DIV, Token.MOD, + Token.AND, Token.Plus, Token.Minus, Token.OR, Token.Equal, + Token.NotEqual, Token.Less, Token.LessEqual, Token.Greater, + Token.GreaterEqual, Token.IN, Token.Comma, Token.RightParen, + Token.THEN, Token.OF, Token.DO, Token.TO, Token.BY, Token.Range, + Token.RightBrace, 0); + lines.Add("Creating Follow Set for: SELECTOR \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* expression */ + set = TokenSet.newFromList + (Token.Semicolon, Token.END, Token.ELSIF, Token.ELSE, Token.Bar, + Token.UNTIL, Token.Comma, Token.RightParen, Token.THEN, + Token.OF, Token.DO, Token.TO, Token.BY, Token.Range, + Token.RightBrace, 0); + lines.Add("Creating Follow Set for: EXPRESSION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* simpleExpression */ + set = TokenSet.newFromList + (Token.Equal, Token.NotEqual, Token.Less, Token.LessEqual, + Token.Greater, Token.GreaterEqual, Token.IN, + Token.Semicolon, Token.END, Token.ELSIF, Token.ELSE, Token.Bar, + Token.UNTIL, Token.Comma, Token.RightParen, Token.THEN, + Token.OF, Token.DO, Token.TO, Token.BY, Token.Range, + Token.RightBrace, 0); + lines.Add("Creating Follow Set for: SIMPLE EXPRESSION \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* term */ + set = TokenSet.newFromList + (Token.Plus, Token.Minus, Token.OR, + Token.Equal, Token.NotEqual, Token.Less, Token.LessEqual, + Token.Greater, Token.GreaterEqual, Token.IN, + Token.Semicolon, Token.END, Token.ELSIF, Token.ELSE, Token.Bar, + Token.UNTIL, Token.Comma, Token.RightParen, Token.THEN, + Token.OF, Token.DO, Token.TO, Token.BY, Token.Range, + Token.RightBrace, 0); + lines.Add("Creating Follow Set for: TERM \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* simpleTerm */ + set = TokenSet.newFromList + (Token.Asterisk, Token.Solidus, Token.DIV, Token.MOD, Token.AND, + Token.Plus, Token.Minus, Token.OR, + Token.Equal, Token.NotEqual, Token.Less, Token.LessEqual, + Token.Greater, Token.GreaterEqual, Token.IN, + Token.Semicolon, Token.END, Token.ELSIF, Token.ELSE, Token.Bar, + Token.UNTIL, Token.Comma, Token.RightParen, Token.THEN, + Token.OF, Token.DO, Token.TO, Token.BY, Token.Range, + Token.RightBrace, 0); + lines.Add("Creating Follow Set for: SIMPLE TERM \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* factor */ + set = TokenSet.newFromList + (Token.Asterisk, Token.Solidus, Token.DIV, Token.MOD, Token.AND, + Token.Plus, Token.Minus, Token.OR, + Token.Equal, Token.NotEqual, Token.Less, Token.LessEqual, + Token.Greater, Token.GreaterEqual, Token.IN, + Token.Semicolon, Token.END, Token.ELSIF, Token.ELSE, Token.Bar, + Token.UNTIL, Token.Comma, Token.RightParen, Token.THEN, + Token.OF, Token.DO, Token.TO, Token.BY, Token.Range, + Token.RightBrace, 0); + lines.Add("Creating Follow Set for: FACTOR \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* designatorOrFuncCall */ + set = TokenSet.newFromList + (Token.Asterisk, Token.Solidus, Token.DIV, Token.MOD, Token.AND, + Token.Plus, Token.Minus, Token.OR, + Token.Equal, Token.NotEqual, Token.Less, Token.LessEqual, + Token.Greater, Token.GreaterEqual, Token.IN, + Token.Semicolon, Token.END, Token.ELSIF, Token.ELSE, Token.Bar, + Token.UNTIL, Token.Comma, Token.RightParen, Token.THEN, + Token.OF, Token.DO, Token.TO, Token.BY, Token.Range, + Token.RightBrace, 0); + lines.Add("Creating Follow Set for: DESIGNATOR OR FUNC CALL \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* setValue */ + set = TokenSet.newFromList + (Token.Asterisk, Token.Solidus, Token.DIV, Token.MOD, Token.AND, + Token.Plus, Token.Minus, Token.OR, + Token.Equal, Token.NotEqual, Token.Less, Token.LessEqual, + Token.Greater, Token.GreaterEqual, Token.IN, + Token.Semicolon, Token.END, Token.ELSIF, Token.ELSE, Token.Bar, + Token.UNTIL, Token.Comma, Token.RightParen, Token.THEN, + Token.OF, Token.DO, Token.TO, Token.BY, Token.Range, + Token.RightBrace, 0); + lines.Add("Creating Follow Set for: SET VALUE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* element */ + set = TokenSet.newFromList + (Token.Comma, Token.RightBrace, 0); + lines.Add("Creating Follow Set for: ELEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Option dependent FOLLOW sets */ + + /* formalType */ + set = TokenSet.newFromList + (Token.Comma, Token.RightParen, 0); + lines.Add("Creating Follow Set for: FORMAL TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* attributedFormalType */ + set = TokenSet.newFromList + (Token.Comma, Token.RightParen, 0); + lines.Add("Creating Follow Set for: ATTRIBUTED FORMAL TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* formalParamList */ + set = TokenSet.newFromList(Token.RightParen, 0); + lines.Add("Creating Follow Set for: FORMAL PARAM LIST \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* formalParams */ + set = TokenSet.newFromList + (Token.Semicolon, Token.RightParen, 0); + lines.Add("Creating Follow Set for: FORMAL PARAMS \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* attribFormalParams */ + set = TokenSet.newFromList + (Token.Semicolon, Token.RightParen, 0); + lines.Add("Creating Follow Set for: ATTRIB FORMAL PARAMS \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* typeDeclarationTail */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: TYPE DECLARATION TAIL \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Alternative FOLLOW sets */ + + /* Alternative formalType */ + set = TokenSet.newFromList + (Token.Comma, Token.RightParen, 0); + lines.Add("Creating Follow Set for: ALT FORMAL TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Alternative attributedFormalType */ + set = TokenSet.newFromList + (Token.Comma, Token.RightParen, 0); + lines.Add("Creating Follow Set for: ALT ATTRIB FORMAL TYPE \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Alternative formalParamList */ + set = TokenSet.newFromList(Token.RightParen, 0); + lines.Add("Creating Follow Set for: ALT FORMAL PARAM LIST \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Alternative formalParams */ + set = TokenSet.newFromList + (Token.Semicolon, Token.RightParen, 0); + lines.Add("Creating Follow Set for: ALT FORMAL PARAMS \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Alternative attribFormalParams */ + set = TokenSet.newFromList + (Token.Semicolon, Token.RightParen, 0); + lines.Add("Creating Follow Set for: ALT ATTRIB FORMAL PARAMS \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Alternative typeDeclarationTail */ + set = TokenSet.newFromList(Token.Semicolon, 0); + lines.Add("Creating Follow Set for: ALT TYPE DECL TAIL \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + return lines; + } /* end genSets */ + + } /* end GenFollowSets */ + +} /* end namespace */ diff --git a/utils/GenResyncSets.cs b/utils/GenResyncSets.cs new file mode 100644 index 0000000..149877e --- /dev/null +++ b/utils/GenResyncSets.cs @@ -0,0 +1,286 @@ +/* M2Sharp -- Modula-2 to C# Translator & Compiler + * + * Copyright (c) 2016 The Modula-2 Software Foundation + * + * Author & Maintainer: Benjamin Kowarsch + * + * @synopsis + * + * M2Sharp is a multi-dialect Modula-2 to C# translator and via-C# compiler. + * It supports the dialects described in the 3rd and 4th editions of Niklaus + * Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag, + * and an extended mode with select features from the revised language by + * B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10). + * + * In translator mode, M2Sharp translates Modula-2 source to C# source files. + * In compiler mode, M2Sharp compiles Modula-2 source via C# source files + * to object code or executables using the host system's C# compiler. + * + * @repository + * + * https://github.com/m2sf/m2sharp + * + * @file + * + * GenResyncSets.cs + * + * A class dedicated to producing representations of the skip sets used by the parser. + * + * @license + * + * M2Sharp is free software: you can redistribute and/or modify it under the + * terms of the GNU Lesser General Public License (LGPL) either version 2.1 + * or at your choice version 3 as published by the Free Software Foundation. + * However, you may not alter the copyright, author and license information. + * + * M2Sharp is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with M2Sharp. If not, see . + * + * NB: Components in the domain part of email addresses are in reverse order. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace org.m2sf.m2sharp +{ + class GenResyncSets + { + public static List genSets() + { + + TokenSet set; + List lines = new List(); + + lines.Add("/* M2C resync set initialisers" + + " -- generated by gen resync sets.c */\n\n"); + + /* Resync to Resync(import) or Resync(definition) or END */ + + set = TokenSet.newFromList + (/* Resync(import) */ + Token.IMPORT, Token.FROM, + /* Resync(definition) */ + Token.CONST, Token.TYPE, Token.VAR, Token.PROCEDURE, + /* END */ + Token.END, + /* EOF */ + Token.EndOfFile, 0); + lines.Add("Creating Resync Set: IMPORT OR DEFINITON OR END \\\n "); + lines.Add(set.PrintLiteral()); + lines.Add("\n"); + + /* Resync to IMPORT or Ident or Semicolon */ + + set = TokenSet.newFromList + (Token.IMPORT, Token.Identifier, Token.Semicolon, + /* EOF -- in case nothing else matches */ + Token.EndOfFile, 0); + lines.Add("Creating Resync Set: IMPORT OR IDENT OR Semicolon \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Resync to Ident or Semicolon */ + + set = TokenSet.newFromList + (Token.Identifier, Token.Semicolon, + /* EOF -- in case nothing else matches */ + Token.EndOfFile, 0); + lines.Add("Creating Resync Set: IDENT OR Semicolon \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Resync to Comma or Semicolon */ + + set = TokenSet.newFromList + (Token.Comma, Token.Semicolon, + /* EOF -- in case nothing else matches */ + Token.EndOfFile, 0); + lines.Add("Creating Resync Set: Comma OR Semicolon \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Resync to Resync(definition) or Ident or Semicolon */ + + set = TokenSet.newFromList + (/* Resync(definition) */ + Token.CONST, Token.TYPE, Token.VAR, Token.PROCEDURE, + /* Ident or Semicolon */ + Token.Identifier, Token.Semicolon, + /* EOF -- in case nothing else matches */ + Token.EndOfFile, 0); + lines.Add("Creating Resync Set: DEFINITION OR IDENT OR Semicolon \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Resync to Resync(definition) or Semicolon */ + + set = TokenSet.newFromList + (/* Resync(definition) */ + Token.CONST, Token.TYPE, Token.VAR, Token.PROCEDURE, + /* Semicolon */ + Token.Semicolon, + /* EOF -- in case nothing else matches */ + Token.EndOfFile, 0); + lines.Add("Creating Resync Set: DEFINITION OR Semicolon \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Resync to Resync(type) or Comma or OF */ + + set = TokenSet.newFromList + (/* Resync(type) */ + Token.ARRAY, Token.POINTER, Token.PROCEDURE, Token.RECORD, Token.SET, + Token.Identifier, Token.LeftParen, Token.LeftBracket, + /* Comma or OF */ + Token.Comma, Token.OF, + /* EOF -- in case nothing else matches */ + Token.EndOfFile, 0); + lines.Add("Creating Resync Set: TYPE OR Comma OR OF \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Resync to Semicolon or END */ + + set = TokenSet.newFromList + (Token.Semicolon, Token.END, + /* EOF -- in case nothing else matches */ + Token.EndOfFile, 0); + lines.Add("Creating Resync Set: Semicolon OR END \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Resync to ELSE or END */ + + set = TokenSet.newFromList + (Token.ELSE, Token.END, + /* EOF -- in case nothing else matches */ + Token.EndOfFile, 0); + lines.Add("Creating Resync Set: ELSE OR END \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Resync to Comma or RightParen */ + + set = TokenSet.newFromList + (Token.Comma, Token.RightParen, + /* EOF -- in case nothing else matches */ + Token.EndOfFile, 0); + lines.Add("Creating " + + "Resync Set: Comma OR RightParen \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Resync to Colon or Semicolon */ + + set = TokenSet.newFromList + (Token.Colon, Token.Semicolon, + /* EOF -- in case nothing else matches */ + Token.EndOfFile, 0); + lines.Add("Creating Resync Set: Colon OR Semicolon \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Resync to Resync(import) or Resync(block) */ + + set = TokenSet.newFromList + (/* Resync(import) */ + Token.IMPORT, Token.FROM, + /* Resync(block) */ + Token.CONST, Token.TYPE, Token.VAR, Token.PROCEDURE, Token.MODULE, + Token.BEGIN, Token.END, + /* EOF -- in case nothing else matches */ + Token.EndOfFile, 0); + lines.Add("Creating Resync Set: IMPORT OR BLOCK \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Resync to Resync(declaration) or Ident or Semicolon */ + + set = TokenSet.newFromList + (/* Resync(declaration) */ + Token.CONST, Token.TYPE, Token.VAR, Token.PROCEDURE, Token.MODULE, + /* Ident or Semicolon */ + Token.Identifier, Token.Semicolon, + /* EOF -- in case nothing else matches */ + Token.EndOfFile, 0); + lines.Add("Creating Resync Set: DECLARATION OR IDENT OR Semicolon \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Resync to Resync(declaration) or Semicolon */ + + set = TokenSet.newFromList + (/* Resync(declaration) */ + Token.CONST, Token.TYPE, Token.VAR, Token.PROCEDURE, Token.MODULE, + /* Semicolon */ + Token.Semicolon, + /* EOF -- in case nothing else matches */ + Token.EndOfFile, 0); + lines.Add("Creating Resync Set: DECLARATION OR Semicolon \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Resync to Resync(statement) or FOLLOW(statement) */ + + set = TokenSet.newFromList + (/* Resync(statement) */ + Token.Identifier, Token.RETURN, Token.WITH, Token.IF, Token.CASE, + Token.LOOP, Token.WHILE, Token.REPEAT, Token.FOR, Token.EXIT, + /* FOLLOW(statement) */ + Token.Semicolon, Token.END, Token.ELSIF, Token.ELSE, Token.Bar, + Token.UNTIL, + /* EOF -- in case nothing else matches */ + Token.EndOfFile, 0); + lines.Add("Creating Resync Set: FIRST OR FOLLOW OF STATEMENT \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Resync to ELSIF or ELSE or END */ + + set = TokenSet.newFromList + (Token.ELSIF, Token.ELSE, Token.END, + /* EOF -- in case nothing else matches */ + Token.EndOfFile, 0); + lines.Add("Creating Resync Set: ELSIF OR ELSE OR END \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + /* Resync to DO in FOR loop */ + + set = TokenSet.newFromList + (Token.DO, + /* EOF -- in case nothing else matches */ + Token.EndOfFile, 0); + lines.Add("Creating Resync Set: FOR LOOP BODY \\\n "); + lines.Add(set.PrintLiteral()); + + lines.Add("\n"); + + return lines; + } + + } +}