Пример #1
0
        /// <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());
        }
Пример #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());
        }
Пример #3
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());
        }
Пример #4
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.GenerateLALR();
                var end = sw.ElapsedMilliseconds;
                sw.Stop();
                Console.Console.Instance.WriteLine($"{end.ToString("#,#")}");
                gen.PrintStates();
                gen.PrintTable();
            }
            catch (Exception e)
            {
                Console.Console.Instance.WriteLine(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"));
        }
Пример #5
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());
        }