Esempio n. 1
0
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("./json-parser-generator <filename> <output> [-f|--format]");
                return;
            }

            lexer = CreateJSonLexer();
#if false
            Console.WriteLine(lexer.ToCSCode("json_lexer"));
#endif
            parser = CreateJSonParser();

            var txt = File.ReadAllText(args[0]);
            var val = parse_json_file(txt);

            var build = new StringBuilder();
            if (args.Contains("--format") || args.Contains("-f"))
            {
                val.print(build, true);
            }
            else
            {
                val.print(build);
            }

            File.WriteAllText(args[1], build.ToString());

            return;
        }
Esempio n. 2
0
        private void bPGG_Click(object sender, EventArgs e)
        {
            try
            {
                var gen = ParserGenerator.GetGenerator(rtbPGNT.Text.Split(','), rtbPGT.Lines.Select(x => x.Trim()).Select(x =>
                                                                                                                          new Tuple <string, string> (x.Split(',')[0], x.Substring(x.Split(',')[0].Length + 1).Trim())).ToArray(),
                                                       rtbPGPR.Lines.Select(x => x.Trim()).ToArray(), rtbPGC.Lines.Select(x => x.Trim()).ToArray());

                rtbPGS.Clear();
                gen.GlobalPrinter.Clear();
                gen.PrintProductionRules();
                if (rbSLR.Checked == true)
                {
                    gen.Generate();
                }
                else if (rbLALR.Checked == true)
                {
                    gen.GenerateLALR();
                }
                else
                {
                    gen.GenerateLR1();
                }
                gen.PrintStates();
                gen.PrintTable();
                rtbPGS.AppendText(gen.GlobalPrinter.ToString());
                srparser = gen.CreateShiftReduceParserInstance();
            }
            catch (Exception ex)
            {
                rtbPGS.AppendText("Generate Error!\r\n" + ex.Message + "\r\n" + ex.StackTrace);
            }
        }
Esempio n. 3
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());
        }
        public static void Main(string[] args)
        {
            string modelPath  = "edu/stanford/nlp/models/srparser/englishSR.ser.gz";
            string taggerPath = "edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger";

            for (int argIndex = 0; argIndex < args.Length;)
            {
                switch (args[argIndex])
                {
                case "-tagger":
                {
                    taggerPath = args[argIndex + 1];
                    argIndex  += 2;
                    break;
                }

                case "-model":
                {
                    modelPath = args[argIndex + 1];
                    argIndex += 2;
                    break;
                }

                default:
                {
                    throw new Exception("Unknown argument " + args[argIndex]);
                }
                }
            }
            string               text      = "My dog likes to shake his stuffed chickadee toy.";
            MaxentTagger         tagger    = new MaxentTagger(taggerPath);
            ShiftReduceParser    model     = ((ShiftReduceParser)ShiftReduceParser.LoadModel(modelPath));
            DocumentPreprocessor tokenizer = new DocumentPreprocessor(new StringReader(text));

            foreach (IList <IHasWord> sentence in tokenizer)
            {
                IList <TaggedWord> tagged = tagger.TagSentence(sentence);
                Tree tree = model.Apply(tagged);
                log.Info(tree);
            }
        }
Esempio n. 5
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"));
        }
Esempio n. 6
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());
        }
Esempio n. 7
0
        private void bPGG_Click(object sender, EventArgs e)
        {
            try
            {
                var gen           = new ParserGenerator.ParserGenerator();
                var non_terminals = new Dictionary <string, ParserProduction>();
                var terminals     = new Dictionary <string, ParserProduction>();

                terminals.Add("''", ParserGenerator.ParserGenerator.EmptyString);

                foreach (var nt in rtbPGNT.Text.Split(','))
                {
                    non_terminals.Add(nt.Trim(), gen.CreateNewProduction(nt.Trim(), false));
                }

                foreach (var t in rtbPGT.Lines)
                {
                    if (t.Trim() == "")
                    {
                        continue;
                    }

                    var name = t.Split(',')[0];
                    var pp   = t.Substring(name.Length + 1).Trim();

                    terminals.Add(pp, gen.CreateNewProduction(name.Trim()));
                }

                var prec = new Dictionary <string, List <Tuple <ParserProduction, int> > >();

                foreach (var pp in rtbPGPR.Lines)
                {
                    if (pp.Trim() == "")
                    {
                        continue;
                    }

                    var split = pp.Split(new[] { "->" }, StringSplitOptions.None);
                    var left  = split[0].Trim();
                    var right = split[1].Split(' ');

                    var  prlist    = new List <ParserProduction>();
                    bool stay_prec = false;
                    foreach (var ntt in right)
                    {
                        if (string.IsNullOrEmpty(ntt))
                        {
                            continue;
                        }
                        if (ntt == "%prec")
                        {
                            stay_prec = true; continue;
                        }
                        if (stay_prec)
                        {
                            if (!prec.ContainsKey(ntt))
                            {
                                prec.Add(ntt, new List <Tuple <ParserProduction, int> >());
                            }
                            prec[ntt].Add(new Tuple <ParserProduction, int>(non_terminals[left], non_terminals[left].sub_productions.Count));
                            continue;
                        }
                        if (non_terminals.ContainsKey(ntt))
                        {
                            prlist.Add(non_terminals[ntt]);
                        }
                        else if (terminals.ContainsKey(ntt))
                        {
                            prlist.Add(terminals[ntt]);
                        }
                        else
                        {
                            rtbPGS.Text = $"Production rule build error!\r\n{ntt} is neither non-terminal nor terminal!\r\nDeclare the token-name!";
                            return;
                        }
                    }
                    non_terminals[left].sub_productions.Add(prlist);
                }

                for (int i = rtbPGC.Lines.Length - 1; i >= 0; i--)
                {
                    var line = rtbPGC.Lines[i].Trim();
                    if (line == "")
                    {
                        continue;
                    }
                    var tt = line.Split(' ')[0];
                    var rr = line.Substring(tt.Length).Trim().Split(',');

                    var left   = true;
                    var items1 = new List <Tuple <ParserProduction, int> >();
                    var items2 = new List <ParserProduction>();

                    if (tt == "%right")
                    {
                        left = false;
                    }

                    foreach (var ii in rr.Select(x => x.Trim()))
                    {
                        if (string.IsNullOrEmpty(ii))
                        {
                            continue;
                        }
                        if (terminals.ContainsKey(ii))
                        {
                            items2.Add(terminals[ii]);
                        }
                        else if (prec.ContainsKey(ii))
                        {
                            items1.AddRange(prec[ii]);
                        }
                        else
                        {
                            rtbPGS.Text = $"Conflict rule applying error!\r\n{ii} is neither terminal nor %prec!\r\nDeclare the token-name!";
                            return;
                        }
                    }

                    if (items1.Count > 0)
                    {
                        gen.PushConflictSolver(left, items1.ToArray());
                    }
                    else
                    {
                        gen.PushConflictSolver(left, items2.ToArray());
                    }
                }

                rtbPGS.Clear();
                gen.GlobalPrinter.Clear();
                gen.PushStarts(non_terminals[rtbPGNT.Text.Split(',')[0].Trim()]);
                if (rbSLR.Checked == true)
                {
                    gen.Generate();
                }
                else if (rbLALR.Checked == true)
                {
                    gen.GenerateLALR();
                }
                else
                {
                    gen.GenerateLR1();
                }
                gen.PrintStates();
                gen.PrintTable();
                rtbPGS.AppendText(gen.GlobalPrinter.ToString());
                srparser = gen.CreateShiftReduceParserInstance();
            } catch (Exception ex)
            {
                rtbPGS.AppendText("Generate Error!\r\n" + ex.Message + "\r\n" + ex.StackTrace);
            }
        }
Esempio n. 8
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());
        }