/// <summary>
        /// HTML Parser를 생성합니다.
        /// </summary>
        /// <returns></returns>
        private ShiftReduceParser get_pargen()
        {
            if (pargen != null)
            {
                return(pargen);
            }

            var gen = new ParserGenerator();

            // Non-Terminals
            var html        = gen.CreateNewProduction("HTML", false);
            var annote      = gen.CreateNewProduction("ANNOTATE", false);  // <!-- ~ -->
            var annote2     = gen.CreateNewProduction("ANNOTATE2", false); // <!~>
            var tag_closing = gen.CreateNewProduction("CLOSING", false);   // <~> ~ </~>
            var tag_aio     = gen.CreateNewProduction("ALLINONE", false);  // <~/>

            // Terminals
            var open          = gen.CreateNewProduction("open");
            var empty_open    = gen.CreateNewProduction("empty-open");
            var close         = gen.CreateNewProduction("close");
            var empty_close   = gen.CreateNewProduction("empty-close");
            var annoate_open  = gen.CreateNewProduction("annoate-open");
            var annoate_close = gen.CreateNewProduction("annoate-close");
            var annoate2_open = gen.CreateNewProduction("annoate2-open");
            var tag_name      = gen.CreateNewProduction("tag-name");
            var attr_name     = gen.CreateNewProduction("attr-name");

            html |= annoate_open + annote + annoate_close;
            html |= annoate_open + annote2;
            html |= tag_closing;
            html |= tag_aio;
            html |= html + html;
            html |= ParserGenerator.EmptyString;



            try
            {
                gen.PushStarts(html);
                gen.PrintProductionRules();
                gen.GenerateLALR();
                gen.PrintStates();
                gen.PrintTable();
            }
            catch (Exception e)
            {
                Console.Console.Instance.WriteLine(e.Message);
            }

            Console.Console.Instance.WriteLine(gen.GlobalPrinter.ToString());

            return(pargen = gen.CreateShiftReduceParserInstance());
        }
Beispiel #2
0
        static void ProcessPGSample()
        {
            var gen = new ParserGenerator();

            // Non-Terminals
            var exp       = gen.CreateNewProduction("exp", false);
            var term      = gen.CreateNewProduction("term", false);
            var factor    = gen.CreateNewProduction("factor", false);
            var func      = gen.CreateNewProduction("func", false);
            var arguments = gen.CreateNewProduction("args", false);

            // Terminals
            var plus     = gen.CreateNewProduction("plus");     // +
            var minus    = gen.CreateNewProduction("minus");    // -
            var multiple = gen.CreateNewProduction("multiple"); // *
            var divide   = gen.CreateNewProduction("divide");   // /
            var id       = gen.CreateNewProduction("id");       // [_$a-zA-Z][_$a-zA-Z0-9]*
            var op_open  = gen.CreateNewProduction("op_open");  // (
            var op_close = gen.CreateNewProduction("op_close"); // )
            var num      = gen.CreateNewProduction("num");      // [0-9]+
            var split    = gen.CreateNewProduction("split");    // ,

            exp       |= exp + plus + term;
            exp       |= exp + minus + term;
            exp       |= term;
            term      |= term + multiple + factor;
            term      |= term + divide + factor;
            term      |= factor;
            factor    |= op_open + exp + op_close;
            factor    |= num;
            factor    |= id;
            factor    |= func;
            func      |= id + op_open + arguments + op_close;
            arguments |= id;
            arguments |= arguments + split + id;
            arguments |= ParserGenerator.EmptyString;

            gen.PushStarts(exp);
            gen.GenerateLALR();
            gen.PrintStates();
            gen.PrintTable();

            Console.Instance.WriteLine(gen.GlobalPrinter.ToString());
        }
Beispiel #3
0
        /// <summary>
        /// Create JavaScript Parser
        /// </summary>
        private static void create_parser()
        {
            //parser = new JSParser().Parser;

            var gen = new ParserGenerator();

            // https://github.com/antlr/grammars-v4/blob/master/javascript/JavaScriptParser.g4
            var program                     = gen.CreateNewProduction("program", false);
            var sourceElement               = gen.CreateNewProduction("sourceElement", false);
            var statement                   = gen.CreateNewProduction("statement", false);
            var block                       = gen.CreateNewProduction("block", false);
            var statementList               = gen.CreateNewProduction("statementList", false);
            var statementListE              = gen.CreateNewProduction("statementListE", false);
            var variableStatement           = gen.CreateNewProduction("variableStatement", false);
            var variableDeclarationList     = gen.CreateNewProduction("variableDeclarationList", false);
            var variableDeclarationListArgs = gen.CreateNewProduction("variableDeclarationListArgs", false);
            var variableDeclaration         = gen.CreateNewProduction("variableDeclaration", false);
            var variableDeclarationT        = gen.CreateNewProduction("variableDeclarationT", false);
            var emptyStatement              = gen.CreateNewProduction("emptyStatement", false);
            var expressionStatement         = gen.CreateNewProduction("expressionStatement", false);
            var ifStatement                 = gen.CreateNewProduction("ifStatement", false);
            var expressionSequenceE         = gen.CreateNewProduction("expressionSequenceE", false);
            var iterationStatement          = gen.CreateNewProduction("iterationStatement", false);
            var varModifier                 = gen.CreateNewProduction("varModifier", false);
            var continueStatement           = gen.CreateNewProduction("continueStatement", false);
            var breakStatement              = gen.CreateNewProduction("breakStatement", false);
            var returnStatement             = gen.CreateNewProduction("returnStatement", false);
            var withStatement               = gen.CreateNewProduction("withStatement", false);
            var switchStatement             = gen.CreateNewProduction("switchStatement", false);
            var caseBlock                   = gen.CreateNewProduction("caseBlock", false);
            var caseClauses                 = gen.CreateNewProduction("caseClauses", false);
            var caseClausesE                = gen.CreateNewProduction("caseClausesE", false);
            var caseClause                  = gen.CreateNewProduction("caseClause", false);
            var defaultClause               = gen.CreateNewProduction("defaultClause", false);
            var labelledStatement           = gen.CreateNewProduction("labelledStatement", false);
            var throwStatement              = gen.CreateNewProduction("throwStatement", false);
            var tryStatement                = gen.CreateNewProduction("tryStatement", false);
            var catchProduction             = gen.CreateNewProduction("catchProduction", false);
            var finallyProduction           = gen.CreateNewProduction("finallyProduction", false);
            var debuggerStatement           = gen.CreateNewProduction("debuggerStatement", false);
            var functionDeclaration         = gen.CreateNewProduction("functionDeclaration", false);
            var classDeclaration            = gen.CreateNewProduction("classDeclaration", false);
            var classTail                   = gen.CreateNewProduction("classTail", false);
            var classElement                = gen.CreateNewProduction("classElement", false);
            var classElements               = gen.CreateNewProduction("classElements", false);
            var methodDefinition            = gen.CreateNewProduction("methodDefinition", false);
            var generatorMethod             = gen.CreateNewProduction("generatorMethod", false);
            var formalParameterList         = gen.CreateNewProduction("formalParameterList", false);
            var formalParameterArg          = gen.CreateNewProduction("formalParameterArg", false);
            var formalParameterArgS         = gen.CreateNewProduction("formalParameterArgS", false);
            var lastFormalParameterArg      = gen.CreateNewProduction("lastFormalParameterArg", false);
            var functionBody                = gen.CreateNewProduction("functionBody", false);
            var sourceElements              = gen.CreateNewProduction("sourceElements", false);
            var arrayLiteral                = gen.CreateNewProduction("arrayLiteral", false);
            var elementList                 = gen.CreateNewProduction("elementList", false);
            var lastElement                 = gen.CreateNewProduction("lastElement", false);
            var objectLiteral               = gen.CreateNewProduction("objectLiteral", false);
            var propertyAssignment          = gen.CreateNewProduction("propertyAssignment", false);
            var propertyAssignmentS         = gen.CreateNewProduction("propertyAssignmentS", false);
            var propertyName                = gen.CreateNewProduction("propertyName", false);
            var arguments                   = gen.CreateNewProduction("arguments", false);
            var lastArgument                = gen.CreateNewProduction("lastArgument", false);
            var expressionSequence          = gen.CreateNewProduction("expressionSequence", false);
            var singleExpression            = gen.CreateNewProduction("singleExpression", false);
            var singleExpressionS           = gen.CreateNewProduction("singleExpressionS", false);
            var arrowFunctionParameters     = gen.CreateNewProduction("arrowFunctionParameters", false);
            var arrowFunctionBody           = gen.CreateNewProduction("arrowFunctionBody", false);
            var assignmentOperator          = gen.CreateNewProduction("assignmentOperator", false);
            var literal                     = gen.CreateNewProduction("literal", false);
            var numericLiteral              = gen.CreateNewProduction("numericLiteral", false);
            var identifierName              = gen.CreateNewProduction("identifierName", false);
            var reservedWord                = gen.CreateNewProduction("reservedWord", false);
            var keyword                     = gen.CreateNewProduction("keyword", false);
            var getter                      = gen.CreateNewProduction("getter", false);
            var setter                      = gen.CreateNewProduction("setter", false);
            var eos  = gen.CreateNewProduction("eos", false);
            var star = gen.CreateNewProduction("star", false);

            program |= sourceElements;
            program |= ParserGenerator.EmptyString;

            sourceElements |= sourceElements + sourceElement;
            sourceElements |= ParserGenerator.EmptyString;

            sourceElement |= "Export" + statement;
            sourceElement |= statement;

            statement |= block;
            statement |= variableStatement;
            statement |= emptyStatement;
            statement |= classDeclaration;
            statement |= expressionStatement;
            statement |= ifStatement;
            statement |= iterationStatement;
            statement |= continueStatement;
            statement |= breakStatement;
            statement |= returnStatement;
            statement |= withStatement;
            statement |= labelledStatement;
            statement |= switchStatement;
            statement |= throwStatement;
            statement |= tryStatement;
            statement |= debuggerStatement;
            statement |= functionDeclaration;

            block |= "{" + statementList + "}";

            statementList  |= statementListE + statement;
            statementListE |= statementListE + statement;
            statementListE |= ParserGenerator.EmptyString;

            variableStatement |= varModifier + variableDeclarationList + eos;

            variableDeclarationList     |= variableDeclaration + variableDeclarationListArgs;
            variableDeclarationListArgs |= "," + variableDeclarationListArgs;
            variableDeclarationListArgs |= ParserGenerator.EmptyString;

            variableDeclaration |= "Identifier" + variableDeclarationT;
            variableDeclaration |= arrayLiteral + variableDeclarationT;
            variableDeclaration |= objectLiteral + variableDeclarationT;

            variableDeclarationT |= "=" + singleExpression;
            variableDeclarationT |= ParserGenerator.EmptyString;

            emptyStatement |= "SemiColon";

            expressionStatement |= expressionSequence + eos; // TODO: 90: {this.notOpenBraceAndNotFunction()}? expressionSequence eos

            ifStatement |= gen.TryCreateNewProduction("If") + "(" + expressionSequence + ")" + statement;
            ifStatement |= gen.TryCreateNewProduction("If") + "(" + expressionSequence + ")" + statement + "Else" + statement;

            expressionSequenceE |= expressionSequence;
            expressionSequenceE |= ParserGenerator.EmptyString;

            iterationStatement |= "Do" + statement + "While" + "(" + expressionSequence + ")" + eos;
            iterationStatement |= gen.TryCreateNewProduction("While") + "(" + expressionSequence + ")" + statement;
            iterationStatement |= gen.TryCreateNewProduction("For") + "(" + expressionSequenceE + ";" + expressionSequenceE + ";" + expressionSequenceE + ")" + statement;
            iterationStatement |= gen.TryCreateNewProduction("For") + "(" + varModifier + variableDeclarationList + ";" + expressionSequenceE + ";" + expressionSequenceE + ")" + statement;
            iterationStatement |= gen.TryCreateNewProduction("For") + "(" + singleExpression + "In" + expressionSequence + ")" + statement;
            iterationStatement |= gen.TryCreateNewProduction("For") + "(" + singleExpression + "Identifier" + expressionSequence + ")" + statement;
            iterationStatement |= gen.TryCreateNewProduction("For") + "(" + varModifier + variableDeclarationList + "In" + expressionSequence + ")" + statement;
            iterationStatement |= gen.TryCreateNewProduction("For") + "(" + varModifier + variableDeclarationList + "Identifier" + expressionSequence + ")" + statement;

            varModifier |= "Var";
            varModifier |= "Let";
            varModifier |= "Const";

            continueStatement |= "Continue" + eos;  // TODO
            continueStatement |= "Continue" + identifierName + eos;

            breakStatement |= "Break" + eos; // TODO
            breakStatement |= "Break" + identifierName + eos;

            returnStatement |= "Return" + eos; // TODO
            returnStatement |= "Return" + expressionSequence + eos;

            withStatement |= gen.TryCreateNewProduction("With") + "(" + expressionSequence + ")" + statement;

            switchStatement |= gen.TryCreateNewProduction("Switch") + "(" + expressionSequence + ")" + caseBlock;

            caseBlock |= gen.TryCreateNewProduction("{") + "}";
            caseBlock |= "{" + caseClauses + "}";
            caseBlock |= "{" + caseClauses + defaultClause + caseClauses + "}";
            caseBlock |= "{" + defaultClause + caseClauses + "}";
            caseBlock |= "{" + caseClauses + defaultClause + "}";
            caseBlock |= "{" + defaultClause + "}";

            caseClauses  |= caseClausesE + caseClause;
            caseClausesE |= caseClausesE + caseClause;
            caseClausesE |= ParserGenerator.EmptyString;

            caseClause |= "Case" + expressionSequence + ":";
            caseClause |= "Case" + expressionSequence + ":" + statementList;

            defaultClause |= gen.TryCreateNewProduction("Default") + ":";
            defaultClause |= gen.TryCreateNewProduction("Default") + ":" + statementList;

            labelledStatement |= gen.TryCreateNewProduction("Identifier") + ":" + statement;

            throwStatement |= "Throw" + expressionSequence + eos; // TODO

            tryStatement |= gen.TryCreateNewProduction("Try") + block + catchProduction;
            tryStatement |= gen.TryCreateNewProduction("Try") + block + catchProduction + finallyProduction;
            tryStatement |= gen.TryCreateNewProduction("Try") + block + finallyProduction;

            catchProduction |= gen.TryCreateNewProduction("Catch") + "(" + gen.TryCreateNewProduction("Identifier") + ")" + block;

            finallyProduction |= "Finally" + block;

            debuggerStatement |= "Debugger" + eos;

            functionDeclaration |= "Function" + gen.TryCreateNewProduction("Identifier") + "(" + gen.TryCreateNewProduction(")") + "{" + functionBody + "}";
            functionDeclaration |= "Function" + gen.TryCreateNewProduction("Identifier") + "(" + formalParameterList + ")" + gen.TryCreateNewProduction("{") + functionBody + "}";

            classDeclaration |= gen.TryCreateNewProduction("Class") + "Identifier" + classTail;

            classTail     |= "{" + classElements + "}";
            classTail     |= "Extends" + singleExpression + "{" + classElements + "}";
            classElements |= classElements + classElement;

            classElement |= methodDefinition;
            classElement |= "Static" + methodDefinition;
            classElement |= "Identifier" + methodDefinition;
            classElement |= "Static" + "Identifier" + methodDefinition;
            classElement |= emptyStatement;

            methodDefinition |= propertyName + "(" + gen.TryCreateNewProduction(")") + "{" + functionBody + "}";
            methodDefinition |= propertyName + "(" + formalParameterList + gen.TryCreateNewProduction(")") + "{" + functionBody + "}";
            methodDefinition |= getter + "(" + gen.TryCreateNewProduction(")") + "{" + functionBody + "}";
            methodDefinition |= setter + "(" + gen.TryCreateNewProduction(")") + "{" + functionBody + "}";
            methodDefinition |= setter + "(" + formalParameterList + gen.TryCreateNewProduction(")") + "{" + functionBody + "}";
            methodDefinition |= generatorMethod;

            generatorMethod |= "Identifier" + gen.TryCreateNewProduction("(") + ")" + gen.TryCreateNewProduction("{") + functionBody + "}";
            generatorMethod |= "*" + gen.TryCreateNewProduction("Identifier") + "(" + gen.TryCreateNewProduction(")") + "{" + functionBody + "}";
            generatorMethod |= gen.TryCreateNewProduction("Identifier") + "(" + formalParameterList + gen.TryCreateNewProduction(")") + "{" + functionBody + "}";
            generatorMethod |= gen.TryCreateNewProduction("*") + "Identifier" + "(" + formalParameterList + gen.TryCreateNewProduction(")") + "{" + functionBody + "}";

            formalParameterList |= formalParameterArg + formalParameterArgS;
            formalParameterList |= formalParameterArg + "," + lastFormalParameterArg;
            formalParameterList |= formalParameterArg + formalParameterArgS + "," + lastFormalParameterArg;
            formalParameterList |= lastFormalParameterArg;
            formalParameterList |= arrayLiteral;
            formalParameterList |= objectLiteral;

            formalParameterArgS |= "," + formalParameterArg + formalParameterArgS;
            formalParameterArgS |= ParserGenerator.EmptyString;

            formalParameterArg |= "Identifier";
            formalParameterArg |= gen.TryCreateNewProduction("Identifier") + "=" + singleExpression;

            lastFormalParameterArg |= gen.TryCreateNewProduction("Ellipsis") + "Identifier";

            functionBody |= sourceElements;
            functionBody |= ParserGenerator.EmptyString;

            arrayLiteral |= "[" + star + "]";
            arrayLiteral |= "[" + star + elementList + star + "]";

            star |= "*" + star;
            star |= ParserGenerator.EmptyString;

            elementList |= singleExpression + singleExpressionS;
            elementList |= singleExpression + singleExpressionS + "," + lastElement;

            singleExpressionS |= "," + singleExpression + singleExpressionS;
            singleExpressionS |= ParserGenerator.EmptyString;

            lastElement |= gen.TryCreateNewProduction("Ellipsis") + "Identifier";

            objectLiteral |= gen.TryCreateNewProduction("{") + "}";
            objectLiteral |= "{" + gen.TryCreateNewProduction(",") + "}";
            objectLiteral |= "{" + propertyAssignment + propertyAssignmentS + "}";
            objectLiteral |= "{" + propertyAssignment + propertyAssignmentS + gen.TryCreateNewProduction(",") + "}";

            propertyAssignmentS |= "," + propertyAssignment + propertyAssignmentS;
            propertyAssignmentS |= ParserGenerator.EmptyString;

            propertyAssignment |= propertyName + ":" + singleExpression;
            propertyAssignment |= propertyName + "=" + singleExpression;
            propertyAssignment |= "[" + singleExpression + "]" + gen.TryCreateNewProduction(":") + singleExpression;
            propertyAssignment |= getter + "(" + gen.TryCreateNewProduction(")") + "{" + functionBody + "}";
            propertyAssignment |= setter + "(" + gen.TryCreateNewProduction(")") + "{" + functionBody + "}";
            propertyAssignment |= setter + "(" + formalParameterList + gen.TryCreateNewProduction(")") + "{" + functionBody + "}";
            propertyAssignment |= generatorMethod;
            propertyAssignment |= identifierName;

            propertyName |= identifierName;
            propertyName |= "StringLiteral";
            propertyName |= numericLiteral;

            arguments |= gen.TryCreateNewProduction("(") + ")";

            arguments |= "(" + singleExpression + singleExpressionS + ")";
            arguments |= "(" + singleExpression + singleExpressionS + "," + lastArgument + ")";

            lastArgument |= gen.TryCreateNewProduction("Ellipsis") + "Identifier";

            expressionSequence |= singleExpression + singleExpressionS;

            singleExpression |= "Function" + gen.TryCreateNewProduction("(") + ")" + gen.TryCreateNewProduction("{") + functionBody + "}";
            singleExpression |= "Function" + gen.TryCreateNewProduction("Identifier") + "(" + gen.TryCreateNewProduction(")") + "{" + functionBody + "}";
            singleExpression |= "Function" + gen.TryCreateNewProduction("(") + formalParameterList + gen.TryCreateNewProduction(")") + "{" + functionBody + "}";
            singleExpression |= "Function" + gen.TryCreateNewProduction("Identifier") + "(" + formalParameterList + gen.TryCreateNewProduction(")") + "{" + functionBody + "}";
            singleExpression |= "Class" + classTail;
            singleExpression |= gen.TryCreateNewProduction("Class") + "Identifier" + classTail;
            singleExpression |= singleExpression + "[" + expressionSequence + "]";
            singleExpression |= singleExpression + "." + identifierName;
            singleExpression |= singleExpression + arguments;
            singleExpression |= "New" + singleExpression;
            singleExpression |= "New" + singleExpression + arguments;
            singleExpression |= singleExpression + "++"; // TODO
            singleExpression |= singleExpression + "--"; // TODO
            singleExpression |= "Delete" + singleExpression;
            singleExpression |= "Void" + singleExpression;
            singleExpression |= "Typeof" + singleExpression;
            singleExpression |= "++" + singleExpression;
            singleExpression |= "--" + singleExpression;
            singleExpression |= "+" + singleExpression;
            singleExpression |= "-" + singleExpression;
            singleExpression |= "~" + singleExpression;
            singleExpression |= "!" + singleExpression;
            singleExpression |= singleExpression + "*" + singleExpression;
            singleExpression |= singleExpression + "/" + singleExpression;
            singleExpression |= singleExpression + "%" + singleExpression;
            singleExpression |= singleExpression + "+" + singleExpression;
            singleExpression |= singleExpression + "-" + singleExpression;
            singleExpression |= singleExpression + "<<" + singleExpression;
            singleExpression |= singleExpression + ">>" + singleExpression;
            singleExpression |= singleExpression + ">>>" + singleExpression;
            singleExpression |= singleExpression + "<" + singleExpression;
            singleExpression |= singleExpression + ">" + singleExpression;
            singleExpression |= singleExpression + "<=" + singleExpression;
            singleExpression |= singleExpression + ">=" + singleExpression;

            singleExpression |= singleExpression + "Instanceof" + singleExpression;
            singleExpression |= singleExpression + "In" + singleExpression;
            singleExpression |= singleExpression + "==" + singleExpression;
            singleExpression |= singleExpression + "!=" + singleExpression;
            singleExpression |= singleExpression + "===" + singleExpression;
            singleExpression |= singleExpression + "!==" + singleExpression;
            singleExpression |= singleExpression + "&" + singleExpression;
            singleExpression |= singleExpression + "^" + singleExpression;
            singleExpression |= singleExpression + "|" + singleExpression;
            singleExpression |= singleExpression + "&&" + singleExpression;
            singleExpression |= singleExpression + "||" + singleExpression;
            singleExpression |= singleExpression + "?" + singleExpression + ":" + singleExpression;
            singleExpression |= singleExpression + "=" + singleExpression;
            singleExpression |= singleExpression + assignmentOperator + singleExpression;
            singleExpression |= singleExpression + "TemplateStringLiteral";
            singleExpression |= "This";
            singleExpression |= "Identifier";
            singleExpression |= "Super";
            singleExpression |= literal;
            singleExpression |= arrayLiteral;
            singleExpression |= objectLiteral;
            singleExpression |= "(" + expressionSequence + ")";
            singleExpression |= arrowFunctionParameters + "=>" + arrowFunctionBody;

            arrowFunctionParameters |= "Identifier";
            arrowFunctionParameters |= gen.TryCreateNewProduction("(") + ")";
            arrowFunctionParameters |= "(" + formalParameterList + ")";

            arrowFunctionBody |= singleExpression;
            arrowFunctionBody |= "{" + functionBody + "}";

            assignmentOperator |= "*=";
            assignmentOperator |= "/=";
            assignmentOperator |= "%=";
            assignmentOperator |= "+=";
            assignmentOperator |= "-=";
            assignmentOperator |= "<<=";
            assignmentOperator |= ">>=";
            assignmentOperator |= ">>>=";
            assignmentOperator |= "&=";
            assignmentOperator |= "^=";
            assignmentOperator |= "|=";

            literal |= "NullLiteral";
            literal |= "BooleanLiteral";
            literal |= "StringLiteral";
            literal |= "TemplateStringLiteral";
            literal |= "RegularExpressionLiteral";
            literal |= "numericLiteral";

            numericLiteral |= "DecimalLiteral";
            numericLiteral |= "HexIntegerLiteral";
            numericLiteral |= "OctalIntegerLiteral";
            numericLiteral |= "OctalIntegerLiteral2";
            numericLiteral |= "BinaryIntegerLiteral";

            identifierName |= "Identifier";
            identifierName |= reservedWord;

            reservedWord |= keyword;
            reservedWord |= "NullLiteral";
            reservedWord |= "BooleanLiteral";

            keyword |= "Class";
            keyword |= "Break";
            keyword |= "Do";
            keyword |= "Instanceof";
            keyword |= "Typeof";
            keyword |= "Case";
            keyword |= "Else";
            keyword |= "New";
            keyword |= "Var";
            keyword |= "Catch";
            keyword |= "Finally";
            keyword |= "Return";
            keyword |= "Void";
            keyword |= "Continue";
            keyword |= "For";
            keyword |= "Switch";
            keyword |= "While";
            keyword |= "Debugger";
            keyword |= "Function";
            keyword |= "This";
            keyword |= "With";
            keyword |= "Default";
            keyword |= "If";
            keyword |= "Throw";
            keyword |= "Delete";
            keyword |= "In";
            keyword |= "Try";
            keyword |= "Enum";
            keyword |= "Extends";
            keyword |= "Super";
            keyword |= "Const";
            keyword |= "Export";
            keyword |= "Import";
            keyword |= "Implements";
            keyword |= "Let";
            keyword |= "Private";
            keyword |= "Public";
            keyword |= "Interface";
            keyword |= "Package";
            keyword |= "Protected";
            keyword |= "Static";
            keyword |= "Yield";

            //getter |= propertyName; // TODO

            //setter |= propertyName; // TODO

            eos |= "SemiColon"; // TODO
            //eos |= "$";
            //eos |= ParserGenerator.EmptyString;


            gen.PushConflictSolver(true, gen.TryCreateNewProduction("Break"),
                                   gen.TryCreateNewProduction("Do"),
                                   gen.TryCreateNewProduction("Typeof"),
                                   gen.TryCreateNewProduction("Case"),
                                   gen.TryCreateNewProduction("Else"),
                                   gen.TryCreateNewProduction("New"),
                                   gen.TryCreateNewProduction("Var"),
                                   gen.TryCreateNewProduction("Catch"),
                                   gen.TryCreateNewProduction("Finally"),
                                   gen.TryCreateNewProduction("Return"),
                                   gen.TryCreateNewProduction("Void"),
                                   gen.TryCreateNewProduction("Continue"),
                                   gen.TryCreateNewProduction("For"),
                                   gen.TryCreateNewProduction("Switch"),
                                   gen.TryCreateNewProduction("While"),
                                   gen.TryCreateNewProduction("Debugger"),
                                   gen.TryCreateNewProduction("Function"),
                                   gen.TryCreateNewProduction("This"),
                                   gen.TryCreateNewProduction("With"),
                                   gen.TryCreateNewProduction("Default"),
                                   gen.TryCreateNewProduction("If"),
                                   gen.TryCreateNewProduction("Throw"),
                                   gen.TryCreateNewProduction("Delete"),
                                   gen.TryCreateNewProduction("Try"),
                                   gen.TryCreateNewProduction("Enum"),
                                   gen.TryCreateNewProduction("Class"),
                                   gen.TryCreateNewProduction("Extends"),
                                   gen.TryCreateNewProduction("Super"),
                                   gen.TryCreateNewProduction("Const"),
                                   gen.TryCreateNewProduction("Export"),
                                   gen.TryCreateNewProduction("Import"),
                                   gen.TryCreateNewProduction("Implements"),
                                   gen.TryCreateNewProduction("Let"),
                                   gen.TryCreateNewProduction("Private"),
                                   gen.TryCreateNewProduction("Public"),
                                   gen.TryCreateNewProduction("Interface"),
                                   gen.TryCreateNewProduction("Package"),
                                   gen.TryCreateNewProduction("Protected"),
                                   gen.TryCreateNewProduction("Static"),
                                   gen.TryCreateNewProduction("Yield"));

            gen.PushConflictSolver(true, gen.TryCreateNewProduction("Identifier"));
            gen.PushConflictSolver(true, gen.TryCreateNewProduction("NullLiteral"));
            gen.PushConflictSolver(true, gen.TryCreateNewProduction("BooleanLiteral"));
            gen.PushConflictSolver(true, gen.TryCreateNewProduction("StringLiteral"));
            gen.PushConflictSolver(true, gen.TryCreateNewProduction("TemplateStringLiteral"));
            gen.PushConflictSolver(true, gen.TryCreateNewProduction("RegularExpressionLiteral"));
            gen.PushConflictSolver(true, gen.TryCreateNewProduction("numericLiteral"));
            gen.PushConflictSolver(true, gen.TryCreateNewProduction("DecimalLiteral"));
            gen.PushConflictSolver(true, gen.TryCreateNewProduction("HexIntegerLiteral"));
            gen.PushConflictSolver(true, gen.TryCreateNewProduction("OctalIntegerLiteral"));
            gen.PushConflictSolver(true, gen.TryCreateNewProduction("OctalIntegerLiteral2"));
            gen.PushConflictSolver(true, gen.TryCreateNewProduction("BinaryIntegerLiteral"));

            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(ifStatement, 0));
            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(keyword, 0));
            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(block, 0));
            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(statementListE, 1));
            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(caseClausesE, 1));
            //gen.PushConflictSolver(true, new Tuple<ParserProduction, int>(eos, 1));
            //gen.PushConflictSolver(false, gen.TryCreateNewProduction("Class"));

            gen.PushConflictSolver(false, gen.TryCreateNewProduction("["));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("("));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("{"));

            gen.PushConflictSolver(false, gen.TryCreateNewProduction("."));

            gen.PushConflictSolver(false, gen.TryCreateNewProduction("++"));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("--"));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("~"));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("!"));

            // +, -
            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(singleExpression, 16), new Tuple <ParserProduction, int>(singleExpression, 17));

            gen.PushConflictSolver(false, gen.TryCreateNewProduction("*"), gen.TryCreateNewProduction("/"), gen.TryCreateNewProduction("%"));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("+"), gen.TryCreateNewProduction("-"));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("<<"), gen.TryCreateNewProduction(">>"), gen.TryCreateNewProduction(">>>"));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("<"), gen.TryCreateNewProduction(">"), gen.TryCreateNewProduction("<="), gen.TryCreateNewProduction(">="));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("Instanceof"));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("In"));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("=="), gen.TryCreateNewProduction("!="), gen.TryCreateNewProduction("==="), gen.TryCreateNewProduction("!=="));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("&"));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("^"));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("|"));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("&&"));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("||"));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("?"), gen.TryCreateNewProduction(":"));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("="));

            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(singleExpression, 47));

            gen.PushConflictSolver(false, gen.TryCreateNewProduction("*="));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("/="));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("%="));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("+="));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("-="));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("<<="));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction(">>="));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction(">>>="));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("&="));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("^="));
            gen.PushConflictSolver(false, gen.TryCreateNewProduction("|="));

            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(formalParameterArgS, 0));
            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(formalParameterArgS, 1));
            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(arrowFunctionBody, 0));

            gen.PushConflictSolver(false, gen.TryCreateNewProduction(","));

            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(singleExpressionS, 1));
            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(propertyAssignmentS, 1));

            gen.PushConflictSolver(false, gen.TryCreateNewProduction("SemiColon"));

            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(sourceElements, 1));
            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(caseBlock, 0));

            gen.PushConflictSolver(false, gen.TryCreateNewProduction("}"));

            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(functionBody, 1));


            try
            {
                gen.PushStarts(program);
                gen.PrintProductionRules();

                System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
                gen.Generate();
                var end = sw.ElapsedMilliseconds;
                sw.Stop();
                Log.Logs.Instance.Push($"{end.ToString("#,#")}");
                gen.PrintStates();
                gen.PrintTable();
            }
            catch (Exception e)
            {
                Log.Logs.Instance.PushError(e.Message + "\r\n" + e.StackTrace);
            }

            File.WriteAllText(DateTime.Now.Ticks + ".txt", gen.GlobalPrinter.ToString());

            parser = gen.CreateExtendedShiftReduceParserInstance();
            File.WriteAllText("jsparser.cs", parser.ToCSCode("JSParser"));
        }
Beispiel #4
0
        /// <summary>
        /// ESRCAL의 파서제너레이터를 생성합니다.
        /// </summary>
        /// <returns></returns>
        private ShiftReduceParser get_pargen()
        {
            if (pargen != null)
            {
                return(pargen);
            }

            var gen = new ParserGenerator();

            // Non-Terminals
            var expr = gen.CreateNewProduction("expr", false);

            // Terminals
            //var id = gen.CreateNewProduction("id");
            var num = gen.CreateNewProduction("num");
            //var str = gen.CreateNewProduction("str");
            var plus     = gen.CreateNewProduction("plus");
            var minus    = gen.CreateNewProduction("minus");
            var multiple = gen.CreateNewProduction("multiple");
            var divide   = gen.CreateNewProduction("divide");
            //var loop = gen.CreateNewProduction("loop");
            var op_open  = gen.CreateNewProduction("op_open");
            var op_close = gen.CreateNewProduction("op_close");

            //var pp_open = gen.CreateNewProduction("pp_open"); // [
            //var pp_close = gen.CreateNewProduction("pp_close"); // ]
            //var equal = gen.CreateNewProduction("equal");
            //var to = gen.CreateNewProduction("to");
            //var scolon = gen.CreateNewProduction("scolon");
            //var comma = gen.CreateNewProduction("comma");
            //var _foreach = gen.CreateNewProduction("foreach");
            //var _if = gen.CreateNewProduction("if");
            //var _else = gen.CreateNewProduction("else");

            expr |= num + ParserAction.Create(x => x.UserContents = double.Parse(x.Contents));
            expr |= expr + plus + expr + ParserAction.Create(x => x.UserContents = (double)x.Childs[0].UserContents + (double)x.Childs[2].UserContents);
            expr |= expr + minus + expr + ParserAction.Create(x => x.UserContents = (double)x.Childs[0].UserContents - (double)x.Childs[2].UserContents);
            expr |= expr + multiple + expr + ParserAction.Create(x => x.UserContents = (double)x.Childs[0].UserContents * (double)x.Childs[2].UserContents);
            expr |= expr + divide + expr + ParserAction.Create(x => x.UserContents = (double)x.Childs[0].UserContents / (double)x.Childs[2].UserContents);
            expr |= minus + expr + ParserAction.Create(x => x.UserContents = -(double)x.Childs[1].UserContents);
            expr |= op_open + expr + op_close + ParserAction.Create(x => x.UserContents = x.Childs[1].UserContents);

            // right associativity, -
            gen.PushConflictSolver(false, new Tuple <ParserProduction, int>(expr, 5));
            // left associativity, *, /
            gen.PushConflictSolver(true, multiple, divide);
            // left associativity, +, -
            gen.PushConflictSolver(true, plus, minus);

            try
            {
                gen.PushStarts(expr);
                gen.PrintProductionRules();
                gen.GenerateLALR();
                gen.PrintStates();
                gen.PrintTable();
            }
            catch (Exception e)
            {
                Console.Console.Instance.WriteLine(e.Message);
            }

            Console.Console.Instance.WriteLine(gen.GlobalPrinter.ToString());

            return(pargen = gen.CreateShiftReduceParserInstance());
        }
Beispiel #5
0
        static ShiftReduceParser CreateJSonParser()
        {
            var gen = new ParserGenerator();

            var JSON     = gen.CreateNewProduction("JSON", false);
            var ARRAY    = gen.CreateNewProduction("ARRAY", false);
            var OBJECT   = gen.CreateNewProduction("OBJECT", false);
            var MEMBERS  = gen.CreateNewProduction("MEMBERS", false);
            var PAIR     = gen.CreateNewProduction("PAIR", false);
            var ELEMENTS = gen.CreateNewProduction("ELEMENTS", false);
            var VALUE    = gen.CreateNewProduction("VALUE", false);

            var object_starts = gen.CreateNewProduction("object_starts");
            var object_ends   = gen.CreateNewProduction("object_ends");
            var comma         = gen.CreateNewProduction("comma");
            var v_pair        = gen.CreateNewProduction("v_pair");
            var array_starts  = gen.CreateNewProduction("array_starts");
            var array_ends    = gen.CreateNewProduction("array_ends");
            var v_true        = gen.CreateNewProduction("v_true");
            var v_false       = gen.CreateNewProduction("v_false");
            var v_null        = gen.CreateNewProduction("v_null");
            var v_string      = gen.CreateNewProduction("v_string");
            var v_number      = gen.CreateNewProduction("v_number");

            JSON    |= OBJECT + ParserAction.Create(x => x.UserContents = x.Childs[0].UserContents);
            JSON    |= ARRAY + ParserAction.Create(x => x.UserContents = x.Childs[0].UserContents);
            OBJECT  |= object_starts + object_ends + ParserAction.Create(x => x.UserContents = new json_object());
            OBJECT  |= object_starts + MEMBERS + object_ends + ParserAction.Create(x => x.UserContents = x.Childs[1].UserContents);
            MEMBERS |= PAIR + ParserAction.Create(x => {
                var jo = new json_object();
                jo.keyvalue.Add(new KeyValuePair <string, json_value>(x.Childs[0].Childs[0].Contents, x.Childs[0].Childs[2].UserContents as json_value));
                x.UserContents = jo;
            });
            MEMBERS |= PAIR + comma + MEMBERS + ParserAction.Create(x => {
                var jo = x.Childs[2].UserContents as json_object;
                jo.keyvalue.Insert(0, new KeyValuePair <string, json_value>(x.Childs[0].Childs[0].Contents, x.Childs[0].Childs[2].UserContents as json_value));
                x.UserContents = jo;
            });
            PAIR     |= v_string + v_pair + VALUE + ParserAction.Create(x => { });
            ARRAY    |= array_starts + array_ends + ParserAction.Create(x => x.UserContents = new json_array());
            ARRAY    |= array_starts + ELEMENTS + array_ends + ParserAction.Create(x => x.UserContents = x.Childs[1].UserContents);
            ELEMENTS |= VALUE + ParserAction.Create(x => {
                var ja = new json_array();
                ja.array.Add(x.Childs[0].UserContents as json_value);
                x.UserContents = ja;
            });
            ELEMENTS |= VALUE + comma + ELEMENTS + ParserAction.Create(x => {
                var ja = x.Childs[2].UserContents as json_array;
                ja.array.Insert(0, x.Childs[0].UserContents as json_value);
                x.UserContents = ja;
            });
            VALUE |= v_string + ParserAction.Create(x => x.UserContents = new json_string {
                str = x.Contents
            });
            VALUE |= v_number + ParserAction.Create(x => x.UserContents = new json_numeric {
                numstr = x.Contents
            });
            VALUE |= OBJECT + ParserAction.Create(x => x.UserContents = x.Childs[0].UserContents);
            VALUE |= ARRAY + ParserAction.Create(x => x.UserContents = x.Childs[0].UserContents);
            VALUE |= v_true + ParserAction.Create(x => x.UserContents = new json_state {
                type = json_token.v_true
            });
            VALUE |= v_false + ParserAction.Create(x => x.UserContents = new json_state {
                type = json_token.v_false
            });
            VALUE |= v_null + ParserAction.Create(x => x.UserContents = new json_state {
                type = json_token.v_null
            });

            gen.PushStarts(JSON);
            gen.PrintProductionRules();
            gen.Generate();
            gen.PrintStates();
            gen.PrintTable();

#if false
            Console.WriteLine(gen.GlobalPrinter.ToString());
            Console.WriteLine(gen.CreateExtendedShiftReduceParserInstance().ToCSCode("json_parser"));
#endif

            return(gen.CreateExtendedShiftReduceParserInstance());
        }
Beispiel #6
0
        /// <summary>
        /// SRCAL의 파서제너레이터를 생성합니다.
        /// </summary>
        /// <returns></returns>
        private ExtendedShiftReduceParser get_pargen()
        {
            if (pargen != null)
            {
                return(pargen);
            }

            var gen = new ParserGenerator();

            // Non-Terminals
            var script   = gen.CreateNewProduction("script", false);
            var line     = gen.CreateNewProduction("line", false);
            var lines    = gen.CreateNewProduction("lines", false);
            var expr     = gen.CreateNewProduction("expr", false);
            var block    = gen.CreateNewProduction("block", false);
            var iblock   = gen.CreateNewProduction("iblock", false);
            var index    = gen.CreateNewProduction("index", false);
            var variable = gen.CreateNewProduction("variable", false);
            var argument = gen.CreateNewProduction("argument", false);
            var function = gen.CreateNewProduction("function", false);
            var runnable = gen.CreateNewProduction("runnable", false);

            // Terminals
            var name     = gen.CreateNewProduction("name");
            var _const   = gen.CreateNewProduction("const"); // number | string
            var loop     = gen.CreateNewProduction("loop");
            var op_open  = gen.CreateNewProduction("op_open");
            var op_close = gen.CreateNewProduction("op_close");
            var pp_open  = gen.CreateNewProduction("pp_open");  // [
            var pp_close = gen.CreateNewProduction("pp_close"); // ]
            var equal    = gen.CreateNewProduction("equal");
            var to       = gen.CreateNewProduction("to");
            var scolon   = gen.CreateNewProduction("scolon");
            var comma    = gen.CreateNewProduction("comma");
            var plus     = gen.CreateNewProduction("plus");     // +
            var minus    = gen.CreateNewProduction("minus");    // -
            var multiple = gen.CreateNewProduction("multiple"); // *
            var divide   = gen.CreateNewProduction("divide");   // /
            var _foreach = gen.CreateNewProduction("foreach");
            var _if      = gen.CreateNewProduction("if");
            var _else    = gen.CreateNewProduction("else");

            script |= lines + ParserAction.Create((m, f, b, x) => {
                var module = new LPModule();
                var sfunc  = module.CreateFunction("start");
                var bb     = sfunc.CreateBasicBlock();
                x.Childs[0].Action(module, sfunc, bb, x.Childs[0]);
                x.UserContents = module;
            });
            script |= ParserGenerator.EmptyString + ParserAction.Create((m, f, b, x) => {
                x.UserContents = new LPModule();
            });

            block |= pp_open + iblock + pp_close + ParserAction.Create((m, f, b, x) => { });
            block |= line + ParserAction.Create((m, f, b, x) => { });

            iblock |= block + ParserAction.Create((m, f, b, x) => { });
            iblock |= lines + ParserAction.Create((m, f, b, x) => { });
            iblock |= ParserGenerator.EmptyString + ParserAction.Create((m, f, b, x) => { });

            line |= expr + ParserAction.Create((m, f, b, x) => { });

            lines |= expr + ParserAction.Create((m, f, b, x) => {
                x.Childs[0].Action(m, f, b, x.Childs[0]);
            });
            lines |= expr + lines + ParserAction.Create((m, f, b, x) => {
                x.Childs[0].Action(m, f, b, x.Childs[0]);
                x.Childs[1].Action(m, f, b, x.Childs[1]);
            });

            expr |= function + ParserAction.Create((m, f, b, x) => {
                x.Childs[0].Action(m, f, b, x.Childs[0]);
            });
            expr |= name + equal + index + ParserAction.Create((m, f, b, x) => { });
            expr |= runnable + ParserAction.Create((m, f, b, x) => { });

            function |= name + op_open + op_close + ParserAction.Create((m, f, b, x) => {
                var caller      = m.CreateFunction(x.Childs[0].Contents);
                caller.IsExtern = true;
                var ci          = LPCallOperator.Create(caller, new List <LPUser>());
                b.Insert(ci);
                x.UserContents = ci;
            });
            function |= name + op_open + argument + op_close + ParserAction.Create((m, f, b, x) => {
                var caller      = m.CreateFunction(x.Childs[0].Contents);
                caller.IsExtern = true;
                x.Childs[2].Action(m, f, b, x);
                var ci = LPCallOperator.Create(caller, x.Childs[2].UserContents as List <LPUser>);
                b.Insert(ci);
                x.UserContents = ci;
            });

            argument |= index + ParserAction.Create((m, f, b, x) => {
                x.Childs[0].Action(m, f, b, x);
                x.UserContents = new List <LPUser> {
                    x.Childs[0].UserContents as LPUser
                };
            });
            argument |= index + comma + argument + ParserAction.Create((m, f, b, x) => { });

            index |= variable + ParserAction.Create((m, f, b, x) => { });
            index |= variable + pp_open + variable + pp_close + ParserAction.Create((m, f, b, x) => { });
            index |= index + plus + index + ParserAction.Create((m, f, b, x) => { });
            index |= index + minus + index + ParserAction.Create((m, f, b, x) => { });
            index |= index + multiple + index + ParserAction.Create((m, f, b, x) => { });
            index |= index + divide + index + ParserAction.Create((m, f, b, x) => { });
            index |= minus + index + ParserAction.Create((m, f, b, x) => { });
            index |= op_open + index + op_close + ParserAction.Create((m, f, b, x) => { });

            variable |= name + ParserAction.Create((m, f, b, x) => { });
            variable |= function + ParserAction.Create((m, f, b, x) => { });
            variable |= _const + ParserAction.Create((m, f, b, x) => { x.UserContents = LPConstant.Create(x.Childs[0].Contents); });

            runnable |= loop + op_open + name + equal + index + to + index + op_close + block + ParserAction.Create((m, f, b, x) => { });
            runnable |= _foreach + op_open + name + scolon + index + op_close + block + ParserAction.Create((m, f, b, x) => { });
            runnable |= _if + op_open + index + op_close + block + ParserAction.Create((m, f, b, x) => { });
            runnable |= _if + op_open + index + op_close + block + _else + block + ParserAction.Create((m, f, b, x) => { });

            gen.PushConflictSolver(true, _else);
            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(runnable, 2));
            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(index, 6));
            gen.PushConflictSolver(false, multiple, divide);
            gen.PushConflictSolver(false, plus, minus);

            //gen.PushConflictSolver(true, new Tuple<ParserProduction, int>(index, 1));
            gen.PushConflictSolver(false, pp_open);
            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(index, 0));

            try
            {
                gen.PushStarts(script);
                gen.PrintProductionRules();
                gen.GenerateLALR2();
                gen.PrintStates();
                gen.PrintTable();
            }
            catch (Exception e)
            {
                Console.Console.Instance.WriteLine(e.Message);
            }

            Console.Console.Instance.WriteLine(gen.GlobalPrinter.ToString());

            return(pargen = gen.CreateExtendedShiftReduceParserInstance());
        }
Beispiel #7
0
        /// <summary>
        ///
        /// </summary>
        static void ProcessTest(string[] args)
        {
            switch (args[0].ToInt32())
            {
            case 1:
                var c2c = new CALtoCS();
                c2c.Compile(File.ReadAllLines("script/danbooru-pages.srcal"));
                break;

            case 2:
                var cal = new ESRCAL();
                cal.Compile(new[] { "5+4-(-4*(2-4)*2)/3+-(-(2*2+3)-2)*(3+1)" });
                //cal.Compile(new[] { "(2*2+3)" });
                break;

            case 3:
                var x = new HTMLParser();

                break;

            case 4:
                var p = JSParserGenerator.Parser;

                break;

            case 5:
                var s = JSScannerGenerator.Scanner;
                break;

            case 6:

                var sg = new ScannerGenerator();

                sg.PushRule("", @"[\t\r\n ]");
                sg.PushRule("comma", "\\,");
                sg.PushRule("v_pair", "\\:");
                sg.PushRule("object_starts", "\\{");
                sg.PushRule("object_ends", "\\}");
                sg.PushRule("array_starts", "\\[");
                sg.PushRule("array_ends", "\\]");
                sg.PushRule("v_true", "true");
                sg.PushRule("v_false", "false");
                sg.PushRule("v_null", "null");

                var vv = new List <StringBuilder>(5);
                vv.Add(new StringBuilder());     // 6
                vv.Add(new StringBuilder());     // 5
                vv.Add(new StringBuilder());     // 4
                vv.Add(new StringBuilder());     // 3
                vv.Add(new StringBuilder());     // 2

                for (int cc = 0xc0; cc < 0xff; cc++)
                {
                    if ((cc & 0xfc) == 0xfc)
                    {
                        vv[0].Append("\\x" + cc.ToString("X2"));
                    }
                    else if ((cc & 0xf8) == 0xf8)
                    {
                        vv[1].Append("\\x" + cc.ToString("X2"));
                    }
                    else if ((cc & 0xf0) == 0xf0)
                    {
                        vv[2].Append("\\x" + cc.ToString("X2"));
                    }
                    else if ((cc & 0xe0) == 0xe0)
                    {
                        vv[3].Append("\\x" + cc.ToString("X2"));
                    }
                    else if ((cc & 0xc0) == 0xc0)
                    {
                        vv[4].Append("\\x" + cc.ToString("X2"));
                    }
                }

                var bb = new StringBuilder();
                for (int i = 0, j = 5; i < 5; i++, j--)
                {
                    bb.Append("[" + vv[i].ToString() + "]" + new string('.', j));

                    if (i != 4)
                    {
                        bb.Append("|");
                    }
                }

                //SimpleRegex sr = new SimpleRegex();
                //sr.MakeNFA($"\"({bb.ToString()}|[^\\\"]|\\\")*\"");
                //sr.OptimizeNFA();
                //sr.NFAtoDFA();
                //sr.MinimizeDFA();
                //Console.Instance.WriteLine(sr.PrintDiagram());

                //sg.PushRule("v_string", $"\"({bb.ToString()}|[^\\\"]|\\\")*\"");
                sg.PushRule("v_string", $"\"([^\\\"]|\\\")*\"");
                sg.PushRule("v_number", @"-?[0-9]+(\.[0-9]+)?([Ee][\+\-]?[0-9]+)?");

                sg.Generate();
                Console.Instance.WriteLine(sg.PrintDiagram());

                var s3 = sg.CreateScannerInstance();
                Console.Instance.WriteLine(s3.ToCSCode("json_lexer"));

                var gen = new ParserGenerator();

                var JSON     = gen.CreateNewProduction("JSON", false);
                var ARRAY    = gen.CreateNewProduction("ARRAY", false);
                var OBJECT   = gen.CreateNewProduction("OBJECT", false);
                var MEMBERS  = gen.CreateNewProduction("MEMBERS", false);
                var PAIR     = gen.CreateNewProduction("PAIR", false);
                var ELEMENTS = gen.CreateNewProduction("ELEMENTS", false);
                var VALUE    = gen.CreateNewProduction("VALUE", false);

                var object_starts = gen.CreateNewProduction("object_starts");
                var object_ends   = gen.CreateNewProduction("object_ends");
                var comma         = gen.CreateNewProduction("comma");
                var v_pair        = gen.CreateNewProduction("v_pair");
                var array_starts  = gen.CreateNewProduction("array_starts");
                var array_ends    = gen.CreateNewProduction("array_ends");
                var v_true        = gen.CreateNewProduction("v_true");
                var v_false       = gen.CreateNewProduction("v_false");
                var v_null        = gen.CreateNewProduction("v_null");
                var v_string      = gen.CreateNewProduction("v_string");
                var v_number      = gen.CreateNewProduction("v_number");

                JSON     |= OBJECT;
                JSON     |= ARRAY;
                OBJECT   |= object_starts + object_ends;
                OBJECT   |= object_starts + MEMBERS + object_ends;
                MEMBERS  |= PAIR;
                MEMBERS  |= PAIR + comma + MEMBERS;
                PAIR     |= v_string + v_pair + VALUE;
                ARRAY    |= array_starts + array_ends;
                ARRAY    |= array_starts + ELEMENTS + array_ends;
                ELEMENTS |= VALUE;
                ELEMENTS |= VALUE + comma + ELEMENTS;
                VALUE    |= v_string;
                VALUE    |= v_number;
                VALUE    |= OBJECT;
                VALUE    |= ARRAY;
                VALUE    |= v_true;
                VALUE    |= v_false;
                VALUE    |= v_null;

                gen.PushStarts(JSON);
                gen.PrintProductionRules();
                gen.GenerateLALR2();
                gen.PrintStates();
                gen.PrintTable();

                Console.Instance.WriteLine(gen.GlobalPrinter.ToString());
                Console.Instance.WriteLine(gen.CreateExtendedShiftReduceParserInstance().ToCSCode("json_parser"));

                break;
            }
        }
Beispiel #8
0
        static void ProcessPGSample()
        {
            var gen = new ParserGenerator();

            // Non-Terminals
            var E = gen.CreateNewProduction("E", false);
            //var T = gen.CreateNewProduction("T", false);
            //var F = gen.CreateNewProduction("F", false);
            //var func = gen.CreateNewProduction("func", false);
            //var arguments = gen.CreateNewProduction("args", false);

            // Terminals
            var plus     = gen.CreateNewProduction("+");   // +
            var minus    = gen.CreateNewProduction("-");   // -
            var multiple = gen.CreateNewProduction("*");   // *
            var divide   = gen.CreateNewProduction("/");   // /
            //var id = gen.CreateNewProduction("id");             // [_$a-zA-Z][_$a-zA-Z0-9]*
            var op_open  = gen.CreateNewProduction("(");   // (
            var op_close = gen.CreateNewProduction(")");   // )
            var num      = gen.CreateNewProduction("num"); // [0-9]+

            //var split = gen.CreateNewProduction("split");       // ,

            //exp |= exp + plus + term;
            //exp |= exp + minus + term;
            //exp |= term;
            //term |= term + multiple + factor;
            //term |= term + divide + factor;
            //term |= factor;
            //factor |= op_open + exp + op_close;
            //factor |= num;
            //factor |= id;
            //factor |= func;
            //func |= id + op_open + arguments + op_close;
            //arguments |= id;
            //arguments |= arguments + split + id;
            //arguments |= ParserGenerator.EmptyString;

            E |= E + plus + E + ParserAction.Create(x => { });;
            E |= E + minus + E + ParserAction.Create(x => { });;
            E |= E + multiple + E + ParserAction.Create(x => { });;
            E |= E + divide + E + ParserAction.Create(x => { });;
            E |= minus + E + ParserAction.Create(x => { });;
            E |= op_open + E + op_close + ParserAction.Create(x => { });;
            E |= num + ParserAction.Create(x => { });;

            gen.PushConflictSolver(false, new Tuple <ParserProduction, int>(E, 4));
            gen.PushConflictSolver(true, multiple, divide);
            gen.PushConflictSolver(true, plus, minus);

            gen.PushStarts(E);
            gen.PrintProductionRules();
            gen.GenerateLALR2();
            gen.PrintStates();
            gen.PrintTable();

            Console.Instance.WriteLine(gen.GlobalPrinter.ToString());
            Console.Instance.WriteLine(gen.CreateShiftReduceParserInstance().ToCSCode("Calculator"));

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

            //var scanner_gen = new ScannerGenerator();
            //
            //scanner_gen.PushRule("", @"[\r\n ]");  // Skip characters
            //scanner_gen.PushRule("+", @"\+");
            //scanner_gen.PushRule("-", @"\-");
            //scanner_gen.PushRule("*", @"\*");
            //scanner_gen.PushRule("/", @"\/");
            //scanner_gen.PushRule("(", @"\(");
            //scanner_gen.PushRule(")", @"\)");
            //scanner_gen.PushRule("num", @"[0-9]+(\.[0-9]+)?([Ee][\+\-]?[0-9]+)?");
            //scanner_gen.Generate();
            //var ss = scanner_gen.CreateScannerInstance();
            //var pp = gen.CreateShiftReduceParserInstance();
            //
            //Action<string, string, int, int> insert = (string x, string y, int a, int b) =>
            //{
            //    pp.Insert(x, y);
            //    if (pp.Error()) throw new Exception($"[COMPILER] Parser error! L:{a}, C:{b}");
            //    while (pp.Reduce())
            //    {
            //        var l = pp.LatestReduce();
            //        Console.Instance.Write(l.Production.PadLeft(8) + " => ");
            //        Console.Instance.WriteLine(string.Join(" ", l.Childs.Select(z => z.Production)));
            //        Console.Instance.Write(l.Production.PadLeft(8) + " => ");
            //        Console.Instance.WriteLine(string.Join(" ", l.Childs.Select(z => z.Contents)));
            //        pp.Insert(x, y);
            //        if (pp.Error()) throw new Exception($"[COMPILER] Parser error! L:{a}, C:{b}");
            //    }
            //};
            //
            //try
            //{
            //    int ll = 0;
            //    var line = "5-(4+2*3-1)/(6+-5)";
            //        ss.AllocateTarget(line.Trim());
            //
            //        while (ss.Valid())
            //        {
            //            var tk = ss.Next();
            //            if (ss.Error())
            //                throw new Exception("[COMPILER] Tokenize error! '" + tk + "'");
            //            insert(tk.Item1, tk.Item2, ll, tk.Item4);
            //        }
            //
            //    if (pp.Error()) throw new Exception();
            //    insert("$", "$", -1, -1);
            //
            //    var tree = pp.Tree;
            //    CALtoCS.PrintTree(tree.root, "", true);
            //}
            //catch (Exception e)
            //{
            //    Console.Instance.WriteLine(e.Message);
            //}
        }
Beispiel #9
0
        /// <summary>
        /// SRCAL의 파서제너레이터를 생성합니다.
        /// </summary>
        /// <returns></returns>
        private ShiftReduceParser get_pargen()
        {
            if (pargen != null)
            {
                return(pargen);
            }

            var gen = new ParserGenerator();

            // Non-Terminals
            var script   = gen.CreateNewProduction("script", false);
            var line     = gen.CreateNewProduction("line", false);
            var lines    = gen.CreateNewProduction("lines", false);
            var expr     = gen.CreateNewProduction("expr", false);
            var block    = gen.CreateNewProduction("block", false);
            var iblock   = gen.CreateNewProduction("iblock", false);
            var index    = gen.CreateNewProduction("index", false);
            var variable = gen.CreateNewProduction("variable", false);
            var argument = gen.CreateNewProduction("argument", false);
            var function = gen.CreateNewProduction("function", false);
            var runnable = gen.CreateNewProduction("runnable", false);

            // Terminals
            var name     = gen.CreateNewProduction("name");
            var _const   = gen.CreateNewProduction("const"); // number | string
            var loop     = gen.CreateNewProduction("loop");
            var op_open  = gen.CreateNewProduction("op_open");
            var op_close = gen.CreateNewProduction("op_close");
            var pp_open  = gen.CreateNewProduction("pp_open");  // [
            var pp_close = gen.CreateNewProduction("pp_close"); // ]
            var equal    = gen.CreateNewProduction("equal");
            var to       = gen.CreateNewProduction("to");
            var scolon   = gen.CreateNewProduction("scolon");
            var comma    = gen.CreateNewProduction("comma");
            var _foreach = gen.CreateNewProduction("foreach");
            var _if      = gen.CreateNewProduction("if");
            var _else    = gen.CreateNewProduction("else");

            script |= lines + ParserAction.Create(x => { });
            script |= ParserGenerator.EmptyString + ParserAction.Create(x => { });

            block |= pp_open + iblock + pp_close + ParserAction.Create(x => { });
            block |= line + ParserAction.Create(x => { });

            iblock |= block + ParserAction.Create(x => { });
            iblock |= lines + ParserAction.Create(x => { });
            iblock |= ParserGenerator.EmptyString + ParserAction.Create(x => { });

            line |= expr + ParserAction.Create(x => { });

            lines |= expr + ParserAction.Create(x => { });
            lines |= expr + lines + ParserAction.Create(x => { });

            expr |= function + ParserAction.Create(x => { });
            expr |= name + equal + index + ParserAction.Create(x => { });
            expr |= runnable + ParserAction.Create(x => { });

            function |= name + op_open + op_close + ParserAction.Create(x => { });
            function |= name + op_open + argument + op_close + ParserAction.Create(x => { });

            argument |= index + ParserAction.Create(x => { });
            argument |= index + comma + argument + ParserAction.Create(x => { });

            index |= variable + ParserAction.Create(x => { });
            index |= variable + pp_open + variable + pp_close + ParserAction.Create(x => { });

            variable |= name + ParserAction.Create(x => { });
            variable |= function + ParserAction.Create(x => { });
            variable |= _const + ParserAction.Create(x => { });

            runnable |= loop + op_open + name + equal + index + to + index + op_close + block + ParserAction.Create(x => { });
            runnable |= _foreach + op_open + name + scolon + index + op_close + block + ParserAction.Create(x => { });
            runnable |= _if + op_open + index + op_close + block + ParserAction.Create(x => { });
            runnable |= _if + op_open + index + op_close + block + _else + block + ParserAction.Create(x => { });

            gen.PushConflictSolver(true, _else);
            gen.PushConflictSolver(true, new Tuple <ParserProduction, int>(runnable, 2));

            try
            {
                gen.PushStarts(script);
                gen.PrintProductionRules();
                gen.GenerateLALR();
                gen.PrintStates();
                gen.PrintTable();
            }
            catch (Exception e)
            {
                Console.Console.Instance.WriteLine(e.Message);
            }

            Console.Console.Instance.WriteLine(gen.GlobalPrinter.ToString());

            return(pargen = gen.CreateShiftReduceParserInstance());
        }