public Regex Parse(string regularExpression) { var grammar = new RegexGrammar(); var parseEngine = new ParseEngine(grammar, new ParseEngineOptions(optimizeRightRecursion: true)); var parseRunner = new ParseRunner(parseEngine, regularExpression); while (!parseRunner.EndOfStream()) { if (!parseRunner.Read()) { throw new Exception( $"Unable to parse regular expression. Error at position {parseRunner.Position}."); } } if (!parseEngine.IsAccepted()) { throw new Exception( $"Error parsing regular expression. Error at position {parseRunner.Position}"); } var parseForest = parseEngine.GetParseForestRootNode(); var parseTree = new InternalTreeNode( parseForest as IInternalForestNode, new SelectFirstChildDisambiguationAlgorithm()); var regexVisitor = new RegexVisitor(); parseTree.Accept(regexVisitor); return(regexVisitor.Regex); }
#pragma warning disable CC0091 // Use static method public EbnfDefinition Parse(string ebnf) { var grammar = new EbnfGrammar(); var parseEngine = new ParseEngine( grammar, new ParseEngineOptions( optimizeRightRecursion: true, loggingEnabled: false)); var parseRunner = new ParseRunner(parseEngine, ebnf); while (!parseRunner.EndOfStream()) { if (!parseRunner.Read()) { throw new Exception( $"Unable to parse Ebnf. Error at line {parseRunner.Line}, column {parseRunner.Column}."); } } if (!parseEngine.IsAccepted()) { throw new Exception( $"Ebnf parse not accepted. Error at line {parseRunner.Line}, column {parseRunner.Column}."); } var parseForest = parseEngine.GetParseForestRootNode(); var parseTree = new InternalTreeNode( parseForest as IInternalForestNode, new SelectFirstChildDisambiguationAlgorithm()); var ebnfVisitor = new EbnfVisitor(); parseTree.Accept(ebnfVisitor); return(ebnfVisitor.Definition); }
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 ParseEngineShouldDisambiguateFollowingOperatorPresidence() { var input = "2*3+5*7"; var parseTester = new ParseTester(new ExpressionGrammar()); parseTester.RunParse(input); var forest = parseTester.ParseEngine.GetParseForestRootNode(); var tree = new InternalTreeNode(forest); // ((2*3)+(5*7)) // (E, 0, 7) = (E, 0, 5) ('*', 5, 6) (E, 6, 7) // (E, 0, 5) = (E, 0, 3) ('+', 3, 4) (E, 4, 5) // (E, 0, 3) = (E, 0, 1) ('*', 1, 2) (E, 2, 3) // (E, 0, 1) = ('2', 0, 1) // (E, 2, 3) = ('3', 2, 3) // (E, 4, 5) = ('5', 4, 5) // (E, 6, 7) = ('7', 6, 7) Assert.Inconclusive(); }
private static InternalTreeNode GetTreeNode(Grammar grammar, string input) { var parseEngine = new ParseEngine(grammar); var parseRunner = new ParseRunner(parseEngine, input); while (!parseRunner.EndOfStream()) { Assert.IsTrue(parseRunner.Read()); } Assert.IsTrue(parseEngine.IsAccepted()); var parseForest = parseEngine.GetParseForestRootNode(); Assert.IsNotNull(parseForest); var internalNode = parseForest; var treeNode = new InternalTreeNode(internalNode); return(treeNode); }
private static InternalTreeNode GetTreeNode(IGrammar grammar, string input) { var parseEngine = new ParseEngine(grammar); var parseRunner = new ParseRunner(parseEngine, input); while (!parseRunner.EndOfStream()) { Assert.IsTrue(parseRunner.Read()); } Assert.IsTrue(parseEngine.IsAccepted()); var parseForest = parseEngine.GetParseForestRootNode(); Assert.IsTrue(parseForest is IInternalForestNode); var internalNode = parseForest as IInternalForestNode; var disambiguationAlgorithm = new SelectFirstChildDisambiguationAlgorithm(); var treeNode = new InternalTreeNode(internalNode, disambiguationAlgorithm); return(treeNode); }