Esempio n. 1
0
        public JsonGrammar(Domain domain)
            : base(domain)
        {
            B = new BnfTerms(new TerminalFactoryS(this));

            this.Root = B.Object;

            B.Object.Rule =
                B.NUMBER + SetUnparsePriority((astValue, childAstValues) => astValue is int || astValue is double?(int?)1 : null)
                |
                B.STRING
                |
                B.BOOLEAN
                |
                B.Array
                |
                B.NULL + SetUnparsePriority((astValue, childAstValues) => astValue == null ? (int?)1 : null)
                |
                B.OBJECT_BEGIN
                + B.KeyValuePairs.ConvertValue(KeyValuePairsToObject, ObjectToKeyValuePairs)
                + B.OBJECT_END
            ;

            B.KeyValuePairs.Rule =
                B.KeyValuePair.PlusList(B.COMMA)
            ;

            B.KeyValuePair.Rule =
                B.Key.BindTo(B.KeyValuePair, fv => fv.Key)
                + B.COLON
                + B.Value.BindTo(B.KeyValuePair, fv => fv.Value);

            B.Key.Rule =
                B.STRING
            ;

            B.Value.Rule =
                B.Object
            ;

            B.Array.Rule =
                B.ARRAY_BEGIN
                + B.ArrayElements.BindTo(B.Array, metaArray => metaArray.Elements)
                + B.ARRAY_END
            ;

            B.ArrayElements.Rule =
                B.Object.StarListTL(B.COMMA)
            ;

            RegisterBracePair(B.OBJECT_BEGIN, B.OBJECT_END);
            RegisterBracePair(B.ARRAY_BEGIN, B.ARRAY_END);

            // TODO: although JSON does not have comments, we define C-style comments for until we solve parsing and unparsing of comments into JSON data
            CommentTerminal DelimitedComment  = new CommentTerminal("DelimitedComment", "/*", "*/");
            CommentTerminal SingleLineComment = new CommentTerminal("SingleLineComment", "//", Environment.NewLine, "\n", "\r");

            NonGrammarTerminals.Add(DelimitedComment);
            NonGrammarTerminals.Add(SingleLineComment);
        }
Esempio n. 2
0
        public BackOGrammar_old() : base(false)
        {
            var lineComment = new CommentTerminal("line_comment", "--", "\n", "\r\n");

            NonGrammarTerminals.Add(lineComment);

            var number         = new NumberLiteral("number");
            var string_literal = new StringLiteral("string", "'", StringOptions.AllowsDoubledQuote);
            var Id_simple      = TerminalFactory.CreateSqlExtIdentifier(this, "id_simple");        //covers normal identifiers (abc) and quoted id's ([abc d], "abc d")
            var comma          = ToTerm(",");
            var dot            = ToTerm(".");
            var CREATE         = ToTerm("CREATE");
            var NULL           = ToTerm("NULL");
            var NOT            = ToTerm("NOT");
            //var ADD = ToTerm("ADD");
            //var COLUMN = ToTerm("COLUMN");
            var UPDATE = ToTerm("UPDATE");
            var SET    = ToTerm("SET");
            var DELETE = ToTerm("DELETE");
            var SELECT = ToTerm("SELECT");
            var FROM   = ToTerm("FROM");
            var COUNT  = ToTerm("COUNT");
            var JOIN   = ToTerm("JOIN");
            //var BY = ToTerm("BY");
        }
        public GramaticaCadena() : base(caseSensitive: false)
        {
            RegexBasedTerminal numero        = new RegexBasedTerminal("numero", "-?[0-9]+(\\?[0-9])?");
            RegexBasedTerminal numerodecimal = new RegexBasedTerminal("decimanl", "[0-9]+[.][0-9]+");
            CommentTerminal    cadena        = new CommentTerminal("string", "\"", ".", "\"");   // es una cadena String
            CommentTerminal    r_char        = new CommentTerminal("caracteres", "'", ".", "'"); // es un caracter char

            #region No Terminales
            var         mas        = ToTerm("+");
            NonTerminal S          = new NonTerminal("S"),
                        SUMACADENA = new NonTerminal("SUMACADENA");
            #endregion

            #region Gramatica
            //Gramatica ambigua:
            S.Rule          = SUMACADENA;
            SUMACADENA.Rule = cadena;

            /* cadena + mas + cadena
             | cadena + mas + numero
             | numero + mas + cadena
             | cadena + mas + numerodecimal
             | numerodecimal + mas + cadena
             | cadena + mas + r_char
             | r_char + mas + cadena;*/
            #endregion

            #region Preferencias
            this.Root = S;
            #endregion
        }
Esempio n. 4
0
        public ASLGrammar()
            : base(true)
        {
            var string_lit = TerminalFactory.CreateCSharpString("string");
            var number     = TerminalFactory.CreateCSharpNumber("number");
            var identifier = TerminalFactory.CreateCSharpIdentifier("identifier");
            var code       = new CustomTerminal("code", MatchCodeTerminal);

            var single_line_comment = new CommentTerminal("SingleLineComment", "//", "\r", "\n", "\u2085", "\u2028", "\u2029");
            var delimited_comment   = new CommentTerminal("DelimitedComment", "/*", "*/");

            NonGrammarTerminals.Add(single_line_comment);
            NonGrammarTerminals.Add(delimited_comment);

            var state     = new KeyTerm("state", "state");
            var init      = new KeyTerm("init", "init");
            var exit      = new KeyTerm("exit", "exit");
            var update    = new KeyTerm("update", "update");
            var start     = new KeyTerm("start", "start");
            var split     = new KeyTerm("split", "split");
            var reset     = new KeyTerm("reset", "reset");
            var startup   = new KeyTerm("startup", "startup");
            var shutdown  = new KeyTerm("shutdown", "shutdown");
            var isLoading = new KeyTerm("isLoading", "isLoading");
            var gameTime  = new KeyTerm("gameTime", "gameTime");
            var comma     = ToTerm(",", "comma");
            var semi      = ToTerm(";", "semi");

            var root        = new NonTerminal("root");
            var state_def   = new NonTerminal("stateDef");
            var version     = new NonTerminal("version");
            var state_list  = new NonTerminal("stateList");
            var method_list = new NonTerminal("methodList");
            var var_list    = new NonTerminal("varList");
            var var         = new NonTerminal("var");
            var module      = new NonTerminal("module");
            var method      = new NonTerminal("method");
            var offset_list = new NonTerminal("offsetList");
            var offset      = new NonTerminal("offset");
            var method_type = new NonTerminal("methodType");

            root.Rule        = state_list + method_list;
            version.Rule     = (comma + string_lit) | Empty;
            state_def.Rule   = state + "(" + string_lit + version + ")" + "{" + var_list + "}";
            state_list.Rule  = MakeStarRule(state_list, state_def);
            method_list.Rule = MakeStarRule(method_list, method);
            var_list.Rule    = MakeStarRule(var_list, semi, var);
            module.Rule      = (string_lit + comma) | Empty;
            var.Rule         = (identifier + identifier + ":" + module + offset_list) | Empty;
            method.Rule      = (method_type + "{" + code + "}") | Empty;
            offset_list.Rule = MakePlusRule(offset_list, comma, offset);
            offset.Rule      = number;
            method_type.Rule = init | exit | update | start | split | isLoading | gameTime | reset | startup | shutdown;

            Root = root;

            MarkTransient(var_list, method_list, offset, method_type);

            LanguageFlags = LanguageFlags.NewLineBeforeEOF;
        }
Esempio n. 5
0
        private string[] CommentTextToDomainCommentTextLines(Token comment, out bool isDecorated)
        {
            CommentTerminal commentTerminal = (CommentTerminal)comment.Terminal;

            CommentKind commentKind = GrammarHelper.GetCommentKind(commentTerminal, GetCommentCleaner(commentTerminal).NewLine);

            string startSymbol = commentTerminal.StartSymbol;

            string endSymbol = commentKind == CommentKind.Delimited
                ? commentTerminal.EndSymbols.First(_endSymbol => comment.Text.EndsWith(_endSymbol))
                : string.Empty;

            string text = comment.Text;

            text = text
                   .Remove(text.Length - endSymbol.Length, endSymbol.Length)
                   .Remove(0, startSymbol.Length);

            var commentCleaner = GetCommentCleaner(commentTerminal);

            // NOTE: we handle "\n" and "\r" as well to deal with wrongly formatted input
            string[] textLines = text.Split(new[] { commentCleaner.NewLine, "\r", "\n" }, StringSplitOptions.None);
            textLines = commentCleaner.GetCleanedUpCommentTextLines(textLines, comment.Location.Column, commentTerminal, out isDecorated);

            return(textLines);
        }
Esempio n. 6
0
        public XmlGrammar()
            : base(false)
        {
            //Terminals
            Terminal comment = new CommentTerminal("comment", "<!--", "-->");

            NonGrammarTerminals.Add(comment);
            StringLiteral stringLiteral          = new StringLiteral("string", "\"", StringOptions.None);
            Terminal      stringContent          = new ToTerminatorTerminal("StringContent", '<');
            KeyTerm       elementOpener          = ToTerm("<");
            KeyTerm       closeElementOpener     = ToTerm("</");
            KeyTerm       elementCloser          = ToTerm(">");
            KeyTerm       openCloseElementCloser = ToTerm("/>");
            KeyTerm       equals       = ToTerm("=");
            KeyTerm       xmlDeclOpen  = ToTerm("<?");
            KeyTerm       xmlDeclClose = ToTerm("?>");

            IdentifierTerminal attributeIdentifier      = new IdentifierTerminal(AttributeIdentifier, ".", ".");
            IdentifierTerminal elementIdentifier        = new IdentifierTerminal(ElementIdentifier, ".-", ".-");
            KeyTerm            xmlDeclarationIdentifier = ToTerm("xml");

            //Non Terminals
            NonTerminal document               = new NonTerminal("document");
            NonTerminal elementStart           = new NonTerminal("ElementStart");
            NonTerminal elementEnd             = new NonTerminal("ElementEnd");
            NonTerminal openCloseElement       = new NonTerminal("OpenCloseElement");
            NonTerminal element                = new NonTerminal("Element");
            NonTerminal requiredElements       = new NonTerminal("RequiredElements");
            NonTerminal innerContent           = new NonTerminal("InnerContent");
            NonTerminal content                = new NonTerminal("Content");
            NonTerminal attribute              = new NonTerminal("Attribute");
            NonTerminal optionalAttribute      = new NonTerminal("OptionalAttribute");
            NonTerminal xmlDeclaration         = new NonTerminal("XmlDeclaration");
            NonTerminal optionalXmlDeclaration = new NonTerminal("OptionalXmlDeclaration");

            //Rules
            this.Root = document;

            innerContent.Rule = element | stringContent;
            content.Rule      = MakeStarRule(content, innerContent);

            attribute.Rule         = attributeIdentifier + equals + stringLiteral;
            optionalAttribute.Rule = MakeStarRule(optionalAttribute, attribute);

            elementStart.Rule     = elementOpener + elementIdentifier + optionalAttribute + elementCloser;
            elementEnd.Rule       = closeElementOpener + elementIdentifier + elementCloser;
            openCloseElement.Rule = elementOpener + elementIdentifier + optionalAttribute + openCloseElementCloser;

            element.Rule          = (elementStart + content + elementEnd) | openCloseElement;
            requiredElements.Rule = MakePlusRule(requiredElements, element);

            xmlDeclaration.Rule         = xmlDeclOpen + xmlDeclarationIdentifier + optionalAttribute + xmlDeclClose;
            optionalXmlDeclaration.Rule = MakeStarRule(optionalXmlDeclaration, xmlDeclaration);

            document.Rule = optionalXmlDeclaration + requiredElements;

            MarkPunctuation(elementOpener, elementCloser, closeElementOpener, openCloseElementCloser, equals, xmlDeclOpen, xmlDeclClose);
            MarkTransient(innerContent);
        }
Esempio n. 7
0
        private void Comments()
        {
            var comment     = new CommentTerminal("multiline_comment", "/*", "*/");
            var lineComment = new CommentTerminal("singleline_comment", "--", "\n", "\r\n");

            NonGrammarTerminals.Add(comment);
            NonGrammarTerminals.Add(lineComment);
        }
        public Gramatica_LUP() : base(caseSensitive: true)
        {
            #region Expresiones_Regulares
            CommentTerminal CONTENIDOUSER = new CommentTerminal("CONTENIDOUSER", "[+USER]", "[-USER]");
            CommentTerminal CONTENIDOPASS = new CommentTerminal("CONTENIDOPASS", "[+PASS]", "[-PASS]");
            CommentTerminal CONTENIDODATA = new CommentTerminal("CONTENIDODATA", "[+DATA]", "[-DATA]");

            #endregion
            #region Terminales
            //var USER = ToTerm("user");
            var MAS    = ToTerm("+");
            var MENOS  = ToTerm("-");
            var QUERY  = ToTerm("QUERY");
            var LOGOUT = ToTerm("LOGOUT");
            var LOGIN  = ToTerm("LOGIN");
            //var PASS = ToTerm("pass");
            //var DATA = ToTerm("data");
            var STRUCT    = ToTerm("STRUCT");
            var ABRIRCOR  = ToTerm("[");
            var CERRARCOR = ToTerm("]");

            #endregion
            #region NO_Terminales
            NonTerminal S              = new NonTerminal("S");
            NonTerminal ETIQUETALOGIN  = new NonTerminal("ETIQUETALOGIN");
            NonTerminal ETIQUETALOGOUT = new NonTerminal("ETIQUETALOGOUT");

            NonTerminal ETIQUETAUSER   = new NonTerminal("ETIQUETAUSER");
            NonTerminal ETIQUETAPASS   = new NonTerminal("ETIQUETAPASS");
            NonTerminal ETIQUETADATA   = new NonTerminal("ETIQUETADATA");
            NonTerminal ETIQUETASTRUCT = new NonTerminal("ETIQUETASTRUCT");
            NonTerminal ETIQUETAQUERY  = new NonTerminal("ETIQUETAQUERY");
            NonTerminal ETIQUETA       = new NonTerminal("ETIQUETA");
            NonTerminal L_ETIQUETAS    = new NonTerminal("L_ETIQUETAS");
            #endregion
            #region Gramatica
            S.Rule = L_ETIQUETAS;

            L_ETIQUETAS.Rule = MakePlusRule(L_ETIQUETAS, ETIQUETA);

            ETIQUETA.Rule = ETIQUETALOGIN
                            | ETIQUETALOGOUT
                            | ETIQUETASTRUCT
                            | ETIQUETAQUERY;
            ETIQUETALOGIN.Rule  = ABRIRCOR + MAS + LOGIN + CERRARCOR + ETIQUETAUSER + ETIQUETAPASS + ABRIRCOR + MENOS + LOGIN + CERRARCOR;
            ETIQUETALOGOUT.Rule = ABRIRCOR + MAS + LOGOUT + CERRARCOR + ETIQUETAUSER + ABRIRCOR + MENOS + LOGOUT + CERRARCOR;
            ETIQUETASTRUCT.Rule = ABRIRCOR + MAS + STRUCT + CERRARCOR + ETIQUETAUSER + ABRIRCOR + MENOS + STRUCT + CERRARCOR;
            ETIQUETAQUERY.Rule  = ABRIRCOR + MAS + QUERY + CERRARCOR + ETIQUETAUSER + ETIQUETADATA + ABRIRCOR + MENOS + QUERY + CERRARCOR;

            ETIQUETAUSER.Rule = CONTENIDOUSER;
            ETIQUETAPASS.Rule = CONTENIDOPASS;
            ETIQUETADATA.Rule = CONTENIDODATA;
            #endregion

            #region Preferencias
            this.Root = S;
            #endregion
        }
        public ExpressionEvaluatorGrammar()
        {
            // 1. Terminals
            var number = new NumberLiteral("number");

            //Let's allow big integers (with unlimited number of digits):
            number.DefaultIntTypes = new TypeCode[] { TypeCode.Int32, TypeCode.Int64, NumberLiteral.TypeCodeBigInt };
            var identifier = new IdentifierTerminal("identifier");
            var comment    = new CommentTerminal("comment", "#", "\n", "\r");

            //comment must to be added to NonGrammarTerminals list; it is not used directly in grammar rules,
            // so we add it to this list to let Scanner know that it is also a valid terminal.
            base.NonGrammarTerminals.Add(comment);
            // 2. Non-terminals
            var Expr           = new NonTerminal("Expr");
            var Term           = new NonTerminal("Term");
            var BinExpr        = new NonTerminal("BinExpr", typeof(BinExprNode));
            var ParExpr        = new NonTerminal("ParExpr");
            var UnExpr         = new NonTerminal("UnExpr", typeof(UnExprNode));
            var UnOp           = new NonTerminal("UnOp");
            var BinOp          = new NonTerminal("BinOp", "operator");
            var PostFixExpr    = new NonTerminal("PostFixExpr", typeof(UnExprNode));
            var PostFixOp      = new NonTerminal("PostFixOp");
            var AssignmentStmt = new NonTerminal("AssignmentStmt", typeof(AssigmentNode));
            var AssignmentOp   = new NonTerminal("AssignmentOp", "assignment operator");
            var Statement      = new NonTerminal("Statement");
            var ProgramLine    = new NonTerminal("ProgramLine");
            var Program        = new NonTerminal("Program", typeof(StatementListNode));

            // 3. BNF rules
            Expr.Rule           = Term | UnExpr | BinExpr | PostFixExpr;
            Term.Rule           = number | ParExpr | identifier;
            ParExpr.Rule        = "(" + Expr + ")";
            UnExpr.Rule         = UnOp + Term;
            UnOp.Rule           = ToTerm("+") | "-" | "++" | "--";
            BinExpr.Rule        = Expr + BinOp + Expr;
            BinOp.Rule          = ToTerm("+") | "-" | "*" | "/" | "**";
            PostFixExpr.Rule    = Term + PostFixOp;
            PostFixOp.Rule      = ToTerm("++") | "--";
            AssignmentStmt.Rule = identifier + AssignmentOp + Expr;
            AssignmentOp.Rule   = ToTerm("=") | "+=" | "-=" | "*=" | "/=";
            Statement.Rule      = AssignmentStmt | Expr | Empty;
            ProgramLine.Rule    = Statement + NewLine;
            Program.Rule        = MakeStarRule(Program, ProgramLine);
            this.Root           = Program; // Set grammar root
            // 4. Operators precedence
            RegisterOperators(1, "+", "-");
            RegisterOperators(2, "*", "/");
            RegisterOperators(3, Associativity.Right, "**");
            // 5. Punctuation and transient terms
            RegisterPunctuation("(", ")");
            RegisterBracePair("(", ")");
            MarkTransient(Term, Expr, Statement, BinOp, UnOp, PostFixOp, AssignmentOp, ProgramLine, ParExpr);
            //automatically add NewLine before EOF so that our BNF rules work correctly when there's no final line break in source
            this.LanguageFlags = LanguageFlags.CreateAst | LanguageFlags.NewLineBeforeEOF | LanguageFlags.CanRunSample;
        }
Esempio n. 10
0
        public IntegrationTestGrammar()
        {
            var comment = new CommentTerminal("comment", "/*", "*/");

            base.NonGrammarTerminals.Add(comment);
            var str  = new StringLiteral("str", "'", StringOptions.AllowsLineBreak);
            var stmt = new NonTerminal("stmt");

            stmt.Rule = str | Empty;
            this.Root = stmt;
        }
        public VASLGrammar() : base(true)
        {
            var code       = new CustomTerminal("code", MatchCodeTerminal);
            var stringLit  = TerminalFactory.CreateCSharpString("string");
            var identifier = TerminalFactory.CreateCSharpIdentifier("identifier");
            var number     = TerminalFactory.CreateCSharpNumber("number");

            number.Options |= NumberOptions.AllowSign;

            var singleLineComment = new CommentTerminal("SingleLineComment", "//", "\r", "\n", "\u2085", "\u2028", "\u2029");
            var delimitedComment  = new CommentTerminal("DelimitedComment", "/*", "*/");

            NonGrammarTerminals.Add(singleLineComment);
            NonGrammarTerminals.Add(delimitedComment);

            // Todo: Aliases

            var init      = new KeyTerm("init", "init");
            var exit      = new KeyTerm("exit", "exit");
            var update    = new KeyTerm("update", "update");
            var start     = new KeyTerm("start", "start");
            var split     = new KeyTerm("split", "split");
            var reset     = new KeyTerm("reset", "reset");
            var startup   = new KeyTerm("startup", "startup");
            var shutdown  = new KeyTerm("shutdown", "shutdown");
            var undoSplit = new KeyTerm("undoSplit", "undoSplit");
            var isLoading = new KeyTerm("isLoading", "isLoading");
            var gameTime  = new KeyTerm("gameTime", "gameTime");
            var comma     = ToTerm(",", "comma");
            var semi      = ToTerm(";", "semi");

            var root       = new NonTerminal("root");
            var version    = new NonTerminal("version");
            var methodList = new NonTerminal("methodList");
            var varList    = new NonTerminal("varList");
            var var        = new NonTerminal("var");
            var method     = new NonTerminal("method");
            var methodType = new NonTerminal("methodType");

            root.Rule       = methodList;
            version.Rule    = (comma + stringLit) | Empty;
            methodList.Rule = MakeStarRule(methodList, method);
            varList.Rule    = MakeStarRule(varList, semi, var);
            method.Rule     = (methodType + "{" + code + "}") | Empty;
            methodType.Rule = init | exit | update | start | split | isLoading | gameTime | reset | startup | shutdown | undoSplit;

            Root = root;

            MarkTransient(varList, methodList, methodType);

            LanguageFlags = LanguageFlags.NewLineBeforeEOF;
        }
Esempio n. 12
0
        /// <summary>
        /// Creates an Irony Grammar
        /// </summary>
        public CRLGrammar() : base(caseSensitive: false)
        {
            //Comentarios
            var singleLineComment = new CommentTerminal("SingleLineComment", "!!", "\r", "\n", "\u2085", "\u2028", "\u2029");
            var delimitedComment  = new CommentTerminal("DelimitedComment", "<<", ">>");

            NonGrammarTerminals.Add(singleLineComment);
            NonGrammarTerminals.Add(delimitedComment);

            MarkPunctuation(";", ",", "(", ")", "{", "}", "=", ":");

            InitializeSyntax();
        }
Esempio n. 13
0
        private readonly TerminalSet mSkipTokensInPreview = new TerminalSet();         //used in token preview for conflict resolution

        public JavaGrammar()
        {
            GrammarComments = "NOTE: This grammar does not parse hex floating point literals.";

            var singleLineComment = new CommentTerminal("SingleLineComment", "//", "\r", "\n", "\u2085", "\u2028", "\u2029");
            var delimitedComment  = new CommentTerminal("DelimitedComment", "/*", "*/");

            NonGrammarTerminals.Add(singleLineComment);
            NonGrammarTerminals.Add(delimitedComment);

            MarkPunctuation(";", ",", "(", ")", "{", "}", "[", "]", ":", "@");

            InitializeSyntax();
        }
Esempio n. 14
0
        private Comment CommentToDomainComment(Token comment, ParseTreeNode parseTreeNodeOwner, CommentPlacement placement, int lineIndexDistanceFromOwner)
        {
            CommentTerminal commentTerminal = (CommentTerminal)comment.Terminal;
            bool            isDecorated;

            return(new Comment(
                       CommentTextToDomainCommentTextLines(comment, out isDecorated),
                       CommentCategoryToDomainCommentCategory(comment.Category),
                       placement,
                       lineIndexDistanceFromOwner,
                       GrammarHelper.GetCommentKind(commentTerminal, GetCommentCleaner(commentTerminal).NewLine),
                       isDecorated
                       ));
        }
Esempio n. 15
0
        public NumericalSchemeGrammar()
        {
            this.GrammarComments = "";

            //Symbols
            var whitespace = new NonTerminal("WhiteSpace", Empty | " " | "\t");
            // Comments
            var singleLineComment = new CommentTerminal("SingleLineComment", "//", "\r", "\n", "\u2085", "\u2028", "\u2029");
            var multiLineComment  = new CommentTerminal("DelimitedComment", "/*", "*/");

            NonGrammarTerminals.Add(singleLineComment);
            NonGrammarTerminals.Add(multiLineComment);

            //Terminals
            var semicolon    = ToTerm(";", "Semicolon");
            var ofString     = new StringLiteral("String", "\"");
            var ofNumber     = new NumberLiteral("Number", NumberOptions.AllowSign);
            var ofIdentifier = TerminalFactory.CreateCSharpIdentifier("Identifier");
            var ofArrayTerm  = new IdentifierTerminal("SchemeTerm", "(),|*.+-/%^~!&");

            //Non-terminals
            var ofValue = new NonTerminal("Value");

            var ofDictionary               = new NonTerminal("Dictionary");
            var ofDictionaryContent        = new NonTerminal("DictionaryContent");
            var ofDictionaryContentWrapper = new NonTerminal("DictionaryContentWrapper");
            var ofDictEntry = new NonTerminal("DictEntry");

            var ofArrayEntry   = new NonTerminal("ArrayEntry");
            var ofArray        = new NonTerminal("Array");
            var ofArrayWrapper = new NonTerminal("ArrayWrapper");

            //BNF
            ofValue.Rule = ofIdentifier | ofArrayTerm | ofString | ofNumber;

            ofDictEntry.Rule                = ofArrayWrapper | ofDictionary;
            ofDictionaryContent.Rule        = MakeStarRule(ofDictionaryContent, whitespace, ofDictEntry);
            ofDictionaryContentWrapper.Rule = "{" + ofDictionaryContent + "}";
            ofDictionary.Rule               = ofIdentifier + whitespace + ofDictionaryContentWrapper;

            ofArrayEntry.Rule   = ofValue;
            ofArray.Rule        = MakeStarRule(ofArray, whitespace, ofArrayEntry);
            ofArrayWrapper.Rule = ofArray + semicolon;

            //Set grammar root
            this.Root = ofDictionaryContent;
            MarkPunctuation("{", "}");
            MarkTransient(ofValue, ofDictionaryContentWrapper);
        }
        public ASLGrammar()
            : base(true)
        {
            var stringLit         = TerminalFactory.CreateCSharpString("string");
            var number            = TerminalFactory.CreateCSharpNumber("number");
            var identifier        = TerminalFactory.CreateCSharpIdentifier("identifier");
            var code              = new CustomTerminal("code", MatchCodeTerminal);
            var singleLineComment = new CommentTerminal("SingleLineComment", "//", "\r", "\n", "\u2085", "\u2028", "\u2029");
            var delimitedComment  = new CommentTerminal("DelimitedComment", "/*", "*/");

            NonGrammarTerminals.Add(singleLineComment);
            NonGrammarTerminals.Add(delimitedComment);

            var state     = new KeyTerm("state", "state");
            var start     = new KeyTerm("start", "start");
            var split     = new KeyTerm("split", "split");
            var reset     = new KeyTerm("reset", "reset");
            var isLoading = new KeyTerm("isLoading", "isLoading");
            var gameTime  = new KeyTerm("gameTime", "gameTime");
            var comma     = ToTerm(",", "comma");
            var semi      = ToTerm(";", "semi");

            var root       = new NonTerminal("root");
            var stateDef   = new NonTerminal("stateDef");
            var methodList = new NonTerminal("methodList");
            var varList    = new NonTerminal("varList");
            var var        = new NonTerminal("var");
            var method     = new NonTerminal("method");
            var offsetList = new NonTerminal("offsetList");
            var offset     = new NonTerminal("offset");
            var methodType = new NonTerminal("methodType");

            root.Rule       = stateDef + methodList;
            stateDef.Rule   = state + "(" + stringLit + ")" + "{" + varList + "}";
            methodList.Rule = MakeStarRule(methodList, method);
            varList.Rule    = MakeStarRule(varList, semi, var);
            var.Rule        = (identifier + identifier + ":" + stringLit + comma + offsetList) | Empty;
            method.Rule     = (methodType + "{" + code + "}") | Empty;
            offsetList.Rule = MakePlusRule(offsetList, comma, offset);
            offset.Rule     = number;
            methodType.Rule = start | split | isLoading | gameTime | reset;

            this.Root = root;

            MarkTransient(varList, methodList, offset, methodType);

            this.LanguageFlags = LanguageFlags.NewLineBeforeEOF;
        }
Esempio n. 17
0
        public GramaticaCadena() : base(caseSensitive: false)
        {
            CommentTerminal cadena = new CommentTerminal("string", "\"", ".", "\"");

            #region No Terminales
            NonTerminal S = new NonTerminal("S");
            #endregion

            #region Gramatica
            //Gramatica ambigua:
            S.Rule = cadena;
            #endregion

            #region Preferencias
            this.Root = S;
            #endregion
        }
Esempio n. 18
0
        public Kbtter3QueryGrammar() : base(false)
        {
            //コメント
            var comment = new CommentTerminal("Comment", "/*", "*/");

            NonGrammarTerminals.Add(comment);

            //リテラル
            var number = new NumberLiteral("Number", NumberOptions.AllowSign | NumberOptions.AllowStartEndDot);
            var str    = new StringLiteral("String", "\"");
            var regex  = new RegexLiteral("Regex", '/', '\\');
            var ident  = new IdentifierTerminal("Identifer");

            //非終端
            var Value             = new NonTerminal("Value");
            var Term              = new NonTerminal("Term");
            var Expression        = new NonTerminal("Expression");
            var BinExpression     = new NonTerminal("BinExpression");
            var ParExpression     = new NonTerminal("ParExpression");
            var PostfixExpression = new NonTerminal("PostfixExpression");
            var Operator          = new NonTerminal("Operator");

            //非終端定義
            Value.Rule             = number | str | ident | regex | "null" | "true" | "false";
            Term.Rule              = Value | ParExpression;
            Operator.Rule          = ToTerm("==") | "!=" | ">" | "<" | ">=" | "<=" | "match" | "&&" | "||" | "+" | "-" | "*" | "/" | "%" | "&" | "|" | "^" | ".";
            BinExpression.Rule     = Expression + Operator + Expression;
            PostfixExpression.Rule = (ToTerm("+") + Term) | ("-" + Term) | ("!" + Term);
            Expression.Rule        = BinExpression | Term | PostfixExpression;
            ParExpression.Rule     = ToTerm("(") + Expression + ")";

            RegisterOperators(10, ".");
            RegisterOperators(9, "*", "/", "%");
            RegisterOperators(8, "+", "-");
            RegisterOperators(7, ">", "<", ">=", "<=", "match");
            RegisterOperators(6, "==", "!=");
            RegisterOperators(5, "&");
            RegisterOperators(4, "^");
            RegisterOperators(3, "|");
            RegisterOperators(2, "&&");
            RegisterOperators(1, "||");

            Root = Expression;
            MarkPunctuation("(", ")");
            MarkTransient(Expression, ParExpression, Value, Operator, Term);
        }
        void InitializeComments()
        {
            ////        comment:
            ////            single_line_comment
            ////            requires_comment
            ////            delimited_comment

            ////        single_line_comment:
            ////            #   input_characters_opt
            ////        input_characters:
            ////            input_character
            ////            input_characters   input_character
            ////        input_character:
            ////            Any Unicode character except a new_line_character
            CommentTerminal comment = new CommentTerminal("comment", "#", "\r", "\n", "\r\n");

            NonGrammarTerminals.Add(comment);
        }
Esempio n. 20
0
        public LightGrammar()
        {
            var lineComment = new CommentTerminal("LineComment", "#", "\n", "\r\n");

            NonGrammarTerminals.Add(lineComment);

            ConstructAll();
            SetAllRules();

            MarkPunctuation("[", "]", "(", ")", "{", "}", ":", "=>", ".", "=");

            RegisterOperators(1, "==");
            RegisterOperators(2, "-");
            RegisterOperators(3, "mod");
            RegisterOperators(4, "**");

            this.Root          = TopLevelRoot;
            this.LanguageFlags = LanguageFlags.CreateAst;
        }
        public GramaticaCaracteres() : base(caseSensitive: false)
        {
            #region ER
            CommentTerminal r_char = new CommentTerminal("caracteres", "'", ".", "'"); // es un caracter char
            #endregion

            #region No Terminales
            NonTerminal S = new NonTerminal("S"),
                        E = new NonTerminal("E");
            #endregion

            #region Gramatica
            //Gramatica ambigua:
            S.Rule = E;
            E.Rule = r_char;
            #endregion

            #region Preferencias
            this.Root = S;
            #endregion
        }
Esempio n. 22
0
        public analizador()
        {
            var entero            = new RegexBasedTerminal("entero", "[0-9]+");
            IdentifierTerminal id = new IdentifierTerminal("id");
            var singleLineComment = new CommentTerminal("SingleLineComment", "//", "\r", "\n", "\u2085", "\u2028", "\u2029");
            var delimitedComment  = new CommentTerminal("DelimitedComment", "/*", "*/");
            var p = new StringLiteral("p", "\"");

            var ENTERO = ToTerm("entero");
            var CADENA = ToTerm("cadena");
            var DOUBLE = ToTerm("double");

            //ENTERO.AstConfig.NodeType = typeof(Arbol);
            //CADENA.AstConfig.NodeType = typeof(Arbol);
            //DOUBLE.AstConfig.NodeType = typeof(Arbol);


            p.AstConfig.NodeType      = typeof(Arbol);
            entero.AstConfig.NodeType = typeof(Arbol);
            id.AstConfig.NodeType     = typeof(Arbol);
            NonTerminal E = new NonTerminal("E", typeof(Arbol));



            E.Rule = E + ToTerm("+") + E
                     | E + ToTerm("-") + E
                     | E + ToTerm("*") + E
                     | E + ToTerm("/") + E
                     | ToTerm("(") + E + ToTerm(")")
                     | entero
                     | id;

            RegisterOperators(1, "+", "-");//ESTABLESEMOS PRESEDENCIA
            RegisterOperators(2, "*", "/");

            this.Root = E;
            NonGrammarTerminals.Add(singleLineComment); // QUITAMOS LOS COMENTARIOS DE LA GRAMATICA
            NonGrammarTerminals.Add(delimitedComment);
            LanguageFlags = LanguageFlags.CreateAst;    //IMPORTANTE PARA CREAR EL ARBOL SIN ESTO NO LO CREARA
        }
Esempio n. 23
0
        private void SetUpTerminals()
        {
            Comment = new CommentTerminal(nameof(Comment), ";", "\n", "\r");
            NonGrammarTerminals.Add(Comment);

            var ident = new IdentifierTerminal(nameof(Ident), ".$_", ".$_");

            ident.Options = IdOptions.IsNotKeyword;
            Ident         = ident;

            var number = new NumberLiteral(nameof(Number), NumberOptions.AllowUnderscore | NumberOptions.IntOnly | NumberOptions.AllowSign);

            number.AddPrefix("0x", NumberOptions.Hex);
            number.AddPrefix("0b", NumberOptions.Binary);
            number.AddPrefix("0", NumberOptions.Octal);
            Number = number;

            NormalString  = new StringLiteral(nameof(NormalString), "\"", StringOptions.NoEscapes);
            EscapedString = new StringLiteral(nameof(EscapedString), "`", StringOptions.AllowsAllEscapes);
            CharLiteral   = new StringLiteral(nameof(CharLiteral), "'", StringOptions.IsChar | StringOptions.AllowsAllEscapes);
            Separator     = ToTerm(SeparatorToken, nameof(Separator));
        }
Esempio n. 24
0
        /// <summary>
        /// 新しいインスタンスを以下略
        /// </summary>
        public Kb10uyConfigGrammar()
            : base()
        {
            var Number       = new NumberLiteral("Number");
            var String       = new StringLiteral("String", "\"", StringOptions.AllowsAllEscapes);
            var CommentLine  = new CommentTerminal("Comment", "#", "\n", "\r");
            var CommentBlock = new CommentTerminal("Comment", "#<", ">");

            //数値設定
            Number.DefaultIntTypes  = new[] { TypeCode.Int32 };
            Number.DefaultFloatType = TypeCode.Double;
            String.EscapeChar       = '\\';
            NonGrammarTerminals.Add(CommentBlock);
            NonGrammarTerminals.Add(CommentLine);

            var Value    = new NonTerminal("Value");
            var Values   = new NonTerminal("Value\'s\'");
            var ValueSet = new NonTerminal("Array");
            var Property = new NonTerminal("Property");
            var Prefix   = new NonTerminal("Prefix");
            var Node     = new NonTerminal("Node");
            var Nodes    = new NonTerminal("Nodes");
            //var PropertySet = new NonTerminal("PropertySet");
            var Group      = new NonTerminal("Group");
            var ConfigRoot = new NonTerminal("Root");

            Value.Rule      = Number | String | ValueSet;
            Values.Rule     = MakeStarRule(Values, ToTerm(","), Value);
            ValueSet.Rule   = ToTerm("[") + Values + "]";
            Prefix.Rule     = ToTerm("Number") | "String" | "Array";
            Property.Rule   = Prefix + ":" + String + "=" + Value + ";";
            Node.Rule       = Property | Group;
            Nodes.Rule      = MakeStarRule(Nodes, Node);
            Group.Rule      = String + ":" + "{" + Nodes + "}";
            ConfigRoot.Rule = MakeStarRule(ConfigRoot, Group);
            Root            = ConfigRoot;

            MarkPunctuation("[", "]", ",", "{", "}", ":", ";", "=");
        }
Esempio n. 25
0
        public Gramatica()
            : base(false)
        {
            // Expresiones regulares se escribe de la siguiente forma
            //existe multiples formas de crear expresiones regulares puedes revisar la documentacion.

            RegexBasedTerminal palabra = new RegexBasedTerminal("palabra", "[0-9]*[a-zA-Z][0-9a-zA-Z]*");
            RegexBasedTerminal numero  = new RegexBasedTerminal("numero", "[0-9]+");
            CommentTerminal    comm    = new CommentTerminal("comm", "\n", "\r");

            base.NonGrammarTerminals.Add(comm);

            //la gramatica siguiente acepta cadenas de entradas de la forma : esto-es-una-lista-de-555-numeros-y-letras-55-61-12-32

            //Declaracion de no terminales

            NonTerminal
                S = new NonTerminal("S"),
                L = new NonTerminal("L");

            //S.Rule para escribir el cuerpo de un no terminal con todas sus producciones.

            S.Rule = L;

            L.Rule = L + "-" + numero
                     | numero
                     | L + "-" + palabra
                     | palabra;


            //indicamos la produccion inicial con la siguiente linea

            this.Root = S;

            MarkPunctuation("-");
        }
Esempio n. 26
0
        void InitializeComments()
        {
            ////        comment:
            ////            single_line_comment
            ////            requires_comment
            ////            delimited_comment

            ////        single_line_comment:
            ////            #   input_characters_opt
            ////        input_characters:
            ////            input_character
            ////            input_characters   input_character
            ////        input_character:
            ////            Any Unicode character except a new_line_character
            CommentTerminal comment = new CommentTerminal("comment", "#", "\r", "\n", "\r\n");

            NonGrammarTerminals.Add(comment);

            ////        delimited_comment:
            ////            <#   delimited_comment_text_opt   hashes   >
            ////        delimited_comment_text:
            ////            delimited_comment_section
            ////            delimited_comment_text   delimited_comment_section
            ////        delimited_comment_section:
            ////            >
            ////            hashes_opt   not_greater_than_or_hash
            ////        hashes:
            ////            #
            ////            hashes   #
            ////        not_greater_than_or_hash:
            ////            Any Unicode character except > or #

            CommentTerminal delimited_comment = new CommentTerminal("delimited_comment", "<#", "#>");

            NonGrammarTerminals.Add(delimited_comment);
        }
Esempio n. 27
0
        public GramaticaCQL() : base(false)
        {
            CommentTerminal blockComment = new CommentTerminal("block-comment", "/*", "*/");
            CommentTerminal lineComment  = new CommentTerminal("line-comment", "//",
                                                               "\r", "\n", "\u2085", "\u2028", "\u2029");

            NonGrammarTerminals.Add(blockComment);
            NonGrammarTerminals.Add(lineComment);

            /* Reserved Words */
            KeyTerm
                null_      = ToTerm("null"),
                true_      = ToTerm("true"),
                false_     = ToTerm("false"),
                type_      = ToTerm("type"),
                if_        = ToTerm("if"),
                not_       = ToTerm("not"),
                exists_    = ToTerm("exists"),
                int_       = ToTerm("int"),
                double_    = ToTerm("double"),
                string_    = ToTerm("string"),
                boolean_   = ToTerm("boolean"),
                date_      = ToTerm("date"),
                time_      = ToTerm("time"),
                use_       = ToTerm("use"),
                create_    = ToTerm("create"),
                database_  = ToTerm("database"),
                drop_      = ToTerm("drop"),
                table_     = ToTerm("table"),
                counter_   = ToTerm("counter"),
                primary_   = ToTerm("primary"),
                key_       = ToTerm("key"),
                alter_     = ToTerm("alter"),
                add_       = ToTerm("add"),
                truncate_  = ToTerm("truncate"),
                commit_    = ToTerm("commit"),
                rollback_  = ToTerm("rollback"),
                user_      = ToTerm("user"),
                with_      = ToTerm("with"),
                password_  = ToTerm("password"),
                grant_     = ToTerm("grant"),
                on_        = ToTerm("on"),
                revoke_    = ToTerm("revoke"),
                insert_    = ToTerm("insert"),
                into_      = ToTerm("into"),
                values_    = ToTerm("values"),
                update_    = ToTerm("update"),
                set_       = ToTerm("set"),
                where_     = ToTerm("where"),
                delete_    = ToTerm("delete"),
                from_      = ToTerm("from"),
                select_    = ToTerm("select"),
                order_     = ToTerm("order"),
                by_        = ToTerm("by"),
                asc_       = ToTerm("asc"),
                desc_      = ToTerm("desc"),
                limit_     = ToTerm("limit"),
                begin_     = ToTerm("begin"),
                batch_     = ToTerm("batch"),
                apply_     = ToTerm("apply"),
                count_     = ToTerm("count"),
                min_       = ToTerm("min"),
                max_       = ToTerm("max"),
                sum_       = ToTerm("sum"),
                avg_       = ToTerm("avg"),
                in_        = ToTerm("in"),
                else_      = ToTerm("else"),
                switch_    = ToTerm("switch"),
                case_      = ToTerm("case"),
                default_   = ToTerm("default"),
                while_     = ToTerm("while"),
                do_        = ToTerm("do"),
                for_       = ToTerm("for"),
                new_       = ToTerm("new"),
                map_       = ToTerm("map"),
                list_      = ToTerm("list"),
                procedure_ = ToTerm("procedure"),
                call_      = ToTerm("call"),
                break_     = ToTerm("break"),
                continue_  = ToTerm("continue"),
                return_    = ToTerm("return"),
                cursor_    = ToTerm("cursor"),
                is_        = ToTerm("is"),
                each_      = ToTerm("each"),
                open_      = ToTerm("open"),
                close_     = ToTerm("close"),
                log_       = ToTerm("log"),
                throw_     = ToTerm("throw"),
                try_       = ToTerm("try"),
                catch_     = ToTerm("catch"),
                as_        = ToTerm("as");

            MarkReservedWords("null", "true", "false", "type", "if", "not", "exists", "int", "double", "string", "boolean",
                              "date", "time", "use", "create", "database", "drop", "table", "counter", "primary", "key", "alter", "add",
                              "truncate", "commit", "rollback", "user", "with", "password", "grant", "on", "revoke", "insert", "into",
                              "values", "update", "set", "where", "delete", "from", "select", "order", "by", "asc", "desc", "limit",
                              "begin", "batch", "apply", "count", "min", "max", "sum", "avg", "in", "else", "switch", "case", "default",
                              "while", "do", "for", "new", "map", "list", "procedure", "call", "break", "continue", "return", "cursor",
                              "is", "each", "open", "close", "log", "throw", "try", "catch", "as");

            /* Relational operators */
            KeyTerm
                menorigual = ToTerm("<="),
                mayorigual = ToTerm(">="),
                menorque   = ToTerm("<"),
                mayorque   = ToTerm(">"),
                igual      = ToTerm("=="),
                diferente  = ToTerm("!=");

            /* Logic operators */
            KeyTerm
                or  = ToTerm("||"),
                and = ToTerm("&&"),
                xor = ToTerm("^"),
                not = ToTerm("!");

            /* Shift operators */
            KeyTerm
                leftShift  = ToTerm("--"),
                rightShift = ToTerm("++");

            /* Arithmetic Operators*/
            KeyTerm
                mas      = ToTerm("+"),
                menos    = ToTerm("-"),
                por      = ToTerm("*"),
                division = ToTerm("/"),
                modulo   = ToTerm("%"),
                potencia = ToTerm("**");

            /* Symbols*/
            KeyTerm
                equal         = ToTerm("="),
                semicolon     = ToTerm(";"),
                leftPar       = ToTerm("("),
                rightPar      = ToTerm(")"),
                dot           = ToTerm("."),
                comma         = ToTerm(","),
                questionmark  = ToTerm("?"),
                colon         = ToTerm(":"),
                leftCor       = ToTerm("["),
                rightCor      = ToTerm("]"),
                leftLla       = ToTerm("{"),
                rightLla      = ToTerm("}"),
                masEqual      = ToTerm("+="),
                menosEqual    = ToTerm("-="),
                porEqual      = ToTerm("*="),
                divisionEqual = ToTerm("/="),
                menormenor    = ToTerm("<<"),
                mayormayor    = ToTerm(">>");

            MarkPunctuation(";");

            var number = new NumberLiteral("number");
            //var identifier = new IdentifierTerminal("identifier");
            RegexBasedTerminal identifier  = new RegexBasedTerminal("identifier", "([a-zA-ZñÑ]|_)([a-zA-ZñÑ]|[0-9]|_)*");
            RegexBasedTerminal identifier2 = new RegexBasedTerminal("identifier2", "@([a-zA-ZñÑ]|_)([a-zA-ZñÑ]|[0-9]|_)*");
            //RegexBasedTerminal date = new RegexBasedTerminal("date", "\'([0-2][0-9]{3}|[0-9][0-9]{0,2})-([0]?[0-9]|[1][0-2])-([0]?[0-9]|[1-2][0-9]|[3][0-1])\'");
            //RegexBasedTerminal time = new RegexBasedTerminal("time", "\'([0]?[0-9]|[1][0-9]|[2][0-4]):([0]?[0-9]|[1-5][0-9]):([0]?[0-9]|[1-5][0-9])\'");
            RegexBasedTerminal date = new RegexBasedTerminal("date", "\'[0-9]+-[0-9]+-[0-9]+\'");
            RegexBasedTerminal time = new RegexBasedTerminal("time", "\'[0-9]+:[0-9]+:[0-9]+\'");
            var stringliteral       = new StringLiteral("stringliteral", "\"", StringOptions.IsTemplate);

            NonTerminal
                INICIO        = new NonTerminal("INICIO"),
                INSTRUCCIONES = new NonTerminal("INSTRUCCIONES"),
                INSTRUCCION   = new NonTerminal("INSTRUCCION"),

                TYPE            = new NonTerminal("TYPE"),
                TYPE_PRIMITIVE  = new NonTerminal("TYPE_PRIMITIVE"),
                TYPE_COLLECTION = new NonTerminal("TYPE_COLLECTION"),
                TYPEDEF         = new NonTerminal("TYPEDEF"),
                IFNOTEXIST      = new NonTerminal("IFNOTEXIST"),
                IFEXIST         = new NonTerminal("IFEXIST"),
                ATTRIBUTE_LIST  = new NonTerminal("ATTRIBUTE_LIST"),
                ATTRIBUTEREF    = new NonTerminal("ATTRIBUTEREF"),
                ATTRIBUTE       = new NonTerminal("ATTRIBUTE"),

                USE           = new NonTerminal("USE"),
                DATABASEDEF   = new NonTerminal("DATABASEDEF"),
                DROP          = new NonTerminal("DROP"),
                TABLEDEF      = new NonTerminal("TABLEDEF"),
                COLUMN_LIST   = new NonTerminal("COLUMN_LIST"),
                COLUMN        = new NonTerminal("COLUMN"),
                ID_LIST       = new NonTerminal("ID_LIST"),
                TABLEALTER    = new NonTerminal("TABLEALTER"),
                TABLEDROP     = new NonTerminal("TABLEDROP"),
                TABLETRUNCATE = new NonTerminal("TABLETRUNCATE"),

                COMMIT   = new NonTerminal("COMMIT"),
                ROLLBACK = new NonTerminal("ROLLBACK"),

                USERDEF = new NonTerminal("USERDEF"),
                GRANT   = new NonTerminal("GRANT"),
                REVOKE  = new NonTerminal("REVOKE"),

                WHERE      = new NonTerminal("WHERE"),
                INSERT     = new NonTerminal("INSERT"),
                UPDATE     = new NonTerminal("UPDATE"),
                DELETE     = new NonTerminal("DELETE"),
                SELECT     = new NonTerminal("SELECT"),
                SELECT_EXP = new NonTerminal("SELECT_EXP"),
                ORDER_LIST = new NonTerminal("ORDER_LIST"),
                ORDER      = new NonTerminal("ORDER"),
                BATCH      = new NonTerminal("BATCH"),
                DML_LIST   = new NonTerminal("DML_LIST"),
                DML        = new NonTerminal("DML"),

                TARGET_LIST = new NonTerminal("TARGET_LIST"),
                TARGET      = new NonTerminal("TARGET"),

                EXPRESSION_STMT = new NonTerminal("EXPRESSION_STMT"),
                ASSIGNMENT_STMT = new NonTerminal("ASSIGNMENT_STMT"),
                ASSIGNMENTS     = new NonTerminal("ASSIGNMENTS"),
                ASSIGNMENT_CALL = new NonTerminal("ASSIGNMENT_CALL"),
                ASSIGNMENT_LIST = new NonTerminal("ASSIGNMENT_LIST"),

                DECLARATION_STMT          = new NonTerminal("DECLARATION_STMT"),
                AUGMENTED_ASSIGNMENT_STMT = new NonTerminal("AUGMENTED_ASSIGNMENT_STMT"),
            //AUGTARGET = new NonTerminal("AUGTARGET"),
                AUG_OPERATOR = new NonTerminal("AUG_OPERATOR"),

                IF_STMT      = new NonTerminal("IF_STMT"),
                IF_LIST      = new NonTerminal("IF_LIST"),
                SWITCH_STMT  = new NonTerminal("SWITCH_STMT"),
                CASES        = new NonTerminal("CASES"),
                WHILE_STMT   = new NonTerminal("WHILE_STMT"),
                DOWHILE_STMT = new NonTerminal("DOWHILE_STMT"),
                FOR_STMT     = new NonTerminal("FOR_STMT"),
                FOR_INIT     = new NonTerminal("FOR_INIT"),
                FOR_UPDATE   = new NonTerminal("FOR_UPDATE"),

                FUNDEF         = new NonTerminal("FUNDEF"),
                PARAMETER_LIST = new NonTerminal("PARAMETER_LIST"),

                PROCDEF = new NonTerminal("PROCDEF"),

                BREAK_STMT    = new NonTerminal("BREAK_STMT"),
                CONTINUE_STMT = new NonTerminal("CONTINUE_STMT"),
                RETURN_STMT   = new NonTerminal("RETURN_STMT"),

                CURSOR_STMT   = new NonTerminal("CURSOR_STMT"),
                FOREACH_STMT  = new NonTerminal("FOREACH_STMT"),
                OPEN_STMT     = new NonTerminal("OPEN_STMT"),
                CLOSE_STMT    = new NonTerminal("CLOSE_STMT"),
                LOG_STMT      = new NonTerminal("LOG_STMT"),
                THROW_STMT    = new NonTerminal("THROW_STMT"),
                TRYCATCH_STMT = new NonTerminal("TRYCATCH_STMT"),

                BLOQUE     = new NonTerminal("BLOQUE"),
                SENTENCIAS = new NonTerminal("SENTENCIAS"),
                SENTENCIA  = new NonTerminal("SENTENCIA"),

            /*STARRED_EXPRESSION = new NonTerminal("STARRED_EXPRESSION"),
             * STARRED_LIST = new NonTerminal("STARRED_LIST"),
             * STARRED_ITEM = new NonTerminal("STARRED_ITEM"),*/

                AGGREGATION     = new NonTerminal("AGGREGATION"),
                AGGREGATION_FUN = new NonTerminal("AGGREGATION_FUN"),

                EXPRESSION_LIST        = new NonTerminal("EXPRESSION_LIST"),
                EXPRESSION             = new NonTerminal("EXPRESSION"),
                CONDITIONAL_EXPRESSION = new NonTerminal("CONDITIONAL_EXPRESSION"),
                INSTANCE      = new NonTerminal("INSTANCE"),
                OR_EXPR       = new NonTerminal("OR_EXPR"),
                AND_EXPR      = new NonTerminal("AND_EXPR"),
                XOR_EXPR      = new NonTerminal("XOR_EXPR"),
                NOT_EXPR      = new NonTerminal("NOT_EXPR"),
                COMPARISON    = new NonTerminal("COMPARISON"),
                COMPARISON_EQ = new NonTerminal("COMPARISON_EQ"),
                COMP_OPERATOR = new NonTerminal("COMP_OPERATOR"),
                SHIFT_EXPR    = new NonTerminal("SHIFT_EXPR"),
                A_EXPR        = new NonTerminal("A_EXPR"),
                M_EXPR        = new NonTerminal("M_EXPR"),
                U_EXPR        = new NonTerminal("U_EXPR"),
                POWER         = new NonTerminal("POWER"),
                PRIMARY       = new NonTerminal("PRIMARY"),
                ATOM          = new NonTerminal("ATOM"),
                FUNCALL       = new NonTerminal("FUNCALL"),
                CALL          = new NonTerminal("CALL"),
                ACCESS        = new NonTerminal("ACCESS"),
                ENCLOSURE     = new NonTerminal("ENCLOSURE"),
                LITERAL       = new NonTerminal("LITERAL"),
                PARENTH_FORM  = new NonTerminal("PARENTH_FORM"),
                MAP_DISPLAY   = new NonTerminal("MAP_DISPLAY"),
                MAP_LIST      = new NonTerminal("MAP_LIST"),
                LIST_DISPLAY  = new NonTerminal("LIST_DISPLAY"),
                SET_DISPLAY   = new NonTerminal("SET_DISPLAY");

            this.Root = INICIO;

            INICIO.Rule = INSTRUCCIONES;

            INSTRUCCIONES.Rule = MakePlusRule(INSTRUCCIONES, INSTRUCCION);

            INSTRUCCION.Rule = TYPEDEF + semicolon
                               | USE + semicolon
                               | DATABASEDEF + semicolon
                               | DROP + semicolon
                               | TABLEDEF + semicolon
                               | TABLEALTER + semicolon
                               | TABLEDROP + semicolon
                               | TABLETRUNCATE + semicolon
                               | COMMIT + semicolon
                               | ROLLBACK + semicolon
                               | USERDEF + semicolon
                               | GRANT + semicolon
                               | REVOKE + semicolon
                               | INSERT + semicolon
                               | UPDATE + semicolon
                               | DELETE + semicolon
                               | SELECT + semicolon
                               | BATCH + semicolon

                               | EXPRESSION_STMT + semicolon
                               | DECLARATION_STMT + semicolon
                               | ASSIGNMENT_STMT + semicolon
                               | ASSIGNMENT_CALL + semicolon
                               | AUGMENTED_ASSIGNMENT_STMT + semicolon
                               | IF_STMT
                               | SWITCH_STMT
                               | WHILE_STMT
                               | DOWHILE_STMT + semicolon
                               | FOR_STMT

                               | FUNDEF
                               | PROCDEF

                               | BREAK_STMT + semicolon
                               | CONTINUE_STMT + semicolon
                               | RETURN_STMT + semicolon

                               | CURSOR_STMT + semicolon
                               | FOREACH_STMT
                               | OPEN_STMT + semicolon
                               | CLOSE_STMT + semicolon
                               | LOG_STMT + semicolon
                               | THROW_STMT + semicolon
                               | TRYCATCH_STMT;

            //INSTRUCCION.ErrorRule = SyntaxError + semicolon;


            TYPE.Rule = int_ | double_ | string_ | boolean_ | date_ | time_ | identifier | counter_ | map_ | list_ | set_;

            TYPE_PRIMITIVE.Rule = int_ | double_ | string_ | boolean_ | date_ | time_;

            TYPE_COLLECTION.Rule = int_ | double_ | string_ | boolean_ | date_ | time_ | identifier | counter_
                                   | map_ + menorque + TYPE_PRIMITIVE + comma + TYPE_COLLECTION + mayorque
                                   | list_ + menorque + TYPE_COLLECTION + mayorque
                                   | set_ + menorque + TYPE_COLLECTION + mayorque;

            IFNOTEXIST.Rule = if_ + not_ + exists_;

            IFEXIST.Rule = if_ + exists_;

            TYPEDEF.Rule = create_ + type_ + identifier + leftPar + ATTRIBUTE_LIST + rightPar
                           | create_ + type_ + IFNOTEXIST + identifier + leftPar + ATTRIBUTE_LIST + rightPar;

            ATTRIBUTE_LIST.Rule = MakePlusRule(ATTRIBUTE_LIST, comma, ATTRIBUTE);

            ATTRIBUTE.Rule = identifier + TYPE_COLLECTION;

            USE.Rule = use_ + identifier;

            DATABASEDEF.Rule = create_ + database_ + identifier
                               | create_ + database_ + IFNOTEXIST + identifier;

            DROP.Rule = drop_ + database_ + identifier
                        | drop_ + database_ + IFNOTEXIST + identifier;

            TABLEDEF.Rule = create_ + table_ + identifier + leftPar + COLUMN_LIST + rightPar
                            | create_ + table_ + IFNOTEXIST + identifier + leftPar + COLUMN_LIST + rightPar;

            COLUMN_LIST.Rule = MakePlusRule(COLUMN_LIST, comma, COLUMN);

            COLUMN.Rule = identifier + TYPE_COLLECTION
                          | identifier + TYPE_COLLECTION + primary_ + key_
                          | primary_ + key_ + leftPar + ID_LIST + rightPar;

            ID_LIST.Rule = MakePlusRule(ID_LIST, comma, identifier);

            TABLEALTER.Rule = alter_ + table_ + identifier + add_ + ATTRIBUTE_LIST
                              | alter_ + table_ + identifier + drop_ + ID_LIST;

            TABLEDROP.Rule = drop_ + table_ + identifier
                             | drop_ + table_ + IFEXIST + identifier;

            TABLETRUNCATE.Rule = truncate_ + table_ + identifier;


            COMMIT.Rule = commit_;

            ROLLBACK.Rule = rollback_;


            USERDEF.Rule = create_ + user_ + identifier + with_ + password_ + stringliteral;

            GRANT.Rule = grant_ + identifier + on_ + identifier;

            REVOKE.Rule = revoke_ + identifier + on_ + identifier;

            WHERE.Rule = where_ + EXPRESSION
                         | where_ + EXPRESSION + in_ + EXPRESSION_LIST
                         | where_ + EXPRESSION + in_ + leftPar + EXPRESSION_LIST + rightPar;

            INSERT.Rule = insert_ + into_ + identifier + values_ + leftPar + EXPRESSION_LIST + rightPar
                          | insert_ + into_ + identifier + leftPar + ID_LIST + rightPar + values_ + leftPar + EXPRESSION_LIST + rightPar;

            UPDATE.Rule = update_ + identifier + set_ + ASSIGNMENT_LIST
                          | update_ + identifier + set_ + ASSIGNMENT_LIST + WHERE;

            DELETE.Rule = delete_ + from_ + identifier
                          | delete_ + from_ + identifier + WHERE
                          | delete_ + TARGET + from_ + identifier
                          | delete_ + TARGET + from_ + identifier + WHERE;

            SELECT.Rule = select_ + SELECT_EXP + from_ + identifier
                          | select_ + SELECT_EXP + from_ + identifier + WHERE
                          | select_ + SELECT_EXP + from_ + identifier + order_ + by_ + ORDER_LIST
                          | select_ + SELECT_EXP + from_ + identifier + WHERE + order_ + by_ + ORDER_LIST
                          | select_ + SELECT_EXP + from_ + identifier + limit_ + EXPRESSION
                          | select_ + SELECT_EXP + from_ + identifier + WHERE + limit_ + EXPRESSION
                          | select_ + SELECT_EXP + from_ + identifier + order_ + by_ + ORDER_LIST + limit_ + EXPRESSION
                          | select_ + SELECT_EXP + from_ + identifier + WHERE + order_ + by_ + ORDER_LIST + limit_ + EXPRESSION;

            SELECT_EXP.Rule = EXPRESSION_LIST | por;

            ORDER_LIST.Rule = MakePlusRule(ORDER_LIST, comma, ORDER);

            ORDER.Rule = identifier
                         | identifier + asc_
                         | identifier + desc_; //TARGET*

            BATCH.Rule = begin_ + batch_ + DML_LIST + apply_ + batch_;

            DML_LIST.Rule = MakePlusRule(DML_LIST, DML);

            DML.Rule = INSERT + semicolon
                       | UPDATE + semicolon
                       | DELETE + semicolon;

            ////////////////////////////////////////////////////////////////

            BLOQUE.Rule = leftLla + SENTENCIAS + rightLla
                          | leftLla + rightLla;

            SENTENCIAS.Rule = MakePlusRule(SENTENCIAS, SENTENCIA);

            SENTENCIA.Rule = TYPEDEF + semicolon    /****/
                             | USE + semicolon
                             | DATABASEDEF + semicolon
                             | DROP + semicolon
                             | TABLEDEF + semicolon
                             | TABLEALTER + semicolon
                             | TABLEDROP + semicolon
                             | TABLETRUNCATE + semicolon
                             | COMMIT + semicolon
                             | ROLLBACK + semicolon
                             | USERDEF + semicolon
                             | GRANT + semicolon
                             | REVOKE + semicolon
                             | INSERT + semicolon
                             | UPDATE + semicolon
                             | DELETE + semicolon
                             | SELECT + semicolon
                             | BATCH + semicolon

                             | EXPRESSION_STMT + semicolon
                             | DECLARATION_STMT + semicolon
                             | ASSIGNMENT_STMT + semicolon
                             | ASSIGNMENT_CALL + semicolon
                             | AUGMENTED_ASSIGNMENT_STMT + semicolon
                             | IF_STMT
                             | SWITCH_STMT
                             | WHILE_STMT
                             | DOWHILE_STMT + semicolon
                             | FOR_STMT

                             | BREAK_STMT + semicolon
                             | CONTINUE_STMT + semicolon
                             | RETURN_STMT + semicolon

                             | CURSOR_STMT + semicolon
                             | FOREACH_STMT
                             | OPEN_STMT + semicolon
                             | CLOSE_STMT + semicolon
                             | LOG_STMT + semicolon
                             | THROW_STMT + semicolon
                             | TRYCATCH_STMT;

            TARGET_LIST.Rule = MakePlusRule(TARGET_LIST, comma, TARGET);

            TARGET.Rule = identifier     //*
                          | identifier2
                          | ATTRIBUTEREF //*
                          | ACCESS;

            EXPRESSION_STMT.Rule = SHIFT_EXPR + leftShift | SHIFT_EXPR + rightShift | FUNCALL | CALL | ATTRIBUTEREF;

            DECLARATION_STMT.Rule = TYPE + TARGET_LIST
                                    | TYPE + TARGET_LIST + equal + EXPRESSION;

            ASSIGNMENT_STMT.Rule = TARGET + equal + EXPRESSION
                                   | TARGET + equal + CALL;

            ASSIGNMENT_CALL.Rule = TARGET_LIST + equal + CALL;

            ASSIGNMENT_LIST.Rule = MakePlusRule(ASSIGNMENT_LIST, comma, ASSIGNMENTS);

            ASSIGNMENTS.Rule = ASSIGNMENT_STMT | AUGMENTED_ASSIGNMENT_STMT;

            AUGMENTED_ASSIGNMENT_STMT.Rule = TARGET + AUG_OPERATOR + EXPRESSION;

            //AUGTARGET.Rule = identifier | identifier2 | ATTRIBUTEREF; //*

            AUG_OPERATOR.Rule = masEqual | menosEqual | porEqual | divisionEqual;

            IF_STMT.Rule = IF_LIST + else_ + BLOQUE
                           | IF_LIST;

            IF_LIST.Rule = IF_LIST + else_ + if_ + leftPar + EXPRESSION + rightPar + BLOQUE
                           | if_ + leftPar + EXPRESSION + rightPar + BLOQUE;

            SWITCH_STMT.Rule = switch_ + leftPar + EXPRESSION + rightPar + leftLla + CASES + rightLla
                               | switch_ + leftPar + EXPRESSION + rightPar + leftLla + CASES + default_ + colon + BLOQUE + rightLla;

            CASES.Rule = CASES + case_ + EXPRESSION + colon + BLOQUE
                         | case_ + EXPRESSION + colon + BLOQUE;

            WHILE_STMT.Rule = while_ + leftPar + EXPRESSION + rightPar + BLOQUE;

            DOWHILE_STMT.Rule = do_ + BLOQUE + while_ + leftPar + EXPRESSION + rightPar;

            FOR_STMT.Rule = for_ + leftPar + FOR_INIT + semicolon + EXPRESSION + semicolon + FOR_UPDATE + rightPar + BLOQUE;

            FOR_INIT.Rule = DECLARATION_STMT | ASSIGNMENT_STMT;

            FOR_UPDATE.Rule = AUGMENTED_ASSIGNMENT_STMT | ASSIGNMENT_STMT | SHIFT_EXPR + leftShift | SHIFT_EXPR + rightShift;

            FUNDEF.Rule = TYPE + identifier + leftPar + PARAMETER_LIST + rightPar + BLOQUE
                          | TYPE + identifier + leftPar + rightPar + BLOQUE;

            PARAMETER_LIST.Rule = PARAMETER_LIST + comma + TYPE + identifier2
                                  | TYPE + identifier2;

            PROCDEF.Rule = procedure_ + identifier + leftPar + PARAMETER_LIST + rightPar + comma + leftPar + PARAMETER_LIST + rightPar + BLOQUE
                           | procedure_ + identifier + leftPar + rightPar + comma + leftPar + rightPar + BLOQUE
                           | procedure_ + identifier + leftPar + PARAMETER_LIST + rightPar + comma + leftPar + rightPar + BLOQUE
                           | procedure_ + identifier + leftPar + rightPar + comma + leftPar + PARAMETER_LIST + rightPar + BLOQUE;

            BREAK_STMT.Rule = break_;

            CONTINUE_STMT.Rule = continue_;

            RETURN_STMT.Rule = return_
                               | return_ + EXPRESSION_LIST;

            CURSOR_STMT.Rule = cursor_ + identifier2 + is_ + SELECT;

            FOREACH_STMT.Rule = for_ + each_ + leftPar + PARAMETER_LIST + rightPar + in_ + identifier2 + BLOQUE
                                | for_ + each_ + leftPar + rightPar + in_ + identifier2 + BLOQUE;

            OPEN_STMT.Rule = open_ + identifier2;

            CLOSE_STMT.Rule = close_ + identifier2;

            LOG_STMT.Rule = log_ + leftPar + EXPRESSION + rightPar;

            THROW_STMT.Rule = throw_ + new_ + identifier;

            TRYCATCH_STMT.Rule = try_ + BLOQUE + catch_ + leftPar + PARAMETER_LIST + rightPar + BLOQUE
                                 | try_ + BLOQUE + catch_ + leftPar + rightPar + BLOQUE;;

            EXPRESSION_LIST.Rule = MakePlusRule(EXPRESSION_LIST, comma, EXPRESSION);

            EXPRESSION.Rule = CONDITIONAL_EXPRESSION | INSTANCE;

            INSTANCE.Rule = new_ + identifier
                            | new_ + map_ + menorque + TYPE_PRIMITIVE + comma + TYPE_COLLECTION + mayorque
                            | new_ + list_ + menorque + TYPE_COLLECTION + mayorque
                            | new_ + set_ + menorque + TYPE_COLLECTION + mayorque;

            CONDITIONAL_EXPRESSION.Rule = OR_EXPR | OR_EXPR + questionmark + EXPRESSION + colon + EXPRESSION;

            OR_EXPR.Rule = AND_EXPR | OR_EXPR + or + AND_EXPR;

            AND_EXPR.Rule = XOR_EXPR | AND_EXPR + and + XOR_EXPR;

            XOR_EXPR.Rule = COMPARISON_EQ | XOR_EXPR + xor + COMPARISON_EQ;

            COMPARISON_EQ.Rule = COMPARISON | COMPARISON_EQ + igual + COMPARISON
                                 | COMPARISON_EQ + diferente + COMPARISON;

            COMPARISON.Rule = A_EXPR | COMPARISON + COMP_OPERATOR + A_EXPR;

            COMP_OPERATOR.Rule = menorque | mayorque | mayorigual | menorigual;

            A_EXPR.Rule = M_EXPR | A_EXPR + mas + A_EXPR | A_EXPR + menos + M_EXPR;

            M_EXPR.Rule = U_EXPR | M_EXPR + por + U_EXPR | M_EXPR + division + U_EXPR
                          | M_EXPR + modulo + U_EXPR;

            U_EXPR.Rule = NOT_EXPR | POWER | menos + U_EXPR | mas + U_EXPR;

            NOT_EXPR.Rule = not + U_EXPR;

            POWER.Rule = SHIFT_EXPR | SHIFT_EXPR + potencia + U_EXPR;

            SHIFT_EXPR.Rule = PRIMARY | SHIFT_EXPR + leftShift | SHIFT_EXPR + rightShift;

            PRIMARY.Rule = ATOM | ATTRIBUTEREF | AGGREGATION | FUNCALL | ACCESS;

            ATOM.Rule = identifier | identifier2 | LITERAL | ENCLOSURE;

            LITERAL.Rule = number | stringliteral | true_ | false_ | date | time | null_;

            ATTRIBUTEREF.Rule = PRIMARY + dot + identifier
                                | PRIMARY + dot + FUNCALL;

            AGGREGATION.Rule = AGGREGATION_FUN + leftPar + menormenor + SELECT + mayormayor + rightPar;

            AGGREGATION_FUN.Rule = count_ | min_ | max_ | sum_ | avg_;

            ENCLOSURE.Rule = PARENTH_FORM | MAP_DISPLAY | LIST_DISPLAY | SET_DISPLAY;

            PARENTH_FORM.Rule = leftPar + EXPRESSION + rightPar
                                | leftPar + TYPE + rightPar + EXPRESSION;

            MAP_DISPLAY.Rule = leftCor + MAP_LIST + rightCor;

            MAP_LIST.Rule = MAP_LIST + comma + EXPRESSION + colon + EXPRESSION
                            | EXPRESSION + colon + EXPRESSION;

            LIST_DISPLAY.Rule = leftCor + EXPRESSION_LIST + rightCor;

            SET_DISPLAY.Rule = leftLla + EXPRESSION_LIST + rightLla
                               | leftLla + EXPRESSION_LIST + rightLla + as_ + identifier;

            FUNCALL.Rule = identifier + leftPar + rightPar
                           | identifier + leftPar + EXPRESSION_LIST + rightPar;

            CALL.Rule = call_ + identifier + leftPar + rightPar
                        | call_ + identifier + leftPar + EXPRESSION_LIST + rightPar;

            ACCESS.Rule = PRIMARY + leftCor + EXPRESSION + rightCor;
        }
Esempio n. 28
0
        public Gramaticao() : base(caseSensitive: true)
        {
            #region ER
            RegexBasedTerminal numero = new RegexBasedTerminal("numero", "[0-9]+");
            RegexBasedTerminal deci   = new RegexBasedTerminal("decimal", "[0-9]+[.][0-9]+");
            IdentifierTerminal id     = new IdentifierTerminal("id");
            CommentTerminal    str    = new CommentTerminal("cadena", "\"", "\"");


            CommentTerminal comentarioLinea  = new CommentTerminal("comentarioLinea", "//", "\n");
            CommentTerminal comentarioBloque = new CommentTerminal("comentarioBloque", "/-", "-/");
            StringLiteral   cha = TerminalFactory.CreateCSharpChar("caracter");



            #endregion

            #region Terminales

            var url       = ToTerm("http");
            var heredar   = ToTerm("hereda_de");
            var sla       = ToTerm("~");
            var pt        = ToTerm(";");
            var dosp      = ToTerm(":");
            var par1      = ToTerm("(");
            var par2      = ToTerm(")");
            var principal = ToTerm("principal");
            var intt      = ToTerm("entero");
            var stringg   = ToTerm("cadena");
            var doublee   = ToTerm("decimal");
            var charr     = ToTerm("caracter");
            var booll     = ToTerm("booleano");
            var mas       = ToTerm("+");
            var menos     = ToTerm("-");
            var por       = ToTerm("*");
            var division  = ToTerm("/");
            var poten     = ToTerm("pow");
            var publico   = ToTerm("publico");
            var protegido = ToTerm("protegido");
            var privado   = ToTerm("privado");
            var corch1    = ToTerm("[");
            var corch2    = ToTerm("]");
            var llave1    = ToTerm("{");
            var llave2    = ToTerm("}");
            var truee     = ToTerm("true");
            var falsee    = ToTerm("false");
            var and       = ToTerm("and");
            var or        = ToTerm("or");
            var not       = ToTerm("not");
            var xor       = ToTerm("xor");
            var mayor     = ToTerm(">");
            var menor     = ToTerm("<");
            var mayori    = ToTerm(">=");
            var menori    = ToTerm("<=");
            var iguali    = ToTerm("==");
            var distinto  = ToTerm("!=");
            var imprimir  = ToTerm("imprimir");
            var metodo    = ToTerm("metodo");
            var funcion   = ToTerm("funcion");
            var retornar  = ToTerm("retorno");
            var clase     = ToTerm("clase");
            var nuevo     = ToTerm("nuevo");
            var si        = ToTerm("Si");
            var sino      = ToTerm("Sino");
            var caso      = ToTerm("caso");
            var defecto   = ToTerm("defecto");
            var mientras  = ToTerm("Mientras");
            var hacer     = ToTerm("hacer");
            var salir     = ToTerm("salir");
            var continuar = ToTerm("continuar");
            var repetir   = ToTerm("Repetir");
            var hasta     = ToTerm("until");
            var x         = ToTerm("X");
            var para      = ToTerm("Para");
            var masmas    = ToTerm("++");
            var menmen    = ToTerm("--");
            var igual     = ToTerm("=");
            var superr    = ToTerm("super");
            var importar  = ToTerm("importar");
            var self      = ToTerm("este");
            var elegir    = ToTerm("elegir");
            var llamar    = ToTerm("llamar");

            #endregion

            #region No Terminales
            NonTerminal S          = new NonTerminal("S"),
                        E          = new NonTerminal("E"),
                        DECGF      = new NonTerminal("DECGF"),
                        BLOQUE     = new NonTerminal("BLOQUE"),
                        SENTENCIA  = new NonTerminal("SENTENCIA"),
                        MOSTRAR    = new NonTerminal("MOSTRAR"),
                        LID        = new NonTerminal("LID"),
                        LVEC       = new NonTerminal("LVEC"),
                        LVEC1      = new NonTerminal("LVEC1"),
                        TIPO       = new NonTerminal("TIPO"),
                        VALOR      = new NonTerminal("VALOR"),
                        CLASE      = new NonTerminal("CLASE"),
                        FUNCIONES  = new NonTerminal("FUNCIONES"),
                        CFUN       = new NonTerminal("CFUN"),
                        LLAMAR     = new NonTerminal("LLAMAR"),
                        ASG        = new NonTerminal("ASIGNAR"),
                        ENTERES    = new NonTerminal("ENTERES"),
                        PARAMETROS = new NonTerminal("PARAMETROS"),
                        CFUNCIONES = new NonTerminal("CFUNCIONES"),
                        DEC        = new NonTerminal("DEC"),
                        DEC2       = new NonTerminal("DEC2"),
                        DECV       = new NonTerminal("DECV"),
                        DECV1      = new NonTerminal("DECV1"),
                        PRIV       = new NonTerminal("PRIVACIADA"),
                        IMPRESION  = new NonTerminal("IMPRESION"),
                        IFC        = new NonTerminal("IFC"),
                        LLAMADA    = new NonTerminal("LLAMADA"),

                        IF          = new NonTerminal("IF"),
                        FOR         = new NonTerminal("FOR"),
                        ASIGNACION  = new NonTerminal("ASIGNACION"),
                        METODOS     = new NonTerminal("METODOS"),
                        PRINCIPAL   = new NonTerminal("PRINCIPAL"),
                        LELIF       = new NonTerminal("LELIF"),
                        ELIF        = new NonTerminal("ELIF"),
                        ELSE        = new NonTerminal("ELSE"),
                        COND        = new NonTerminal("COND"),
                        ELEGIR      = new NonTerminal("ELEGIR"),
                        CASO        = new NonTerminal("CASO"),
                        CASO2       = new NonTerminal("CASO2"),
                        DEF         = new NonTerminal("DEF"),
                        RET         = new NonTerminal("RET"),
                        CICLOS      = new NonTerminal("CICLOS"),
                        SAL         = new NonTerminal("SAL"),
                        WHILE       = new NonTerminal("WHILE"),
                        DOWHILE     = new NonTerminal("DO"),
                        INCREMENTO  = new NonTerminal("INCRE"),
                        CONDIFOR    = new NonTerminal("CONDI"),
                        ATRIBUTOS   = new NonTerminal("ATRIBUTOS"),
                        ATRIBUTO1   = new NonTerminal("ATRIBUTO1"),
                        ATRIO       = new NonTerminal("ATRIO"),
                        OPEREL      = new NonTerminal("OPEREL"),
                        TABU1       = new NonTerminal("TABU"),
                        DECLARACION = new NonTerminal("DECLARACION"),
                        LSENTENCIAS = new NonTerminal("LSENTENCIAS"),
                        TIPO2       = new NonTerminal("TIPO2"),
                        PARA        = new NonTerminal("PARA"),
                        PARAMETROS1 = new NonTerminal("PARAMETROS1"),
                        CUERPO      = new NonTerminal("CUERPO"),
                        CUERPO2     = new NonTerminal("CUERPO2"),
                        LCUERPO     = new NonTerminal("LCUERPO"),
                        DECG        = new NonTerminal("DECG"),
                        CONSTRUCTOR = new NonTerminal("CONSTRUCTOR"),
                        INSTANCIA   = new NonTerminal("INSTANCIA"),
                        SUPER       = new NonTerminal("SUPER"),
                        SELF        = new NonTerminal("SELF"),
                        ATRIBUTO    = new NonTerminal("ATRIBUTO"),
                        HERENCIA    = new NonTerminal("HERENCIA"),
                        MIENTRAS    = new NonTerminal("MIENTRAS"),
                        HACER       = new NonTerminal("HACER"),
                        REPETIR     = new NonTerminal("REPETIR"),
                        LOOP        = new NonTerminal("X"),
                        MM          = new NonTerminal("MM"),
                        TABU        = new NonTerminal("TABULACIONES"),
                        IMPORT      = new NonTerminal("IMPORT"),
                        IMPORT2     = new NonTerminal("IMPORT2"),
                        PATH        = new NonTerminal("PATH"),
                        PATH2       = new NonTerminal("PATH2"),
                        URL         = new NonTerminal("URL"),
                        TIPOA       = new NonTerminal("TIPOA"),
                        PARAF       = new NonTerminal("FOR"),
                        S1          = new NonTerminal("S1"),
                        EA          = new NonTerminal("EA"),
                        VALA        = new NonTerminal("VALA"),
                        VALA2       = new NonTerminal("VALA2"),
                        LE          = new NonTerminal("LE"),
                        DEC3        = new NonTerminal("DEC3"),
                        DEC4        = new NonTerminal("DEC"),
                        DEC5        = new NonTerminal("DEC5"),
                        EC          = new NonTerminal("E"),
                        NATIVAS     = new NonTerminal("NATIVAS");

            #endregion

            #region Gramatica
            S.Rule = IMPORT2 + S1;

            S1.Rule = MakePlusRule(S1, CLASE);

            IMPORT2.Rule = MakePlusRule(IMPORT2, IMPORT) | Empty;

            IMPORT.Rule = importar + par1 + str + par2 + pt | llamar + par1 + str + par2 + pt;

            TIPOA.Rule = ToTerm("olc") | ToTerm("tree");

            PATH.Rule = MakePlusRule(PATH, sla, id);

            CLASE.Rule = clase + id + HERENCIA + llave1 + LCUERPO + llave2;

            HERENCIA.Rule = heredar + id | Empty;

            LCUERPO.Rule = MakePlusRule(LCUERPO, CUERPO);

            PRINCIPAL.Rule = principal + par1 + par2 + llave1 + BLOQUE + llave2;

            CUERPO.Rule = METODOS | DECGF | CONSTRUCTOR | PRINCIPAL;

            PRIV.Rule = protegido | privado | publico | Empty;


            PARA.Rule = TIPO2 + id
                        | TIPO2 + id + LVEC;

            PARAMETROS.Rule = MakePlusRule(PARAMETROS, ToTerm(","), PARA) | Empty;

            CONSTRUCTOR.Rule = id + par1 + PARAMETROS + par2 + llave1 + BLOQUE + llave2;

            FUNCIONES.Rule = par1 + PARAMETROS + par2 + llave1 + BLOQUE + llave2;

            METODOS.Rule = PRIV + metodo + id + par1 + PARAMETROS + par2 + llave1 + BLOQUE + llave2;

            BLOQUE.Rule = MakeStarRule(BLOQUE, SENTENCIA);

            SENTENCIA.Rule = DEC | ATRIBUTO + pt | LLAMADA + pt | ASIGNACION | SELF | CICLOS | MM + pt | salir + pt | continuar + pt | RET | IMPRESION;

            LLAMADA.Rule = id + par1 + LE + par2;

            RET.Rule = retornar + E + pt;

            INSTANCIA.Rule = nuevo + id + par1 + LE + par2;

            IMPRESION.Rule = imprimir + par1 + E + par2 + pt;


            ASIGNACION.Rule = ATRIBUTO + ASG + pt;

            SELF.Rule = self + ToTerm(".") + ATRIBUTO + ASG + pt;


            ATRIBUTO.Rule = MakePlusRule(ATRIBUTO, ToTerm("."), ATRIBUTOS);

            ATRIBUTOS.Rule = id | id + par1 + EA + par2 | id + LVEC;

            EA.Rule = LE | Empty;

            ASG.Rule = igual + E | igual + INSTANCIA;


            CICLOS.Rule = IF | PARAF | MIENTRAS | HACER | REPETIR | LOOP;

            IF.Rule = si + par1 + COND + par2 + llave1 + BLOQUE + llave2 + LELIF;

            LELIF.Rule = MakePlusRule(LELIF, ELIF) | Empty;

            ELIF.Rule = sino + si + par1 + COND + par2 + llave1 + BLOQUE + llave2 | sino + llave1 + BLOQUE + llave2;


            ELEGIR.Rule = elegir + caso + corch1 + E + corch2 + dosp + CASO + DEF;

            CASO2.Rule = E + dosp + BLOQUE;

            DEF.Rule = defecto + dosp + BLOQUE;

            CASO.Rule = MakePlusRule(CASO, CASO2);

            MIENTRAS.Rule = mientras + par1 + COND + par2 + llave1 + BLOQUE + llave2;

            HACER.Rule = hacer + llave1 + BLOQUE + llave2 + mientras + par1 + COND + par2 + pt;

            REPETIR.Rule = repetir + llave1 + BLOQUE + llave2 + hasta + par1 + COND + par2 + pt;

            PARAF.Rule = para + par1 + intt + id + igual + E + pt + COND + pt + MM + par2 + llave1 + BLOQUE + llave2;

            MM.Rule = ATRIBUTO + masmas | ATRIBUTO + menmen;

            LOOP.Rule = x + par1 + COND + ToTerm(",") + COND + par2 + llave1 + BLOQUE + llave2;


            DECGF.Rule = PRIV + TIPO2 + id + (DECG | FUNCIONES);

            DEC3.Rule = ToTerm(",") + LID | LVEC | Empty;

            DEC5.Rule = E | VALA | INSTANCIA;

            DEC4.Rule = igual + DEC5 | Empty;

            DECG.Rule = DEC3 + DEC4 + pt | LVEC + DEC4 + pt;

            LE.Rule = MakePlusRule(LE, ToTerm(","), E) | Empty;

            VALA.Rule = MakePlusRule(VALA, ToTerm(","), VALA2);

            VALA2.Rule = llave1 + LE + llave2 | llave1 + VALA + llave2;

            DEC.Rule = TIPO2 + DEC2 + DEC4 + pt;

            DEC2.Rule = LID | id + LVEC;

            LVEC1.Rule = corch1 + E + corch2;

            LVEC.Rule = MakePlusRule(LVEC, LVEC1);

            LID.Rule = MakePlusRule(LID, ToTerm(","), id);


            OPEREL.Rule = iguali
                          | mayor
                          | menor
                          | mayori
                          | menori
                          | distinto;

            E.Rule =
                E + OPEREL + E
                | E + mas + E
                | E + menos + E
                | E + por + E
                | E + division + E
                | E + poten + E
                | E + masmas
                | E + menmen
                | par1 + E + par2
                | VALOR;

            COND.Rule = COND + or + COND
                        | COND + and + COND
                        | COND + xor + COND
                        | not + COND
                        | EC + OPEREL + EC
                        | VALOR;

            EC.Rule = EC + mas + EC
                      | EC + menos + EC
                      | EC + por + EC
                      | EC + division + EC
                      | EC + poten + EC
                      | VALOR;

            VALOR.Rule = numero
                         | deci
                         | str
                         | truee
                         | falsee
                         | cha
                         | LLAMADA
                         | self + ToTerm(".") + ATRIBUTO
                         | ATRIBUTO;

            TIPO.Rule = intt
                        | stringg
                        | charr
                        | booll
                        | doublee;
            TIPO2.Rule = TIPO
                         | id;

            #endregion

            #region Preferencias
            this.Root = S;
            this.NonGrammarTerminals.Add(comentarioLinea);
            this.NonGrammarTerminals.Add(comentarioBloque);
            this.MarkTransient(VALOR, PRIV, TIPO, TIPO2, SENTENCIA, CUERPO2, BLOQUE, CICLOS, CUERPO, PARAMETROS1, EA);
            this.RegisterOperators(2, Associativity.Left, mas, menos);
            this.RegisterOperators(3, Associativity.Left, por, division);
            this.RegisterOperators(4, Associativity.Left, poten);
            this.RegisterOperators(6, Associativity.Left, and);
            this.RegisterOperators(7, Associativity.Left, or);
            this.RegisterOperators(8, Associativity.Left, xor);
            this.RegisterOperators(9, Associativity.Left, not);
            this.RegisterOperators(10, Associativity.Left, iguali, distinto, mayor, menor, mayori, menori);
            this.MarkPunctuation(":", ".", "llamar", "hereda_de", "=", "Para", "X", "Si", "Sino", "Mientras", "Repetir", "until", "este", "metodo", "principal", "imprimir", ";", ",", "[", "]", "(", ")", "~", "{", "}", "void", "funcion", "clase", "si", "si_no_si", "si_no", "elegir", "caso", "defecto", "mientras", "para", "hacer", "repetir", "hasta", "loop");
            #endregion
        }
Esempio n. 29
0
        public TuringGrammarBroken()
        {
            //Expr -> n | v | Expr BinOp Expr | UnOP Expr | ( Expr )
            //BinOp -> + | - | * | / | **
            //UnOp -> -
            //ExprLine -> Expr EOF
            MarkReservedWords("not=");
            //1. Terminals
            Terminal        num               = new NumberLiteral("number");
            Terminal        iden              = new IdentifierTerminal("identifier");
            Terminal        stringLiteral     = TerminalFactory.CreateCSharpString("stringLiteral");
            CommentTerminal SingleLineComment = new CommentTerminal("SingleLineComment", "%", "\r", "\n", "\u2085", "\u2028", "\u2029");
            CommentTerminal DelimitedComment  = new CommentTerminal("DelimitedComment", "/*", "*/");

            NonGrammarTerminals.Add(SingleLineComment);
            NonGrammarTerminals.Add(DelimitedComment);

            //2. Non-Terminals
            var expandedIdentifier = new NonTerminal("expandedIndentifier");
            var Expr      = new NonTerminal("expr");
            var BinOp     = new NonTerminal("binOp", "operator");
            var BinExpr   = new NonTerminal("binExpr");
            var unOp      = new NonTerminal("unOp");
            var unExpr    = new NonTerminal("unExpr");
            var ParExpr   = new NonTerminal("parExpr");
            var Statement = new NonTerminal("statement");
            var Program   = new NonTerminal("program");
            var setEqual  = new NonTerminal("setEqual");

            var varType             = new NonTerminal("varType");
            var variableDeclaration = new NonTerminal("variableDeclaration");
            var varOrConst          = new NonTerminal("varOrConst");
            var idenList            = new NonTerminal("identifierList");
            var assignment          = new NonTerminal("assignment");
            var typeSpecifier       = new NonTerminal("typeSpecifier");

            var ifBlock      = new NonTerminal("ifBlock");
            var elseIfBlock  = new NonTerminal("elseIfBlock");
            var optElseBlock = new NonTerminal("optElseBlock");

            var caseBlock  = new NonTerminal("caseBlock");
            var labelBlock = new NonTerminal("labelBlock");

            var functionCall       = new NonTerminal("functionCall");
            var optArgs            = new NonTerminal("optArgs");
            var args               = new NonTerminal("args");
            var functionDefinition = new NonTerminal("functionDefinition");
            var optParams          = new NonTerminal("optParams");
            var parameters         = new NonTerminal("parameters");
            var parameter          = new NonTerminal("parameter");

            var io          = new NonTerminal("io");
            var optSameLine = new NonTerminal("optionalSameLine");

            var loop     = new NonTerminal("loop");
            var forLoop  = new NonTerminal("forLoop");
            var exitLoop = new NonTerminal("exitLoop");

            var and        = new NonTerminal("and");
            var or         = new NonTerminal("or");
            var not        = new NonTerminal("not");
            var result     = new NonTerminal("result");
            var recordList = new NonTerminal("recordList");
            var type       = new NonTerminal("type");
            var memberCall = new NonTerminal("memberCall");
            var range      = new NonTerminal("range");
            var boolean    = new NonTerminal("boolean");
            var ioArgs     = new NonTerminal("ioArgs");
            var newer      = new NonTerminal("new");

            //3. BNF rules
            varType.Rule = ToTerm("int") | "nat" | "string" | "real" | "boolean" | "array" + range + "of" + varType | ToTerm("flexible") + "array" + range + "of" + varType | "record" + recordList + "end" + "record" |
                           "int1" | "int2" | "int4" | "real1" | "real2" | "real4" | "nat1" | "nat2" | "nat4" | ToTerm("char") + "(" + num + ")" | ToTerm("string") + "(" + num + ")" | iden;
            range.Rule         = Expr + ".." + Expr;
            setEqual.Rule      = ToTerm(":=") + Expr;
            typeSpecifier.Rule = ToTerm(":") + varType;
            Expr.Rule          = num | iden | BinExpr | ParExpr | stringLiteral | unExpr | functionCall | memberCall | boolean;
            BinOp.Rule         = ToTerm("-") | "*" | "/" | "**" | "+" | "div" | "mod" | and | or | "=" | ">" | "<" | ">=" | "<=" | "~=" | "not=";
            BinOp.Precedence   = 1;
            unOp.Rule          = not | "-";
            unOp.Precedence    = 2;

            BinExpr.Rule = Expr + BinOp + Expr;
            unExpr.Rule  = unOp + Expr;
            ParExpr.Rule = "(" + Expr + ")";
            boolean.Rule = ToTerm("true") | "false";

            assignment.Rule = expandedIdentifier + setEqual;

            optArgs.Rule = args | Empty;
            args.Rule    = MakePlusRule(args, ToTerm(","), Expr);
            //args.Rule = Expr + "," + args | Expr;
            functionCall.Rule = iden + "(" + optArgs + ")";

            optSameLine.Rule = ToTerm("..") | Empty;
            io.Rule          = ToTerm("put") + args + optSameLine | ToTerm("get") + args | ToTerm("put") + ":" + args + optSameLine | ToTerm("get") + ":" + args | ToTerm("open") + ":" + iden + "," + Expr + "," + ioArgs;
            ioArgs.Rule      = ToTerm("get") | "put" | "write" | "read" | "seek" | "tell" | ioArgs + "," + ioArgs;

            newer.Rule = ToTerm("new") + iden + "," + Expr;

            optParams.Rule          = ToTerm("(") + parameters + ")" | ToTerm("(") + ")" | Empty;
            parameters.Rule         = parameter + "," + parameters | parameter;
            parameter.Rule          = idenList + typeSpecifier | "var" + idenList + typeSpecifier;
            functionDefinition.Rule = "function" + iden + optParams + typeSpecifier + Program + "end" + iden
                                      | "fcn" + iden + optParams + typeSpecifier + Program + "end" + iden
                                      | "procedure" + iden + optParams + Program + "end" + iden
                                      | "proc" + iden + optParams + Program + "end" + iden;

            ifBlock.Rule      = ToTerm("if") + Expr + ToTerm("then") + Program + elseIfBlock + optElseBlock + ToTerm("end") + "if";
            elseIfBlock.Rule  = ToTerm("elsif") + Expr + ToTerm("then") + Program + elseIfBlock | Empty;
            optElseBlock.Rule = ToTerm("else") + Program | Empty;

            caseBlock.Rule  = ToTerm("case") + iden + "of" + labelBlock + "end case";
            labelBlock.Rule = ToTerm("label") + Expr + ":" + Program + labelBlock | ToTerm("label") + ":" + labelBlock | Empty;

            idenList.Rule            = iden + "," + idenList | iden;
            varOrConst.Rule          = ToTerm("var") | "const";
            variableDeclaration.Rule = varOrConst + idenList | varOrConst + idenList + setEqual | varOrConst + idenList + typeSpecifier | varOrConst + idenList + typeSpecifier + setEqual;

            loop.Rule     = "loop" + Program + "end" + "loop";
            forLoop.Rule  = "for" + ("decreasing" | Empty) + iden + ":" + (range | iden) + Program + "end" + "for";
            exitLoop.Rule = ToTerm("exit") | ToTerm("exit") + "when" + Expr;

            and.Rule        = ToTerm("and") | "&";
            or.Rule         = ToTerm("or") | "|";
            not.Rule        = ToTerm("not") | "~" | "!";
            result.Rule     = "result" + Expr | "return" + Expr;
            recordList.Rule = iden + typeSpecifier + recordList | Empty;
            type.Rule       = "type" + iden + typeSpecifier;
            memberCall.Rule = expandedIdentifier + "." + expandedIdentifier;
            //memberCall.Rule = iden + "." + functionCall | iden + "." + iden | iden + "." + memberCall;

            expandedIdentifier.Rule = iden | functionCall | memberCall;
            Statement.Rule          = functionCall | memberCall | iden | variableDeclaration | ifBlock | caseBlock | functionCall | functionDefinition | io | assignment | result | loop | forLoop | type | newer | exitLoop;
            Program.Rule            = Statement + Program | Empty;
            this.Root = Program;

            //4. Set operator precendence and associativity
            RegisterOperators(05, Associativity.Left, "not");
            RegisterOperators(10, Associativity.Left, "=", "not=");
            RegisterOperators(30, Associativity.Left, "+", "-");
            RegisterOperators(40, Associativity.Left, "*", "/", "div", "mod");
            RegisterOperators(50, Associativity.Right, "**");


            //5. Register Parenthesis as punctuation symbols so they will not appear in the syntax tree
            MarkPunctuation("(", ")", ",");
            RegisterBracePair("(", ")");
            MarkTransient(Expr, BinOp, ParExpr);

            this.LanguageFlags = LanguageFlags.NewLineBeforeEOF;
        }
Esempio n. 30
0
        public CilGrammar()
            : base(true)
        {
            LanguageFlags = LanguageFlags.CreateAst;

            // comments

            var SINGLELINECOMMENT = new CommentTerminal("SINGLELINECOMMENT", "//", "\n", "\r\n");

            ConfigureAstNode(SINGLELINECOMMENT);

            NonGrammarTerminals.Add(SINGLELINECOMMENT);

            // lexical tokens

            var HEXBYTE = new RegexBasedTerminal("HEXBYTE", @"[A-F0-9]{2}"); // DOCS: not specified in ECMA grammar

            ConfigureAstNode(HEXBYTE);

            var DOTTEDNAME = CreateNonTerminal("DOTTEDNAME");

            DOTTEDNAME.Rule = _("TODO: DOTTEDNAME");
            ConfigureAstNode(DOTTEDNAME);

            var ID = new IdentifierTerminal("ID");

            ID.AddPrefix("$", IdOptions.None); // DOCS: ECMA page 110
            ConfigureAstNode(ID);

            var QSTRING = new StringLiteral("QSTRING", "\"");

            ConfigureAstNode(QSTRING);

            var SQSTRING = new StringLiteral("SQSTRING", "'");

            ConfigureAstNode(SQSTRING);

            var INT32 = new NumberLiteral("INT32", NumberOptions.AllowSign | NumberOptions.IntOnly);

            INT32.AddPrefix("0x", NumberOptions.Hex);
            INT32.DefaultIntTypes = new TypeCode[] { TypeCode.Int32 };
            ConfigureAstNode(INT32);

            var INT64 = new NumberLiteral("INT64", NumberOptions.AllowSign | NumberOptions.IntOnly);

            INT64.AddPrefix("0x", NumberOptions.Hex);
            INT64.DefaultIntTypes = new TypeCode[] { TypeCode.Int64 };
            ConfigureAstNode(INT64);

            var FLOAT64 = new NumberLiteral("FLOAT64", NumberOptions.AllowStartEndDot | NumberOptions.AllowSign);

            ConfigureAstNode(FLOAT64);

            // non-terminals

            var decls               = CreateNonTerminal("decls");
            var decl                = CreateNonTerminal("decl");
            var compQstring         = CreateNonTerminal("compQstring");
            var languageDecl        = CreateNonTerminal("languageDecl");
            var customAttrDecl      = CreateNonTerminal("customAttrDecl");
            var moduleHead          = CreateNonTerminal("moduleHead");
            var vtfixupDecl         = CreateNonTerminal("vtfixupDecl");
            var vtableDecl          = CreateNonTerminal("vtableDecl");
            var nameSpaceHead       = CreateNonTerminal("nameSpaceHead");
            var classHead           = CreateNonTerminal("classHead");
            var classAttr           = CreateNonTerminal("classAttr");
            var extendsClause       = CreateNonTerminal("extendsClause");
            var implClause          = CreateNonTerminal("implClause");
            var classNames          = CreateNonTerminal("classNames");
            var classDecls          = CreateNonTerminal("classDecls");
            var classDecl           = CreateNonTerminal("classDecl");
            var fieldDecl           = CreateNonTerminal("fieldDecl");
            var atOpt               = CreateNonTerminal("atOpt");
            var initOpt             = CreateNonTerminal("initOpt");
            var repeatOpt           = CreateNonTerminal("repeatOpt");
            var customHead          = CreateNonTerminal("customHead");
            var customHeadWithOwner = CreateNonTerminal("customHeadWithOwner");
            var memberRef           = CreateNonTerminal("memberRef");
            var customType          = CreateNonTerminal("customType");
            var ownerType           = CreateNonTerminal("ownerType");
            var eventHead           = CreateNonTerminal("eventHead");
            var eventDecls          = CreateNonTerminal("eventDecls");
            var propHead            = CreateNonTerminal("propHead");
            var propDecls           = CreateNonTerminal("propDecls");
            var methodHeadPart1     = CreateNonTerminal("methodHeadPart1");
            var methodHead          = CreateNonTerminal("methodHead");
            var methAttr            = CreateNonTerminal("methAttr");
            var pinvAttr            = CreateNonTerminal("pinvAttr");
            var methodName          = CreateNonTerminal("methodName");
            var paramAttr           = CreateNonTerminal("paramAttr");
            var fieldAttr           = CreateNonTerminal("fieldAttr");
            var implAttr            = CreateNonTerminal("implAttr");
            var localsHead          = CreateNonTerminal("localsHead");
            var methodDecl          = CreateNonTerminal("methodDecl");
            var scopeBlock          = CreateNonTerminal("scopeBlock");
            var sehBlock            = CreateNonTerminal("sehBlock");
            var methodDecls         = CreateNonTerminal("methodDecls");
            var dataDecl            = CreateNonTerminal("dataDecl");
            var ddHead              = CreateNonTerminal("ddHead");
            var tls                = CreateNonTerminal("tls");
            var ddBody             = CreateNonTerminal("ddBody");
            var ddItemList         = CreateNonTerminal("ddItemList");
            var ddItemCount        = CreateNonTerminal("ddItemCount");
            var ddItem             = CreateNonTerminal("ddItem");
            var fieldInit          = CreateNonTerminal("fieldInit");
            var bytearrayhead      = CreateNonTerminal("bytearrayhead");
            var bytes              = CreateNonTerminal("bytes");
            var hexbytes           = CreateNonTerminal("hexbytes");
            var instr_r_head       = CreateNonTerminal("instr_r_head");
            var instr_tok_head     = CreateNonTerminal("instr_tok_head");
            var methodSpec         = CreateNonTerminal("methodSpec");
            var instr              = CreateNonTerminal("instr");
            var sigArgs0           = CreateNonTerminal("sigArgs0");
            var sigArgs1           = CreateNonTerminal("sigArgs1");
            var sigArg             = CreateNonTerminal("sigArg");
            var name1              = CreateNonTerminal("name1");
            var className          = CreateNonTerminal("className");
            var slashedName        = CreateNonTerminal("slashedName");
            var typeSpec           = CreateNonTerminal("typeSpec");
            var callConv           = CreateNonTerminal("callConv");
            var callKind           = CreateNonTerminal("callKind");
            var nativeType         = CreateNonTerminal("nativeType");
            var type               = CreateNonTerminal("type");
            var bounds1            = CreateNonTerminal("bounds1");
            var bound              = CreateNonTerminal("bound");
            var labels             = CreateNonTerminal("labels");
            var id                 = CreateNonTerminal("id");
            var int16s             = CreateNonTerminal("int16s");
            var int32              = CreateNonTerminal("int32");
            var int64              = CreateNonTerminal("int64");
            var float64            = CreateNonTerminal("float64");
            var secDecl            = CreateNonTerminal("secDecl");
            var psetHead           = CreateNonTerminal("psetHead");
            var nameValPairs       = CreateNonTerminal("nameValPairs");
            var nameValPair        = CreateNonTerminal("nameValPair");
            var truefalse          = CreateNonTerminal("truefalse");
            var caValue            = CreateNonTerminal("caValue");
            var secAction          = CreateNonTerminal("secAction");
            var extSourceSpec      = CreateNonTerminal("extSourceSpec");
            var fileDecl           = CreateNonTerminal("fileDecl");
            var hashHead           = CreateNonTerminal("hashHead");
            var assemblyHead       = CreateNonTerminal("assemblyHead");
            var asmAttr            = CreateNonTerminal("asmAttr");
            var assemblyDecls      = CreateNonTerminal("assemblyDecls");
            var assemblyDecl       = CreateNonTerminal("assemblyDecl");
            var asmOrRefDecl       = CreateNonTerminal("asmOrRefDecl");
            var publicKeyHead      = CreateNonTerminal("publicKeyHead");
            var publicKeyTokenHead = CreateNonTerminal("publicKeyTokenHead");
            var localeHead         = CreateNonTerminal("localeHead");
            var assemblyRefHead    = CreateNonTerminal("assemblyRefHead");
            var assemblyRefDecls   = CreateNonTerminal("assemblyRefDecls");
            var assemblyRefDecl    = CreateNonTerminal("assemblyRefDecl");
            var comtypeHead        = CreateNonTerminal("comtypeHead");
            var exportHead         = CreateNonTerminal("exportHead");
            var comtypeDecls       = CreateNonTerminal("comtypeDecls");
            var manifestResHead    = CreateNonTerminal("manifestResHead");
            var manresAttr         = CreateNonTerminal("manresAttr");
            var manifestResDecls   = CreateNonTerminal("manifestResDecls");
            var manifestResDecl    = CreateNonTerminal("manifestResDecl");

            // instructions

            var INSTR_NONE     = CreateNonTerminal("INSTR_NONE");
            var INSTR_VAR      = CreateNonTerminal("INSTR_VAR");
            var INSTR_I        = CreateNonTerminal("INSTR_I");
            var INSTR_I8       = CreateNonTerminal("INSTR_I8");
            var INSTR_R        = CreateNonTerminal("INSTR_R");
            var INSTR_BRTARGET = CreateNonTerminal("INSTR_BRTARGET");
            var INSTR_METHOD   = CreateNonTerminal("INSTR_METHOD");
            var INSTR_FIELD    = CreateNonTerminal("INSTR_FIELD");
            var INSTR_TYPE     = CreateNonTerminal("INSTR_TYPE");
            var INSTR_STRING   = CreateNonTerminal("INSTR_STRING");
            var INSTR_SIG      = CreateNonTerminal("INSTR_SIG");
            var INSTR_RVA      = CreateNonTerminal("INSTR_RVA");
            var INSTR_SWITCH   = CreateNonTerminal("INSTR_SWITCH");
            var INSTR_PHI      = CreateNonTerminal("INSTR_PHI");
            var INSTR_TOK      = CreateNonTerminal("INSTR_TOK");

            INSTR_NONE.Rule =
                _("add") |
                ___("add.ovf") |
                ___("add.ovf.un") |
                _("and") |
                _("arglist") |
                _("break") |
                _("ceq") |
                _("cgt") |
                ___("cgt.un") |
                _("ckfinite") |
                _("clt") |
                ___("clt.un") |
                ___("conv.i") |
                ___("conv.i1") |
                ___("conv.i2") |
                ___("conv.i4") |
                ___("conv.i8") |
                ___("conv.ovf.i") |
                ___("conv.ovf.i.un") |
                ___("conv.ovf.i1") |
                ___("conv.ovf.i1.un") |
                ___("conv.ovf.i2") |
                ___("conv.ovf.i2.un") |
                ___("conv.ovf.i4") |
                ___("conv.ovf.i4.un") |
                ___("conv.ovf.i8") |
                ___("conv.ovf.i8.un") |
                ___("conv.ovf.u") |
                ___("conv.ovf.u.un") |
                ___("conv.ovf.u1") |
                ___("conv.ovf.u1.un") |
                ___("conv.ovf.u2") |
                ___("conv.ovf.u2.un") |
                ___("conv.ovf.u4") |
                ___("conv.ovf.u4.un") |
                ___("conv.ovf.u8") |
                ___("conv.ovf.u8.un") |
                ___("conv.r.un") |
                ___("conv.r4") |
                ___("conv.r8") |
                ___("conv.u") |
                ___("conv.u1") |
                ___("conv.u2") |
                ___("conv.u4") |
                ___("conv.u8") |
                _("cpblk") |
                _("div") |
                ___("div.un") |
                _("dup") |
                _("endfault") |
                _("endfilter") |
                _("endfinally") |
                _("initblk") |
                ___("ldarg.0") |
                ___("ldarg.1") |
                ___("ldarg.2") |
                ___("ldarg.3") |
                ___("ldc.i4.0") |
                ___("ldc.i4.1") |
                ___("ldc.i4.2") |
                ___("ldc.i4.3") |
                ___("ldc.i4.4") |
                ___("ldc.i4.5") |
                ___("ldc.i4.6") |
                ___("ldc.i4.7") |
                ___("ldc.i4.8") |
                ___("ldc.i4.M1") |
                ___("ldc.i4.m1") | // DOCS: non present in ECMA grammar
                ___("ldelem.i") |
                ___("ldelem.i1") |
                ___("ldelem.i2") |
                ___("ldelem.i4") |
                ___("ldelem.i8") |
                ___("ldelem.r4") |
                ___("ldelem.r8") |
                ___("ldelem.ref") |
                ___("ldelem.u1") |
                ___("ldelem.u2") |
                ___("ldelem.u4") |
                ___("ldind.i") |
                ___("ldind.i1") |
                ___("ldind.i2") |
                ___("ldind.i4") |
                ___("ldind.i8") |
                ___("ldind.r4") |
                ___("ldind.r8") |
                ___("ldind.ref") |
                ___("ldind.u1") |
                ___("ldind.u2") |
                ___("ldind.u4") |
                _("ldlen") |
                ___("ldloc.0") |
                ___("ldloc.1") |
                ___("ldloc.2") |
                ___("ldloc.3") |
                _("ldnull") |
                _("localloc") |
                _("mul") |
                ___("mul.ovf") |
                ___("mul.ovf.un") |
                _("neg") |
                _("nop") |
                _("not") |
                _("or") |
                _("pop") |
                _("refanytype") |
                _("rem") |
                ___("rem.un") |
                _("ret") |
                _("rethrow") |
                _("shl") |
                _("shr") |
                ___("shr.un") |
                ___("stelem.i") |
                ___("stelem.i1") |
                ___("stelem.i2") |
                ___("stelem.i4") |
                ___("stelem.i8") |
                ___("stelem.r4") |
                ___("stelem.r8") |
                ___("stelem.ref") |
                ___("stind.i") |
                ___("stind.i1") |
                ___("stind.i2") |
                ___("stind.i4") |
                ___("stind.i8") |
                ___("stind.r4") |
                ___("stind.r8") |
                ___("stind.ref") |
                ___("stloc.0") |
                ___("stloc.1") |
                ___("stloc.2") |
                ___("stloc.3") |
                _("sub") |
                ___("sub.ovf") |
                ___("sub.ovf.un") |
                _("tail.") |
                _("throw") |
                _("volatile.") |
                _("xor");

            INSTR_VAR.Rule =
                _("ladrg") |
                ___("ldarg.s") |
                _("ldarga") |
                ___("ldarga.s") |
                _("ldloc") |
                ___("ldloc.s") |
                _("ldloca") |
                ___("ldloca.s") |
                _("starg") |
                ___("starg.s") |
                _("stloc") |
                ___("stloc.s");

            INSTR_I.Rule =
                ___("ldc.i4") |
                ___("ldc.i4.s") |
                _("unaligned.");

            INSTR_I8.Rule =
                ___("ldc.i8");

            INSTR_R.Rule =
                ___("ldc.r4") |
                ___("ldc.r8");

            INSTR_BRTARGET.Rule =
                _("beq") |
                ___("beq.s") |
                _("bge") |
                ___("bge.s") |
                ___("bge.un") |
                ___("bge.un.s") |
                _("bgt") |
                ___("bgt.s") |
                ___("bgt.un") |
                ___("bgt.un.s") |
                _("ble") |
                ___("ble.s") |
                ___("ble.un") |
                ___("ble.un.s") |
                _("blt") |
                ___("blt.s") |
                ___("blt.un") |
                ___("blt.un.s") |
                ___("bne.un") |
                ___("bne.un.s") |
                _("br") |
                ___("br.s") |
                _("brfalse") |
                ___("brfalse.s") |
                _("brtrue") |
                ___("brtrue.s") |
                _("leave") |
                ___("leave.s");

            INSTR_METHOD.Rule =
                _("call") |
                _("callvirt") |
                _("jmp") |
                _("ldftn") |
                _("ldvirtftn") |
                _("newobj");

            INSTR_FIELD.Rule =
                _("ldfld") |
                _("ldflda") |
                _("ldsfld") |
                _("ldsflda") |
                _("stfld") |
                _("stsfld");

            INSTR_TYPE.Rule =
                _("box") |
                _("castclass") |
                _("cpobj") |
                _("initobj") |
                _("isinst") |
                _("ldelem") | // DOCS: non-present in ECMA grammar
                _("ldelema") |
                _("ldobj") |
                _("mkrefany") |
                _("newarr") |
                _("refanyval") |
                _("sizeof") |
                _("stelem") | // DOCS: non-present in ECMA grammar
                _("stobj") |
                _("unbox") |
                ___("unbox.any"); // DOCS: non-present in ECMA grammar

            INSTR_STRING.Rule =
                _("ldstr");

            INSTR_SIG.Rule = _("TODO: INSTR_SIG");

            INSTR_RVA.Rule = _("TODO: INSTR_RVA");

            INSTR_SWITCH.Rule =
                _("switch");

            INSTR_PHI.Rule = _("TODO: INSTR_PHI");

            INSTR_TOK.Rule =
                _("ldtoken");

            // rules

            Root = decls;

            decls.Rule =
                Empty |
                decls + decl;

            decl.Rule =
                classHead + _("{") + classDecls + _("}") |
                nameSpaceHead + _("{") + decls + _("}") |
                methodHead + methodDecls + _("}") |
                fieldDecl |
                dataDecl |
                vtableDecl |
                vtfixupDecl |
                extSourceSpec |
                fileDecl |
                assemblyHead + _("{") + assemblyDecls + _("}") |
                assemblyRefHead + _("{") + assemblyRefDecls + _("}") |
                comtypeHead + _("{") + comtypeDecls + _("}") |
                manifestResHead + _("{") + manifestResDecls + _("}") |
                moduleHead |
                secDecl |
                customAttrDecl |
                _(".subsystem") + int32 |
                _(".corflags") + int32 |
                _(".file") + _("alignment") + int32 |
                _(".imagebase") + int64 |
                languageDecl |
                _(".stackreserve") + int64; // DOCS: not present in ECMA grammar

            compQstring.Rule =
                QSTRING |
                compQstring + _("+") + QSTRING;

            // TODO - languageDecl
            languageDecl.Rule = _("TODO: languageDecl");

            customAttrDecl.Rule =
                _(".custom") + customType |
                _(".custom") + customType + _("=") + compQstring |
                customHead + bytes + _(")") |
                _(".custom") + _("(") + ownerType + _(")") + customType |
                _(".custom") + _("(") + ownerType + _(")") + customType + _("=") + compQstring |
                customHeadWithOwner + bytes + _(")");

            moduleHead.Rule =
                _(".module") |
                _(".module") + name1 |
                _(".module") + _("extern") + name1;

            // TODO - vtfixupDecl
            vtfixupDecl.Rule = _("TODO: vtfixupDecl");

            // TODO - vtableDecl
            vtableDecl.Rule = _("TODO: vtableDecl");

            // TODO - nameSpaceHead
            nameSpaceHead.Rule = _("TODO: nameSpaceHead");

            classHead.Rule =
                _(".class") + classAttr + id + extendsClause + implClause |
                _(".class") + classAttr + name1 + extendsClause + implClause; // DOCS: not present in ECMA grammar

            classAttr.Rule =
                Empty |
                classAttr + _("public") |
                classAttr + _("private") |
                classAttr + _("value") |
                classAttr + _("enum") |
                classAttr + _("interface") |
                classAttr + _("sealed") |
                classAttr + _("abstract") |
                classAttr + _("auto") |
                classAttr + _("sequential") |
                classAttr + _("explicit") |
                classAttr + _("ansi") |
                classAttr + _("unicode") |
                classAttr + _("autochar") |
                classAttr + _("import") |
                classAttr + _("serializable") |
                classAttr + _("nested") + _("public") |
                classAttr + _("nested") + _("private") |
                classAttr + _("nested") + _("family") |
                classAttr + _("nested") + _("assembly") |
                classAttr + _("nested") + _("famandassem") |
                classAttr + _("nested") + _("famorassem") |
                classAttr + _("beforefieldinit") |
                classAttr + _("specialname") |
                classAttr + _("rtspecialname");

            extendsClause.Rule =
                Empty |
                _("extends") + className;

            implClause.Rule =
                Empty |
                _("implements") + classNames;

            classNames.Rule =
                className + _(",") + className |
                className;

            classDecls.Rule =
                Empty |
                classDecls + classDecl;

            classDecl.Rule =
                methodHead + methodDecls + _("}") |
                classHead + _("{") + classDecls + _("}") |
                eventHead + _("{") + eventDecls + _("}") |
                propHead + _("{") + propDecls + _("}") |
                fieldDecl |
                dataDecl |
                secDecl |
                extSourceSpec |
                customAttrDecl |
                _(".size") + int32 |
                _(".pack") + int32 |
                exportHead + _("{") + comtypeDecls + _("}") |
                _(".override") + typeSpec + _("::") + methodName + _("with") + callConv + type + typeSpec + _("::") + methodName + _("(") + sigArgs0 + _(")") |
                languageDecl;

            fieldDecl.Rule =
                _(".field") + repeatOpt + fieldAttr + type + id + atOpt + initOpt;

            atOpt.Rule =
                Empty |
                _("at") + id;

            initOpt.Rule =
                Empty |
                _("=") + fieldInit;

            repeatOpt.Rule =
                Empty |
                _("[") + int32 + _("]");

            customHead.Rule =
                _(".custom") + customType + _("=") + _("(");

            customHeadWithOwner.Rule =
                _(".custom") + _("(") + ownerType + _(")") + customType + _("=") + _("(");

            memberRef.Rule =
                methodSpec + callConv + type + typeSpec + _("::") + methodName + _("(") + sigArgs0 + _(")") |
                methodSpec + callConv + type + methodName + _("(") + sigArgs0 + _(")") |
                _("field") + type + typeSpec + _("::") + id |
                _("field") + type + id;

            customType.Rule =
                callConv + type + typeSpec + _("::") + _(".ctor") + _("(") + sigArgs0 + _(")") |
                callConv + type + _(".ctor") + _("(") + sigArgs0 + _(")");

            ownerType.Rule =
                typeSpec |
                memberRef;

            // TODO - eventHead
            eventHead.Rule = _("TODO: eventHead");

            // TODO - eventDecls
            eventDecls.Rule = _("TODO: eventDecls");

            // TODO - propHead
            propHead.Rule = _("TODO: propHead");

            // TODO - propDecls
            propDecls.Rule = _("TODO: propDecls");

            methodHeadPart1.Rule =
                _(".method");

            methodHead.Rule =
                methodHeadPart1 + methAttr + callConv + paramAttr + type + methodName + _("(") + sigArgs0 + _(")") + implAttr + _("{") |
                methodHeadPart1 + methAttr + callConv + paramAttr + type + _("marshal") + _("(") + nativeType + _(")") + methodName + _("(") + sigArgs0 + _(")") + implAttr + _("{");

            methAttr.Rule =
                Empty |
                methAttr + _("static") |
                methAttr + _("public") |
                methAttr + _("private") |
                methAttr + _("family") |
                methAttr + _("final") |
                methAttr + _("specialname") |
                methAttr + _("virtual") |
                methAttr + _("abstract") |
                methAttr + _("assembly") |
                methAttr + _("famandassem") |
                methAttr + _("famorassem") |
                methAttr + _("privatescope") |
                methAttr + _("hidebysig") |
                methAttr + _("newslot") |
                methAttr + _("rtspecialname") |
                methAttr + _("unmanagedexp") |
                methAttr + _("reqsecobj") |
                methAttr + _("pinvokeimpl") + _("(") + compQstring + _("as") + compQstring + pinvAttr + _(")") |
                methAttr + _("pinvokeimpl") + _("(") + compQstring + pinvAttr + _(")") |
                methAttr + _("pinvokeimpl") + _("(") + pinvAttr + _(")");

            // TODO - pinvAttr
            pinvAttr.Rule = _("TODO: pinvAttr");

            methodName.Rule =
                _(".ctor") |
                _(".cctor") |
                name1;

            fieldAttr.Rule =
                Empty |
                fieldAttr + _("static") |
                fieldAttr + _("public") |
                fieldAttr + _("private") |
                fieldAttr + _("family") |
                fieldAttr + _("initonly") |
                fieldAttr + _("rtspecialname") |
                fieldAttr + _("specialname") |
                fieldAttr + _("pinvokeimpl") + _("(") + compQstring + _("as") + compQstring + pinvAttr + _(")") |
                fieldAttr + _("pinvokeimpl") + _("(") + compQstring + pinvAttr + _(")") |
                fieldAttr + _("pinvokeimpl") + _("(") + pinvAttr + _(")") |
                fieldAttr + _("marshal") + _("(") + nativeType + _(")") |
                fieldAttr + _("assembly") |
                fieldAttr + _("famandassem") |
                fieldAttr + _("famorassem") |
                fieldAttr + _("privatescope") |
                fieldAttr + _("literal") |
                fieldAttr + _("notserialized");

            paramAttr.Rule =
                Empty |
                paramAttr + _("[") + _("in") + _("]") |
                paramAttr + _("[") + _("out") + _("]") |
                paramAttr + _("[") + _("opt") + _("]") |
                paramAttr + _("[") + int32 + _("]");

            implAttr.Rule =
                Empty |
                implAttr + _("native") |
                implAttr + _("cil") |
                implAttr + _("optil") |
                implAttr + _("managed") |
                implAttr + _("unmanaged") |
                implAttr + _("forwardref") |
                implAttr + _("preservesig") |
                implAttr + _("runtime") |
                implAttr + _("internalcall") |
                implAttr + _("synchronized") |
                implAttr + _("noinlining");

            localsHead.Rule =
                _(".locals");

            methodDecl.Rule =
                _(".emitbyte") + int32 |
                sehBlock |
                _(".maxstack") + int32 |
                localsHead + _("(") + sigArgs0 + _(")") |
                localsHead + _("init") + _("(") + sigArgs0 + _(")") |
                _(".entrypoint") |
                _(".zeroinit") |
                dataDecl |
                instr |
                id + _(":") |
                secDecl |
                extSourceSpec |
                languageDecl |
                customAttrDecl |
                _(".export") + _("[") + int32 + _("]") |
                _(".export") + _("[") + int32 + _("]") + _("as") + id |
                _(".vtentry") + int32 + _(":") + int32 |
                _(".override") + typeSpec + _("::") + methodName |
                scopeBlock |
                _(".param") + _("[") + int32 + _("]") + initOpt;

            // TODO - scopeBlock
            scopeBlock.Rule = _("TODO: scopeBlock");

            // TODO - sehBlock
            sehBlock.Rule = _("TODO: sehBlock");

            methodDecls.Rule =
                Empty |
                methodDecls + methodDecl;

            dataDecl.Rule =
                ddHead + ddBody;

            ddHead.Rule =
                _(".data") + tls + id + _("=") |
                _(".data") + tls;

            tls.Rule =
                Empty |
                _("tls") |
                _("cil"); // DOCS: non-present in ECMA grammar

            ddBody.Rule =
                _("{") + ddItemList + _("}") |
                ddItem;

            ddItemList.Rule =
                ddItem + _(",") + ddItemList |
                ddItem;

            ddItemCount.Rule =
                Empty |
                _("[") + int32 + _("]");

            ddItem.Rule =
                _("char") + _("*") + _("(") + compQstring + _(")") |
                _("&") + _("(") + id + _(")") |
                bytearrayhead + bytes + _(")") |
                _("float32") + _("(") + float64 + _(")") + ddItemCount |
                _("float64") + _("(") + float64 + _(")") + ddItemCount |
                _("int64") + _("(") + int64 + _(")") + ddItemCount |
                _("int32") + _("(") + int32 + _(")") + ddItemCount |
                _("int16") + _("(") + int32 + _(")") + ddItemCount |
                _("int8") + _("(") + int32 + _(")") + ddItemCount |
                _("float32") + ddItemCount |
                _("float64") + ddItemCount |
                _("int64") + ddItemCount |
                _("int32") + ddItemCount |
                _("int16") + ddItemCount |
                _("int8") + ddItemCount;

            fieldInit.Rule =
                _("float32") + _("(") + float64 + _(")") |
                _("float64") + _("(") + float64 + _(")") |
                _("float32") + _("(") + int64 + _(")") |
                _("float64") + _("(") + int64 + _(")") |
                _("int64") + _("(") + int64 + _(")") |
                _("int32") + _("(") + int64 + _(")") |
                _("int16") + _("(") + int64 + _(")") |
                _("char") + _("(") + int64 + _(")") |
                _("int8") + _("(") + int64 + _(")") |
                _("uint8") + _("(") + int64 + _(")") | // DOCS: non-present in ECMA grammar
                _("bool") + _("(") + truefalse + _(")") |
                compQstring |
                bytearrayhead + bytes + _(")") |
                _("nullref");

            bytearrayhead.Rule =
                _("bytearray") + _("(");

            bytes.Rule =
                Empty |
                hexbytes;

            hexbytes.Rule =
                HEXBYTE |
                hexbytes + HEXBYTE;

            // TODO - instr_r_head
            instr_r_head.Rule = _("TODO: instr_r_head");

            instr_tok_head.Rule =
                INSTR_TOK;

            // TODO - methodSpec
            methodSpec.Rule = _("TODO: methodSpec");

            instr.Rule =
                INSTR_NONE |
                INSTR_VAR + int32 |
                INSTR_VAR + id |
                INSTR_I + int32 |
                INSTR_I8 + int64 |
                INSTR_R + float64 |
                INSTR_R + int64 |
                instr_r_head + bytes + _(")") |
                INSTR_BRTARGET + int32 |
                INSTR_BRTARGET + id |
                INSTR_METHOD + callConv + type + typeSpec + _("::") + methodName + _("(") + sigArgs0 + _(")") |
                INSTR_METHOD + callConv + type + methodName + _("(") + sigArgs0 + _(")") |
                INSTR_FIELD + type + typeSpec + _("::") + id |
                INSTR_FIELD + type + id |
                INSTR_TYPE + typeSpec |
                INSTR_STRING + compQstring |
                INSTR_STRING + bytearrayhead + bytes + _(")") |
                INSTR_SIG + callConv + type + _("(") + sigArgs0 + _(")") |
                INSTR_RVA + id |
                INSTR_RVA + int32 |
                instr_tok_head + ownerType |
                INSTR_SWITCH + _("(") + labels + _(")") |
                INSTR_PHI + int16s;

            sigArgs0.Rule =
                Empty |
                sigArgs1;

            sigArgs1.Rule =
                sigArg |
                sigArgs1 + _(",") + sigArg;

            sigArg.Rule =
                _("...") |
                paramAttr + type |
                paramAttr + type + id |
                paramAttr + type + _("marshal") + _("(") + nativeType + _(")") |
                paramAttr + type + _("marshal") + _("(") + nativeType + _(")") + id;

            name1.Rule =
                id |
                DOTTEDNAME |
                name1 + _(".") + name1;

            className.Rule =
                _("[") + name1 + _("]") + slashedName |
                _("[") + _(".module") + name1 + _("]") + slashedName |
                slashedName;

            slashedName.Rule =
                name1 |
                slashedName + _("/") + name1;

            typeSpec.Rule =
                className |
                _("[") + name1 + _("]") |
                _("[") + _(".module") + name1 + _("]") |
                type;

            callConv.Rule =
                _("instance") + callConv |
                _("explicit") + callConv |
                callKind;

            callKind.Rule =
                Empty |
                _("default") |
                _("vararg") |
                _("unmanaged") + _("cdecl") |
                _("unmanaged") + _("stdcall") |
                _("unmanaged") + _("thiscall") |
                _("unmanaged") + _("fastcall");

            // TODO - nativeType
            nativeType.Rule = _("TODO: nativeType");

            type.Rule =
                _("class") + className |
                _("object") |
                _("string") |
                _("value") + _("class") + className |
                _("valuetype") + className |
                type + _("[") + _("]") |
                type + ("[") + bounds1 + _("]") |
                type + _("value") + _("[") + int32 + _("]") |
                type + _("&") |
                type + _("*") |
                type + _("pinned") |
                type + _("modreq") + _("(") + className + _(")") |
                type + _("modopt") + _("(") + className + _(")") |
                _("!") + int32 |
                methodSpec + callConv + type + _("*") + _("(") + sigArgs0 + _(")") |
                _("typedref") |
                _("char") |
                _("void") |
                _("bool") |
                _("int8") |
                _("int16") |
                _("int32") |
                _("int64") |
                _("float32") |
                _("float64") |
                _("unsigned") + _("int8") |
                _("unsigned") + _("int16") |
                _("unsigned") + _("int32") |
                _("unsigned") + _("int64") |
                _("native") + _("int") |
                _("native") + _("unsigned") + _("int") |
                _("native") + _("float") |
                _("uint8") |  // DOCS: not present in ECMA grammar
                _("uint16") | // DOCS: not present in ECMA grammar
                _("uint32") | // DOCS: not present in ECMA grammar
                _("uint64");  // DOCS: not present in ECMA grammar

            bounds1.Rule =
                bound |
                bounds1 + _(",") + bound;

            bound.Rule =
                Empty |
                _("...") |
                int32 |
                int32 + _("...") + int32 |
                int32 + _("...");

            labels.Rule =
                Empty |
                id + _(",") + labels |
                int32 + _(",") + labels |
                id |
                int32;

            id.Rule =
                ID |
                SQSTRING;

            // TODO - int16s
            int16s.Rule = _("TODO: int16s");

            int32.Rule =
                INT32;

            int64.Rule =
                INT64;

            float64.Rule =
                FLOAT64 |
                _("float32") + _("(") + int32 + _(")") |
                _("float64") + _("(") + int64 + _(")");

            secDecl.Rule =
                _(".permission") + secAction + typeSpec + _("(") + nameValPairs + _(")") |
                _(".permission") + secAction + typeSpec |
                psetHead + bytes + _(")") |
                _(".permissionset") + secAction + _("=") + _("{") + nameValPairs + _("}"); // DOCS: non-present in ECMA script

            psetHead.Rule =
                _(".permissionset") + secAction + _("=") + _("(");

            nameValPairs.Rule =
                nameValPair |
                nameValPair + _(",") + nameValPairs;

            nameValPair.Rule =
                compQstring + _("=") + caValue |
                className + _("=") + caValue; // DOCS: non-present in ECMA script

            truefalse.Rule =
                _("true") |
                _("false");

            caValue.Rule =
                truefalse |
                int32 |
                _("int32") + ("(") + int32 + _(")") |
                compQstring |
                className + _("(") + _("int8") + _(":") + int32 + _(")") |
                className + _("(") + _("int16") + _(":") + int32 + _(")") |
                className + _("(") + _("int32") + _(":") + int32 + _(")") |
                className + _("(") + int32 + _(")") |
                _("{") + _("property") + _("bool") + SQSTRING + _("=") + _("bool") + _("(") + _("true") + _(")") + _("}"); // DOCS: non-present in ECMA script

            secAction.Rule =
                _("request") |
                _("demand") |
                _("assert") |
                _("deny") |
                _("permitonly") |
                _("linkcheck") |
                _("inheritcheck") |
                _("reqmin") |
                _("reqopt") |
                _("reqrefuse") |
                _("prejitgrant") |
                _("prejitdeny") |
                _("noncasdemand") |
                _("noncaslinkdemand") |
                _("noncasinheritance");

            // TODO - extSourceSpec
            extSourceSpec.Rule = _("TODO: extSourceSpec");

            // TODO - fileDecl
            fileDecl.Rule = _("TODO: fileDecl");

            // TODO - hashHead
            hashHead.Rule = _("TODO: hashHead");

            assemblyHead.Rule =
                _(".assembly") + asmAttr + name1;

            asmAttr.Rule =
                Empty |
                asmAttr + _("noappdomain") |
                asmAttr + _("noprocess") |
                asmAttr + _("nomachine");

            assemblyDecls.Rule =
                Empty |
                assemblyDecls + assemblyDecl;

            assemblyDecl.Rule =
                _(".hash") + _("algorithm") + int32 |
                secDecl |
                asmOrRefDecl;

            asmOrRefDecl.Rule =
                publicKeyHead + bytes + _(")") |
                _(".ver") + int32 + _(":") + int32 + _(":") + int32 + _(":") + int32 |
                _(".locale") + compQstring |
                localeHead + bytes + _(")") |
                customAttrDecl;

            // TODO - publicKeyHead
            publicKeyHead.Rule = _("TODO: publicKeyHead");

            publicKeyTokenHead.Rule =
                _(".publickeytoken") + _("=") + _("(");

            // TODO - localeHead
            localeHead.Rule = _("TODO: localeHead");

            assemblyRefHead.Rule =
                _(".assembly") + _("extern") + name1 |
                _(".assembly") + _("extern") + name1 + _("as") + name1;

            assemblyRefDecls.Rule =
                Empty |
                assemblyRefDecls + assemblyRefDecl;

            assemblyRefDecl.Rule =
                hashHead + bytes + _(")") |
                asmOrRefDecl |
                publicKeyTokenHead + bytes + _(")");

            // TODO - comtypeHead
            comtypeHead.Rule = _("TODO: comtypeHead");

            // TODO - exportHead
            exportHead.Rule = _("TODO: exportHead");

            // TODO - comtypeDecls
            comtypeDecls.Rule = _("TODO: comtypeDecls");

            manifestResHead.Rule =
                _(".mresource") + manresAttr + name1;

            manresAttr.Rule =
                Empty |
                manresAttr + _("public") |
                manresAttr + _("private");

            manifestResDecls.Rule =
                Empty |
                manifestResDecls + manifestResDecl;

            manifestResDecl.Rule =
                _(".file") + name1 + _("at") + int32 |
                _(".assembly") + _("extern") + name1 |
                customAttrDecl;
        }
Esempio n. 31
0
        public RubyGrammar()
        {
            #region Terminals
              //String Literals with single and double-quote start/end symbols
              StringLiteral STRING = new StringLiteral("STRING", TermOptions.SpecialIgnoreCase);
              STRING.AddStartEnd("\"", ScanFlags.None);
              STRING.AddStartEnd("'", ScanFlags.None);
              Terminal HereDoc = new Terminal("HereDoc"); //-- implement me!
              Terminal RegExLiteral = new Terminal("RegExLiteral"); //-- implement me!
              IdentifierTerminal IDENTIFIER = new IdentifierTerminal("identifier", "_!?", "_$@");
              //                                                name     extraChars      extraFirstChars
              IDENTIFIER.DisplayName = "variable";
              //we need to isolate reserved words to avoid ambiguities in grammar
              IDENTIFIER.AddKeywords("do", "end", "def", "class",
                                       "if", "case", "return", "yield", "while", "until");  //and some others...
              Terminal Number = new NumberLiteral("Number");
              Terminal Comment = new CommentTerminal("Comment", "#", "\n");
              NonGrammarTerminals.Add(Comment); //add comment explicitly to this list as it is not reachable from Root

              //some conveniency variables
              Terminal Pipe = Symbol("|");
              Terminal dot = Symbol(".");
              Terminal comma = Symbol(",");
              #endregion

              #region NonTerminals
              //NT variables names match element names in original grammar
              NonTerminal PROGRAM = new NonTerminal("PROGRAM");
              NonTerminal COMPSTMT = new NonTerminal("COMPSTMT");
              NonTerminal STMT = new NonTerminal("STMT");
              NonTerminal BLOCK = new NonTerminal("BLOCK");
              NonTerminal EXPR = new NonTerminal("EXPR");
              //NonTerminal CALL = new NonTerminal("CALL");
              NonTerminal COMMAND = new NonTerminal("COMMAND");
              NonTerminal FUNCTION = new NonTerminal("FUNCTION");
              NonTerminal ARG = EXPR;// new NonTerminal("ARG");
              NonTerminal PRIMARY = new NonTerminal("PRIMARY", "operand");
              NonTerminal WHEN_ARGS = new NonTerminal("WHEN_ARGS");
              NonTerminal THEN = new NonTerminal("THEN");
              NonTerminal BLOCK_BEGIN = new NonTerminal("BLOCK_BEGIN");
              NonTerminal BLOCK_END = new NonTerminal("BLOCK_END");
              NonTerminal BLOCK_VAR = new NonTerminal("BLOCK_VAR");
            //      NonTerminal MLHS_ITEM = new NonTerminal("MLHS_ITEM");
              NonTerminal LHS = new NonTerminal("LHS");
              NonTerminal MRHS = new NonTerminal("MRHS");
            //      NonTerminal MLHS = MRHS; // new NonTerminal("MLHS");
              NonTerminal CALL_ARGS = new NonTerminal("CALL_ARGS");
              NonTerminal CALL_ARGS_P = new NonTerminal("CALL_ARGS_P");
              NonTerminal AMP_ARG = new NonTerminal("AMP_ARG");
              NonTerminal STAR_ARG = new NonTerminal("STAR_ARG");
              NonTerminal ARGS = new NonTerminal("ARGS");
              NonTerminal ARGDECL = new NonTerminal("ARGDECL");
              NonTerminal ARGLIST = new NonTerminal("ARGLIST");
            //      NonTerminal SINGLETON = new NonTerminal("SINGLETON");
              NonTerminal ASSOCS = new NonTerminal("ASSOCS");
              NonTerminal ASSOC = new NonTerminal("ASSOC");
            //      NonTerminal VARIABLE = new NonTerminal("VARIABLE");  --merged into IDENTIFIER
              NonTerminal LITERAL = new NonTerminal("LITERAL", "value");
              NonTerminal TERM = new NonTerminal("TERM");
              NonTerminal DO = new NonTerminal("DO");
            //      NonTerminal VARNAME = new NonTerminal("VARNAME");   // note 1
              NonTerminal GLOBAL = new NonTerminal("GLOBAL");
              NonTerminal RETURN_STMT = new NonTerminal("RETURN_STMT");
              NonTerminal YIELD_STMT = new NonTerminal("YIELD_STMT");
              NonTerminal DEFINEDQ_STMT = new NonTerminal("DEFINEDQ_STMT");
              NonTerminal FUNCTION_STMT = new NonTerminal("FUNCTION_STMT");
              NonTerminal IF_STMT = new NonTerminal("IF_STMT");
              NonTerminal UNLESS_STMT = new NonTerminal("UNLESS_STMT");
              NonTerminal WHILE_STMT = new NonTerminal("WHILE_STMT");
              NonTerminal UNTIL_STMT = new NonTerminal("UNTIL_STMT");
              NonTerminal CASE_STMT = new NonTerminal("CASE_STMT");
              NonTerminal FOR_STMT = new NonTerminal("FOR_STMT");
              NonTerminal BLOCK_STMT = new NonTerminal("BLOCK_STMT");
              NonTerminal CLASS_DEF = new NonTerminal("CLASS_DEF");
              NonTerminal BASE_REF = new NonTerminal("BASE_REF");
              NonTerminal MODULE = new NonTerminal("MODULE_STMT");
              NonTerminal DEFFUNC_STMT = new NonTerminal("DEFFUNC_STMT");
              NonTerminal DEFSING_STMT = new NonTerminal("DEFSING_STMT");
              NonTerminal SINGLETON = new NonTerminal("SINGLETON");
              NonTerminal END = new NonTerminal("end");

              NonTerminal SYMBOL = new NonTerminal("SYMBOL");
              //Not in original grammar
              NonTerminal FNAME = new NonTerminal("FNAME");
              BLOCK_BEGIN.Rule = Symbol("do") | "{";
              BLOCK_END.Rule = Symbol("end") | "}";
              NonTerminal OPERATION = new NonTerminal("OPERATION");
              //      Terminal VARNAME = IDENTIFIER;
              NonTerminal AUG_ASGN = new NonTerminal("AUG_ASGN");
              NonTerminal BINOP = new NonTerminal("BINOP", "operator");
              NonTerminal UNOP = new NonTerminal("UNOP");
              NonTerminal DELIM = new NonTerminal("DELIM");

              #endregion

              #region Rules
              //Set grammar root
              this.Root = PROGRAM;

              //PROGRAM         : COMPSTMT
              PROGRAM.Rule = COMPSTMT; // +Grammar.Eof;
              //COMPSTMT        : STMT (TERM EXPR)* [TERM]
              COMPSTMT.Rule = NewLine.Q() + STMT.Plus(TERM) + TERM.Q();

              /* STMT   : CALL do [`|' [BLOCK_VAR] `|'] COMPSTMT end
                | undef FNAME
                | alias FNAME FNAME
                | STMT if EXPR
                | STMT while EXPR
                | STMT unless EXPR
                | STMT until EXPR
                | `BEGIN' `{' COMPSTMT `}'
                | `"end"' `{' COMPSTMT `}'
                | LHS `=' COMMAND [do [`|' [BLOCK_VAR] `|'] COMPSTMT end]
                | EXPR    */
              STMT.Rule =     FUNCTION
                      | COMMAND + BLOCK.Q()
                      | "undef" + FNAME | "alias" + FNAME + FNAME
                      | STMT + (Symbol("if")|"while"|"unless"|"until") + EXPR
                      | Symbol("BEGIN") + "{" + COMPSTMT + "}"
                     // | Symbol("end") + BLOCK_BEGIN + COMPSTMT + BLOCK_END    // don't quite get it
                   //   | LHS + "=" + COMMAND + BLOCK.Q()
                      | LHS + "=" + EXPR  //changed this
                      | LHS + AUG_ASGN + EXPR
                      | EXPR;
              BLOCK.Rule = "do" + WithQ(Pipe + BLOCK_VAR.Q() + Pipe) + COMPSTMT + "end";

              /* EXPR   : MLHS `=' MRHS
                | return CALL_ARGS
                | yield CALL_ARGS
                | EXPR and EXPR
                | EXPR or EXPR
                | not EXPR
                | COMMAND
                | `!' COMMAND
                | ARG   */
              //this one is completely changed, for better or worse...
              EXPR.Rule = //  MRHS + "=" + EXPR | //changed to EXPR
                     //  LHS + "=" + EXPR  //changed this
                    // | LHS + AUG_ASGN + EXPR
                      EXPR + BINOP + EXPR
                     | UNOP + EXPR
                     //| "(" + EXPR + ")"
                      | EXPR + "?" + EXPR + ":" + EXPR   //added this to cover "?" operator
                     | "defined?" + ARG
                     | PRIMARY
                      ;
              ARG = EXPR;
              // CALL   : FUNCTION | COMMAND
             // CALL.Expression = FUNCTION | COMMAND; //expression embedded directly into STMT

              /* COMMAND         : OPERATION CALL_ARGS
                | PRIMARY `.' OPERATION CALL_ARGS
                | PRIMARY `::' OPERATION CALL_ARGS
                | super CALL_ARGS   */
              COMMAND.Rule =  OPERATION + CALL_ARGS
                         | PRIMARY + DELIM + OPERATION + CALL_ARGS
                         | "super" + CALL_ARGS;
              OPERATION.Rule =  IDENTIFIER;
              DELIM.Rule = dot | "::";

              /* FUNCTION   : OPERATION [`(' [CALL_ARGS] `)']
                | PRIMARY `.' OPERATION `(' [CALL_ARGS] `)'
                | PRIMARY `::' OPERATION `(' [CALL_ARGS] `)'
                | PRIMARY `.' OPERATION
                | PRIMARY `::' OPERATION
                | super `(' [CALL_ARGS] `)'
                | super  */
              FUNCTION.Rule = OPERATION + CALL_ARGS_P
                   | PRIMARY + DELIM + OPERATION + CALL_ARGS_P.Q()
                   | "super" + CALL_ARGS_P;
              CALL_ARGS_P.Rule = "(" + CALL_ARGS.Q() + ")";
              /*  ARG   : LHS `=' ARG
                | LHS OP_ASGN ARG
                | ARG `..' ARG
                | ARG `...' ARG
                | ARG `+' ARG
                | ARG `-' ARG
                | ARG `*' ARG
                | ARG `/' ARG
                | ARG `%' ARG
                | ARG `**' ARG
                | `+' ARG
                | `-' ARG
                | ARG `|' ARG
                | ARG `^' ARG
                | ARG `&' ARG
                | ARG `<=>' ARG
                | ARG `>' ARG
                | ARG `>=' ARG
                | ARG `<' ARG
                | ARG `<=' ARG
                | ARG `==' ARG
                | ARG `===' ARG
                | ARG `!=' ARG
                | ARG `=~' ARG
                | ARG `!~' ARG
                | `!' ARG
                | `~' ARG
                | ARG `<<' ARG
                | ARG `>>' ARG
                | ARG `&&' ARG
                | ARG `||' ARG
                | defined? ARG
                | PRIMARY   */

            /*  ARG.Expression = LHS + "=" + EXPR  //changed this
                     | LHS + AUG_ASGN + EXPR
                     | ARG + BINOP + ARG  //moved to EXPR
                     | UNOP + ARG
                     | "defined?" + ARG
                     | PRIMARY
                     ; */
              AUG_ASGN.Rule = Symbol("+=") | "-=" | "*=" | "/=" | "%=" | "**=" | "&=" | "|=" | "^=" | "<<=" | ">>=" | "&&=" | "||=";

              BINOP.Rule = Symbol("..") | "..." | "+" | "-" | "*" | "/" | "%" | "**" | "|" | "^" | "&"
                         | "<=>" | ">" | ">=" | "<" | "<=" | "==" | "===" | "!=" | "=~" | "!~" | "<<" | ">>" | "&&" | "||"
                         | "and" | "or";  //added these two here
              UNOP.Rule = Symbol("+") | "-" | "!" | "~";
               /*PRIMARY:    */
              /*        `(' COMPSTMT `)'
                | LITERAL
                | VARIABLE
                | PRIMARY `::' IDENTIFIER
                | `::' IDENTIFIER
                | PRIMARY `[' [ARGS] `]'
                | `[' [ARGS [`,']] `]'
                | `{' [(ARGS|ASSOCS) [`,']] `}'
                | return [`(' [CALL_ARGS] `)']
                | yield [`(' [CALL_ARGS] `)']
                | defined? `(' ARG `)'
                | FUNCTION
                | FUNCTION `{' [`|' [BLOCK_VAR] `|'] COMPSTMT `}'
                | if EXPR THEN
                  COMPSTMT
                  (elsif EXPR THEN COMPSTMT)*
                  [else COMPSTMT]
                  end
                | unless EXPR THEN
                  COMPSTMT
                  [else COMPSTMT]
                  end
                | while EXPR DO COMPSTMT end
                | until EXPR DO COMPSTMT end
                | case COMPSTMT
                  (when WHEN_ARGS THEN COMPSTMT)+
                  [else COMPSTMT]
                  end
                | for BLOCK_VAR in EXPR DO
                  COMPSTMT
                  end
                | begin
                  COMPSTMT
                  [rescue [ARGS] DO COMPSTMT]+
                  [else COMPSTMT]
                  [ensure COMPSTMT]
                  end
                | class IDENTIFIER [`<' IDENTIFIER]
                  COMPSTMT
                  end"=
                | module IDENTIFIER
                  COMPSTMT
                  end
                | def FNAME ARGDECL
                  COMPSTMT
                  end
                | def SINGLETON (`.'|`::') FNAME ARGDECL
                  COMPSTMT
                  end */
              PRIMARY.Rule =
               // "(" + COMPSTMT + ")" |   //-- removed this to fix ambiguity
            LITERAL
            | LHS  //note 1.
            | "[" + WithQ(ARGS + comma.Q()) + "]"
            | "{" + WithQ( (ARGS|ASSOC) + comma.Q() ) + "}"
            | RETURN_STMT | YIELD_STMT | DEFINEDQ_STMT | FUNCTION_STMT | IF_STMT | UNLESS_STMT | WHILE_STMT
            | UNTIL_STMT | CASE_STMT | FOR_STMT | BLOCK_STMT | CLASS_DEF | MODULE | DEFFUNC_STMT | DEFSING_STMT;
             // LHS.Expression = VARIABLE | PRIMARY + "[" + ARGS.Q() + "]" | PRIMARY + "." + IDENTIFIER;

              RETURN_STMT.Rule = "return" + EXPR;// CALL_ARGS_P.Q(); //changed this
              YIELD_STMT.Rule = "yield" + CALL_ARGS_P.Q();
              DEFINEDQ_STMT.Rule = Symbol("defined?") + "(" + ARG + ")";
              FUNCTION_STMT.Rule = FUNCTION + WithQ("{" + WithQ("|" + BLOCK_VAR.Q() + "|") + COMPSTMT + "}");
              IF_STMT.Rule = "if" + EXPR + THEN + COMPSTMT + WithStar("elsif" + EXPR + THEN + COMPSTMT) + WithQ("else" + COMPSTMT) + END;
              UNLESS_STMT.Rule = "unless" + EXPR + THEN + COMPSTMT + "else" + COMPSTMT + END;
              WHILE_STMT.Rule = "while" + EXPR + DO + COMPSTMT + END;
              UNTIL_STMT.Rule = "until" + EXPR + DO + COMPSTMT + END;
              CASE_STMT.Rule = "case" + COMPSTMT + WithPlus("when" + WHEN_ARGS + THEN + COMPSTMT)
                                 + WithQ("else" + COMPSTMT) + END;
              FOR_STMT.Rule = "for" + BLOCK_VAR + "in" + EXPR + DO + COMPSTMT + END;
              BLOCK_STMT.Rule = "begin" + COMPSTMT + WithPlus("rescue" + ARGS.Q() + DO + COMPSTMT)
                                 + WithQ("else" + COMPSTMT) + WithQ("ensure" + COMPSTMT) + END;
              CLASS_DEF.Rule = "class" + IDENTIFIER + BASE_REF.Q() + COMPSTMT + END;
              BASE_REF.Rule = "<" + IDENTIFIER;
              MODULE.Rule = "module" + IDENTIFIER + COMPSTMT + END;
              DEFFUNC_STMT.Rule = "def" + FNAME + ARGDECL.Q() + COMPSTMT + END;
              DEFSING_STMT.Rule = "def" + SINGLETON + (dot|"::") + FNAME + ARGDECL.Q() + COMPSTMT + END;
              END.Rule = "end"; // TERM.Q() + "end";
              //  SINGLETON : VARIABLE | `(' EXPR `)'
              SINGLETON.Rule = IDENTIFIER | "(" + EXPR + ")";
              // WHEN_ARGS       : ARGS [`,' `*' ARG]  | `*' ARG
              WHEN_ARGS.Rule = ARGS + WithQ(comma + "*" + ARG) | "*" + ARG;
              // THEN   : TERM | then | TERM then
              THEN.Rule = TERM | "then" | TERM + "then";
              // DO     : TERM | do | TERM do
              DO.Rule = TERM | "do" | TERM + "do";
              //  BLOCK_VAR       : LHS | MLHS
            //      BLOCK_VAR.Expression = LHS | MLHS;   // -- ambiguous, changing to the following:
              BLOCK_VAR.Rule = IDENTIFIER | "(" + IDENTIFIER.Plus(comma) + ")";
              //  MLHS  : MLHS_ITEM `,' [MLHS_ITEM (`,' MLHS_ITEM)*] [`*' [LHS]]  | `*' LHS
            //      MLHS.Expression = MLHS_ITEM.Plus(",") + WithQ("*" + LHS.Q()) | "*" + LHS;  --ambiguous
              //MLHS.Expression = PRIMARY.Plus(",") + WithQ("*" + LHS.Q()) | "*" + LHS;
              //  MLHS_ITEM  : LHS | '(' MLHS ')'
              //MLHS_ITEM.Expression = LHS | "(" + MLHS + ")";  //--ambiguous!!! using PRIMARY
              //MLHS_ITEM = PRIMARY;

              /* LHS    : VARIABLE
                | PRIMARY `[' [ARGS] `]'
                | PRIMARY `.' IDENTIFIER  */
             // LHS.Expression = IDENTIFIER | PRIMARY + "[" + ARGS.Q() + "]" | PRIMARY + dot + IDENTIFIER;
              LHS.Rule = OPERATION
                     | PRIMARY + "[" + ARGS.Q() + "]"
                     | "(" + EXPR + ")";
              //   MRHS : ARGS [`,' `*' ARG] | `*' ARG
              MRHS.Rule = ARGS + WithQ(comma + "*" + ARG) | "*" + ARG;
              /* CALL_ARGS   : ARGS
                | ARGS [`,' ASSOCS] [`,' `*' ARG] [`,' `&' ARG]
                | ASSOCS [`,' `*' ARG] [`,' `&' ARG]
                | `*' ARG [`,' `&' ARG]
                | `&' ARG
                | COMMAND    */
              CALL_ARGS.Rule = // ARGS |  //removed this - it is covered by next expression
                             ARGS + WithQ(comma + ASSOCS) + STAR_ARG.Q() + AMP_ARG.Q()
                           | ASSOCS + STAR_ARG.Q() + AMP_ARG.Q()
                           | "*" + ARG + AMP_ARG.Q()
                           | "&" + ARG
                           | COMMAND;
              AMP_ARG.Rule = comma + "&" + ARG;
              STAR_ARG.Rule = comma + "*" + ARG;
              //  ARGS            : ARG (`,' ARG)*
              ARGS.Rule = ARG.Plus(comma);
              // ARGDECL         : `(' ARGLIST `)'  | ARGLIST TERM
              ARGDECL.Rule = "(" + ARGLIST + ")" | ARGLIST + TERM;
              /*   ARGLIST         : IDENTIFIER(`,'IDENTIFIER)*[`,'`*'[IDENTIFIER]][`,'`&'IDENTIFIER]
                | `*'IDENTIFIER[`,'`&'IDENTIFIER]
                | [`&'IDENTIFIER]    */
              ARGLIST.Rule = IDENTIFIER.Plus(comma) + WithQ(comma + "*" + IDENTIFIER.Q()) + WithQ(comma + "&" + IDENTIFIER)
                           | "*" + IDENTIFIER + WithQ(comma + "&" + IDENTIFIER)
                           | "&" + IDENTIFIER;
              // ASSOCS : ASSOC (`,' ASSOC)*
              ASSOCS.Rule = ASSOC.Plus(comma);
              //ASSOC : ARG `=>' ARG
              ASSOC.Rule = ARG + "=>" + ARG;
              //  VARIABLE : VARNAME | nil | self    -- variable is merged into IDENTIFIER
              //VARIABLE.Expression = IDENTIFIER | "nil" | "self";
              // LITERAL : numeric | SYMBOL | STRING | STRING2 | HERE_DOC | REGEXP
              LITERAL.Rule = Number | SYMBOL | STRING | HereDoc | RegExLiteral;
              SYMBOL.Rule = Symbol(":") + IDENTIFIER; // (FNAME | VARNAME); //note 1.
              /*  FNAME           : IDENTIFIER | `..' | `|' | `^' | `&'
                | `<=>' | `==' | `===' | `=~'
                | `>' | `>=' | `<' | `<='
                | `+' | `-' | `*' | `/' | `%' | `**'
                | `<<' | `>>' | `~'
                | `+@' | `-@' | `[]' | `[]='  */
              FNAME.Rule = IDENTIFIER | ".." | "|" | "^" | "&" | "<=>" | "==" | "===" | "=~"
                | ">" | ">=" | "<" | "<="  | "+" | "-" | "*" | "/" | "%" | "**"
                | "<<" | ">>" | "~" | "+@" | "-@" | "[]" | "[]=";
              // TERM : `;' | `\n'
              TERM.Rule = NewLine | ";";  //NewLine is produced by token filter
              #endregion

              //error handling
              EXPR.ErrorRule = SyntaxError;
              DEFFUNC_STMT.ErrorRule = "def" + SyntaxError + COMPSTMT + END;

              #region misc: Operators, TokenFilters, etc
              //Register operators - not sure if precedence is assigned correctly
              RegisterOperators(100, Associativity.Right, "**");
              RegisterOperators( 90, "<<", ">>");
              RegisterOperators( 80, "*", "/", "%");
              RegisterOperators( 70, "+", "-");
              RegisterOperators( 60, "&", "&&", "and");
              RegisterOperators( 50, "|", "||", "or", "^");
              RegisterOperators( 40, ">", ">=", "<", "<=", "?");
              RegisterOperators( 30, "<=>" , "==" , "===" , "!=" , "=~" , "!~");
              RegisterOperators( 20, "..", "...");

              RegisterPunctuation("(", ")", "," );

              CodeOutlineFilter filter = new CodeOutlineFilter(false);
              TokenFilters.Add(filter);
              #endregion
        }
Esempio n. 32
0
        public PythonGrammar()
        {
            #region Declare Terminals
              ConstantTerminal Constants = new ConstantTerminal("Constants");
              Constants.Add("True", true);
              Constants.Add("False", false);
              IdentifierTerminal Identifier = new IdentifierTerminal("Identifier");
              Terminal Comment = new CommentTerminal("Comment", "#", "\n");
              NonGrammarTerminals.Add(Comment);

              Terminal comma = Symbol(",", "comma");
              //commaQ is optional trailing comma in lists; it causes several conflicts in this grammar
              // so we get rid of it (by assigning it Empty value)
              //NonTerminal commaQ = comma.Q();  //this causes several conflicts
              NonTerminal commaQ = Empty;
              Terminal dot = Symbol(".", "dot");
              Terminal LBr   = Symbol("[");
              Terminal RBr   = Symbol("]");
              Terminal bQuote   = Symbol("`");
              Terminal ellipsis   = Symbol("...");
              Terminal colon = Symbol(":");
              Terminal NAME = Identifier;
              Terminal NEWLINE = Grammar.NewLine;
              Terminal INDENT = Grammar.Indent;
              Terminal DEDENT = Grammar.Dedent;
              Terminal semicolon = Symbol(";");
              Terminal EOF = Grammar.Eof;
              Terminal NUMBER = TerminalFactory.CreatePythonNumber("NUMBER");
              #endregion

              #region Declare NonTerminals
              StringLiteral STRING = TerminalFactory.CreatePythonString("String");
              NonTerminal single_input = new NonTerminal("single_input");
              NonTerminal file_input = new NonTerminal("file_input");
              NonTerminal eval_input = new NonTerminal("eval_input");
              NonTerminal decorator = new NonTerminal("decorator");
              NonTerminal funcdef = new NonTerminal("funcdef");
              NonTerminal parameters = new NonTerminal("parameters");
              NonTerminal varargslist = new NonTerminal("varargslist");
              NonTerminal vararg = new NonTerminal("vararg");
              NonTerminal fpdef = new NonTerminal("fpdef");
              NonTerminal fpdef_ext = new NonTerminal("fpdef_ext");
              NonTerminal fplist = new NonTerminal("fplist");
              NonTerminal stmt = new NonTerminal("stmt");
              NonTerminal simple_stmt = new NonTerminal("simple_stmt");
              NonTerminal small_stmt = new NonTerminal("small_stmt");
              NonTerminal expr_stmt = new NonTerminal("expr_stmt");
              NonTerminal yield_or_testlist = new NonTerminal("yield_or_testlist");
              NonTerminal augassign = new NonTerminal("augassign");
              NonTerminal print_stmt = new NonTerminal("print_stmt");
              NonTerminal del_stmt = new NonTerminal("del_stmt");
              NonTerminal pass_stmt = new NonTerminal("pass_stmt");
              NonTerminal flow_stmt = new NonTerminal("flow_stmt");
              NonTerminal break_stmt = new NonTerminal("break_stmt");
              NonTerminal continue_stmt = new NonTerminal("continue_stmt");
              NonTerminal return_stmt = new NonTerminal("return_stmt");
              NonTerminal yield_stmt = new NonTerminal("yield_stmt");
              NonTerminal raise_stmt = new NonTerminal("raise_stmt");
              NonTerminal import_stmt = new NonTerminal("import_stmt");
              NonTerminal import_name = new NonTerminal("import_name");
              NonTerminal import_from = new NonTerminal("import_from");
              NonTerminal import_as_name = new NonTerminal("import_as_name");
              NonTerminal dotted_as_name = new NonTerminal("dotted_as_name");
              NonTerminal import_as_names = new NonTerminal("import_as_names");
              NonTerminal dotted_as_names = new NonTerminal("dotted_as_names");
              NonTerminal dotted_name = new NonTerminal("dotted_name");
              NonTerminal global_stmt = new NonTerminal("global_stmt");
              NonTerminal exec_stmt = new NonTerminal("exec_stmt");
              NonTerminal assert_stmt = new NonTerminal("assert_stmt");
              NonTerminal compound_stmt = new NonTerminal("compound_stmt");
              NonTerminal if_stmt = new NonTerminal("if_stmt");
              NonTerminal else_clause = new NonTerminal("else_clause");
              NonTerminal while_stmt = new NonTerminal("while_stmt");
              NonTerminal for_stmt = new NonTerminal("for_stmt");
              NonTerminal try_stmt = new NonTerminal("try_stmt");
              NonTerminal finally_block = new NonTerminal("finally_block");
              NonTerminal with_stmt = new NonTerminal("with_stmt");
              NonTerminal with_var = new NonTerminal("with_var");
              NonTerminal except_clause = new NonTerminal("except_clause");
              NonTerminal suite = new NonTerminal("suite");
              NonTerminal testlist_safe = new NonTerminal("testlist_safe");
              NonTerminal old_test = new NonTerminal("old_test");
              NonTerminal old_lambdef = new NonTerminal("old_lambdef");
              NonTerminal test = new NonTerminal("test");
              NonTerminal testlist = new NonTerminal("testlist");
              NonTerminal testlist1 = new NonTerminal("testlist1");
              NonTerminal or_test = new NonTerminal("or_test");
              NonTerminal and_test = new NonTerminal("and_test");
              NonTerminal not_test = new NonTerminal("not_test");
              NonTerminal comparison = new NonTerminal("comparison");
              NonTerminal comp_op = new NonTerminal("comp_op");
              NonTerminal expr = new NonTerminal("expr");
              NonTerminal xor_expr = new NonTerminal("xor_expr");
              NonTerminal and_expr = new NonTerminal("and_expr");
              NonTerminal shift_expr = new NonTerminal("shift_expr");
              NonTerminal arith_expr = new NonTerminal("arith_expr");
              NonTerminal shift_op = new NonTerminal("shift_op");
              NonTerminal sum_op = new NonTerminal("sum_op");
              NonTerminal mul_op = new NonTerminal("mul_op");

              NonTerminal term = new NonTerminal("term");
              NonTerminal factor = new NonTerminal("factor");
              NonTerminal power = new NonTerminal("power");
              NonTerminal atom = new NonTerminal("atom");
              NonTerminal listmaker = new NonTerminal("listmaker");
              NonTerminal testlist_gexp = new NonTerminal("testlist_gexp");
              NonTerminal lambdef = new NonTerminal("lambdef");
              NonTerminal trailer = new NonTerminal("trailer");
              NonTerminal subscriptlist = new NonTerminal("subscriptlist");
              NonTerminal subscript = new NonTerminal("subscript");
              NonTerminal sliceop = new NonTerminal("sliceop");
              NonTerminal exprlist = new NonTerminal("exprlist");
              NonTerminal dictmaker = new NonTerminal("dictmaker");
              NonTerminal dict_elem = new NonTerminal("dict_elem");
              NonTerminal classdef = new NonTerminal("classdef");
              NonTerminal arglist = new NonTerminal("arglist");
              NonTerminal argument = new NonTerminal("argument");
              NonTerminal list_iter = new NonTerminal("list_iter");
              NonTerminal list_for = new NonTerminal("list_for");
              NonTerminal list_if = new NonTerminal("list_if");
              NonTerminal gen_iter = new NonTerminal("gen_iter");
              NonTerminal gen_for = new NonTerminal("gen_for");
              NonTerminal gen_if = new NonTerminal("gen_if");
              NonTerminal encoding_decl = new NonTerminal("encoding_decl");
              NonTerminal yield_expr = new NonTerminal("yield_expr");
              #endregion

              #region RULES
              // The commented rules before each statement are original grammar rules
              //  copied from the grammar file.
              //Set grammar root
              base.Root = file_input;
              //file_input: (NEWLINE | stmt)* ENDMARKER
              file_input.Rule = NEWLINE.Q() + stmt.Star();// +EOF; //EOF is added by default as a lookahead

              //single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
              single_input.Rule = NEWLINE | simple_stmt | compound_stmt + NEWLINE;
              //eval_input: testlist NEWLINE* ENDMARKER
              eval_input.Rule = NEWLINE.Q() + WithStar(testlist + NEWLINE); // +EOF;  //changed this

              //decorators: decorator+
              //decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
              decorator.Rule = "@" + dotted_name + WithQ("(" + arglist.Q() + ")") + NEWLINE;
              //funcdef: [decorators] 'def' NAME parameters ':' suite
              funcdef.Rule = decorator.Star() + "def" + NAME + parameters + ":" + suite;
              // parameters: '(' [varargslist] ')'
              parameters.Rule = "(" + varargslist.Q() + ")";

              /* varargslist: ((fpdef ['=' test] ',')*
              ('*' NAME [',' '**' NAME] | '**' NAME) |
                fpdef ['=' test] (',' fpdef ['=' test])* [','])  */
              fpdef_ext.Rule = fpdef + WithQ("=" + test);
            /*      varargslist.Expression = WithStar(fpdef_ext + comma) +
                    WithQ("*" + NAME + WithQ (comma + "**" + NAME) | "**" + NAME) |
                    fpdef_ext.Plus(comma) + commaQ; */  // ambiguous
              varargslist.Rule = vararg.Plus(comma) + commaQ;
              vararg.Rule = fpdef_ext | "*" + NAME | "**" + NAME; // added this to grammar
              // fpdef: NAME | '(' fplist ')'
              fpdef.Rule = NAME | "(" + fplist + ")";
              //fplist: fpdef (',' fpdef)* [',']
              fplist.Rule = fpdef.Plus(comma) + commaQ;

              //stmt: simple_stmt | compound_stmt
              stmt.Rule = simple_stmt | compound_stmt;
              //simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
              simple_stmt.Rule = small_stmt.Plus(semicolon) + semicolon.Q() + NEWLINE;
              /* small_stmt: (expr_stmt | print_stmt  | del_stmt | pass_stmt | flow_stmt |
             import_stmt | global_stmt | exec_stmt | assert_stmt)   */
              small_stmt.Rule = expr_stmt | print_stmt  | del_stmt | pass_stmt | flow_stmt |
                   import_stmt | global_stmt | exec_stmt | assert_stmt;
              /* expr_stmt: testlist (augassign (yield_expr|testlist) |
                     ('=' (yield_expr|testlist))*)    */
              //Note!: the following is a less strict expression, it allows augassign to appear multiple times
              //  in non-first position; the after-parse analysis should catch this
              expr_stmt.Rule = testlist + WithStar( (augassign|"=") + yield_or_testlist);
              yield_or_testlist.Rule = yield_expr | testlist;
              /* augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
            '<<=' | '>>=' | '**=' | '//=') */
              augassign.Rule = Symbol("+=") | "-=" | "*=" | "/=" | "%=" | "&=" | "|=" | "^=" |
                  "<<=" | ">>=" | "**=" | "//=";
              //# For normal assignments, additional restrictions enforced by the interpreter
              /*  print_stmt: 'print' ( [ test (',' test)* [','] ] |
                      '>>' test [ (',' test)+ [','] ] )     */
              print_stmt.Rule = "print" + (Empty | testlist | ">>" + testlist);  //modified slightly using testlist
              //del_stmt: 'del' exprlist
              del_stmt.Rule = "del" + exprlist;
              //pass_stmt: 'pass'
              pass_stmt.Rule = "pass";
              //flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
              flow_stmt.Rule = break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt;
              //break_stmt: 'break'
              break_stmt.Rule = "break";
              // continue_stmt: 'continue'
              continue_stmt.Rule = "continue";
              // return_stmt: 'return' [testlist]
              return_stmt.Rule = "return" + testlist.Q();
              // yield_stmt: yield_expr
              yield_stmt.Rule = yield_expr;
              // raise_stmt: 'raise' [test [',' test [',' test]]]
              raise_stmt.Rule = "raise" + WithQ( test + WithQ("," + test + WithQ("," + test)));
              // import_stmt: import_name | import_from
              import_stmt.Rule = import_name | import_from;
              // import_name: 'import' dotted_as_names
              import_name.Rule = "import" + dotted_as_names;
              // import_from: ('from' ('.'* dotted_name | '.'+)
              //        'import' ('*' | '(' import_as_names ')' | import_as_names))
              // import_from.Expression = Symbol("from") + (dot.Star() + dotted_name | dot.Plus()) +   //ambiguious
              import_from.Rule = Symbol("from") + dot.Star() + (dotted_name | dot) +
                    "import" + (Symbol("*") | "(" + import_as_names + ")" | import_as_names);
              // import_as_name: NAME ['as' NAME]
              import_as_name.Rule = NAME + WithQ("as" + NAME);
              // dotted_as_name: dotted_name ['as' NAME]
              dotted_as_name.Rule = dotted_name + WithQ("as" + NAME);
              // import_as_names: import_as_name (',' import_as_name)* [',']
              import_as_names.Rule = import_as_name.Plus(comma) + commaQ;
              // dotted_as_names: dotted_as_name (',' dotted_as_name)*
              dotted_as_names.Rule = dotted_as_name.Plus(comma);
              // dotted_name: NAME ('.' NAME)*
              dotted_name.Rule = NAME.Plus(dot);
              // global_stmt: 'global' NAME (',' NAME)*
              global_stmt.Rule = "global" + NAME.Plus(comma);
              // exec_stmt: 'exec' expr ['in' test [',' test]]
              exec_stmt.Rule = "exec" + expr + WithQ("in" + test.Plus(comma));
              // assert_stmt: 'assert' test [',' test]
              assert_stmt.Rule = "assert" + test.Plus(comma);

              // compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef
              compound_stmt.Rule = if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef;
              // if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
              if_stmt.Rule = "if" + test + ":" + suite +
                    WithStar("elif" + test + ":" + suite) + else_clause.Q();
              else_clause.Rule = "else" + colon + suite;
              // while_stmt: 'while' test ':' suite ['else' ':' suite]
              while_stmt.Rule = "while" + test + ":" + suite + else_clause.Q();
              // for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
              for_stmt.Rule = "for" + exprlist + "in" + testlist + ":" + suite + else_clause.Q();
            /* try_stmt: ('try' ':' suite
               ((except_clause ':' suite)+
            ['else' ':' suite]
            ['finally' ':' suite] |
               'finally' ':' suite))   */
              try_stmt.Rule = "try" + colon + suite +
            (  (except_clause + ":" + suite)+ else_clause.Q() + finally_block.Q() | finally_block   );
              finally_block.Rule = "finally" + colon + suite;
              // with_stmt: 'with' test [ with_var ] ':' suite
              with_stmt.Rule = "with" + test + with_var.Q() + ":" + suite;
              // with_var: 'as' expr
              with_var.Rule = "as" + expr;
              // NB compile.c makes sure that the default except clause is last
              // except_clause: 'except' [test [('as' | ',') test]]
              except_clause.Rule = "except" + WithQ(test + WithQ( (Symbol("as") | ",") + test));
              // suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
              suite.Rule = simple_stmt | NEWLINE  + INDENT + stmt.Plus() + DEDENT;

              //# Backward compatibility cruft to support:
              //# [ x for x in lambda: True, lambda: False if x() ]
              //# even while also allowing:
              //# lambda x: 5 if x else 2
              //# (But not a mix of the two)

              // testlist_safe: old_test [(',' old_test)+ [',']]
              testlist_safe.Rule = old_test.Plus(comma) + commaQ;
              // old_test: or_test | old_lambdef
              old_test.Rule = or_test | old_lambdef;
              // old_lambdef: 'lambda' [varargslist] ':' old_test
              old_lambdef.Rule = "lambda" + varargslist.Q() + ":" + old_test;

              // test: or_test ['if' or_test 'else' test] | lambdef
              test.Rule = or_test + WithQ("if" + or_test + "else" + test) | lambdef;
              // or_test: and_test ('or' and_test)*
              or_test.Rule = and_test + WithStar("or" + and_test);
              // and_test: not_test ('and' not_test)*
              and_test.Rule = not_test + WithStar("and" + not_test);
              // not_test: 'not' not_test | comparison
              not_test.Rule = "not" + not_test | comparison;
              // comparison: expr (comp_op expr)*
              comparison.Rule = expr + WithStar(comp_op + expr);
              // comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
              comp_op.Rule = Symbol("<")|">"|"=="|">="|"<="|"<>"|"!="|"in"|
                           Symbol("not") + "in"|"is"|Symbol("is") + "not";
              // expr: xor_expr ('|' xor_expr)*
              expr.Rule = xor_expr.Plus(Symbol("|"));
              // xor_expr: and_expr ('^' and_expr)*
              xor_expr.Rule = and_expr.Plus(Symbol("^"));
              // and_expr: shift_expr ('&' shift_expr)*
              and_expr.Rule = shift_expr.Plus(Symbol("&"));
              // shift_expr: arith_expr (('<<'|'>>') arith_expr)*
              shift_expr.Rule = arith_expr.Plus(shift_op); //
              shift_op.Rule = Symbol("<<")|">>";
              // arith_expr: term (('+'|'-') term)*
              arith_expr.Rule = term.Plus(sum_op);
              sum_op.Rule = Symbol("+") | "-";
              // term: factor (('*'|'/'|'%'|'//') factor)*
              term.Rule = factor.Plus(mul_op);
              mul_op.Rule = Symbol("*")|"/"|"%"|"//";
              // factor: ('+'|'-'|'~') factor | power
              factor.Rule = (Symbol("+")|"-"|"~") + factor | power;
              // power: atom trailer* ['**' factor]
              power.Rule = atom + trailer.Star() + WithQ("**" + factor);
              /* atom: ('(' [yield_expr|testlist_gexp] ')' |
              '[' [listmaker] ']' |
              '{' [dictmaker] '}' |
              '`' testlist1 '`' |
              NAME | NUMBER | STRING+)  */
              atom.Rule = "(" + WithQ(yield_expr|testlist_gexp) + ")" |
             "[" + listmaker.Q() + "]" |
             "{" + dictmaker.Q() + "}" |
             "`" + testlist1 + "`" |
             NAME | NUMBER | STRING; //.Plus();  //removed "+" - seems strange at least
              // listmaker: test ( list_for | (',' test)* [','] )
              //  listmaker.Expression = test + ( list_for | WithStar("," + test) + commaQ ); // ambigouous
            //      listmaker.Expression = test + list_for.Q() | testlist;                             // modified version
              listmaker.Rule = test + list_for.Q() + testlist.Q() + commaQ;                             // modified version
              // testlist_gexp: test ( gen_for | (',' test)* [','] )
              //   testlist_gexp.Expression = test + ( gen_for | test.Star(comma) + commaQ ); // ambiguous
              testlist_gexp.Rule = test + gen_for | test.Plus(comma) + commaQ;          // modified version
              // lambdef: 'lambda' [varargslist] ':' test
              lambdef.Rule = "lambda" + varargslist.Q() + ":" + test;
              // trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
              trailer.Rule = "(" + arglist.Q() + ")" | "[" + subscriptlist + "]" | "." + NAME;
              // subscriptlist: subscript (',' subscript)* [',']
              subscriptlist.Rule = subscript.Plus(comma) + commaQ;
              // subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
              subscript.Rule = "..." | test | test.Q() + ":" + test.Q() + sliceop.Q();
              // sliceop: ':' [test]
              sliceop.Rule = ":" + test.Q();
              // exprlist: expr (',' expr)* [',']
              exprlist.Rule = expr.Plus(comma)  + commaQ;
              // testlist: test (',' test)* [',']
              testlist.Rule = test.Plus(comma) + commaQ;
              // dictmaker: test ':' test (',' test ':' test)* [',']
              dictmaker.Rule = dict_elem.Plus(comma) + commaQ;
              dict_elem.Rule = test + ":" + test;

              // classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
              classdef.Rule = "class" + NAME + WithQ("(" + testlist.Q() + ")") + ":" + suite;

              // arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test)
              arglist.Rule = WithStar(argument + comma) +
               (argument + commaQ | "*" + test + WithQ(comma + "**" + test) | "**" + test);

              // argument: test [gen_for] | test '=' test  # Really [keyword '='] test
              argument.Rule = test + gen_for.Q() | test + "=" + test;  //# Really [keyword "="] test

              // list_iter: list_for | list_if
              list_iter.Rule = list_for | list_if;
              // list_for: 'for' exprlist 'in' testlist_safe [list_iter]
              list_for.Rule = "for" + exprlist + "in" + testlist_safe + list_iter.Q();
              // list_if: 'if' old_test [list_iter]
              list_if.Rule = "if" + old_test + list_iter.Q();

              // gen_iter: gen_for | gen_if
              gen_iter.Rule = gen_for | gen_if;
              // gen_for: 'for' exprlist 'in' or_test [gen_iter]
              gen_for.Rule = "for" + exprlist + "in" + or_test + gen_iter.Q();
              // gen_if: 'if' old_test [gen_iter]
              gen_if.Rule = "if" + old_test + gen_iter.Q();

              // testlist1: test (',' test)*
              testlist1.Rule = test.Plus(comma);

              // # not used in grammar, but may appear in "node" passed from Parser to Compiler
              // encoding_decl: NAME
              encoding_decl.Rule = NAME;

              // yield_expr: 'yield' [testlist]
              yield_expr.Rule = "yield" + testlist.Q();
              #endregion

              RegisterPunctuation( "(", ")", ",", ":" );

              TokenFilters.Add(new CodeOutlineFilter(true));

              id.AddKeywords("and", "del", "from", "not", "while", "as", "elif", "global", "or", "with",
                                  "assert", "else", "if", "pass", "yield", "break", "except", "import", "print",
                                  "class", "exec", "in", "raise", "continue", "finally", "is", "return",
                                  "def", "for", "lambda", "try");
        }