Beispiel #1
0
            private Grammar MakeGrammar()
            {
                var parser = new EbnfParser();
                var source = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "Expression.nug"));

                var definition = parser.Parse(source);

                var grammar = new EbnfGrammarGenerator().Generate(definition);

                return(grammar);
            }
    public static void Main(string[] args)
    {
        string grammarText = LoadFromResource(nameof(DefaultNamespaceName), "Grammar", "syntax5.ebnf");
        //string input = File.ReadAllText("/etc/apache2/httpd.conf", Encoding.UTF8);
        string input = "1 1";

        var definition  = new EbnfParser().Parse(grammarText);
        var grammar     = new EbnfGrammarGenerator().Generate(definition);
        var parseEngine = new ParseEngine(grammar);
        var parseRunner = new ParseRunner(parseEngine, input);

        var recognized    = false;
        var errorPosition = 0;

        while (!parseRunner.EndOfStream())
        {
            recognized = parseRunner.Read();
            if (!recognized)
            {
                errorPosition = parseRunner.Position;
                break;
            }
        }

        var accepted = false;

        if (recognized)
        {
            accepted = parseRunner.ParseEngine.IsAccepted();
            if (!accepted)
            {
                errorPosition = parseRunner.Position;
            }
        }
        Console.WriteLine($"Recognized: {recognized}, Accepted: {accepted}");
        if (!recognized || !accepted)
        {
            Console.Error.WriteLine($"Error at position {errorPosition}");
        }

        // get the parse forest root from the parse engine
        var parseForestRoot = parseEngine.GetParseForestRootNode();

        // create a internal tree node and supply the disambiguation algorithm for tree traversal.
        var parseTree = new InternalTreeNode(
            parseForestRoot,
            new SelectFirstChildDisambiguationAlgorithm());

        Console.WriteLine(parseTree.ToString());
    }
        public void TestProtcolBuffersV3Grammar()
        {
            var ebnfPath      = Path.Combine(TestContext.TestDeploymentDir, "Runtime", GrammarFile);
            var ebnf          = File.ReadAllText(ebnfPath);
            var ebnfGenerator = new EbnfGrammarGenerator();
            var ebnfParser    = new EbnfParser();
            var ebnfDefintion = ebnfParser.Parse(ebnf);

            var parseEngine = new ParseEngine(ebnfGenerator.Generate(ebnfDefintion));

            var inputPath   = Path.Combine(TestContext.TestDeploymentDir, "Runtime", ProtoFile);
            var input       = File.ReadAllText(inputPath);
            var parseRunner = new ParseRunner(parseEngine, input);

            while (!parseRunner.EndOfStream())
            {
                Assert.IsTrue(parseRunner.Read());
            }
            Assert.IsTrue(parseRunner.ParseEngine.IsAccepted());
        }
        private static IGrammar GenerateGrammar(EbnfDefinition definition)
        {
            var generator = new EbnfGrammarGenerator();

            return(generator.Generate(definition));
        }