public void AycockHorspoolAlgorithmShouldAcceptVulnerableGrammar() { var a = new TerminalLexerRule( new CharacterTerminal('a'), new TokenName("a")); ProductionExpression SPrime = "S'", S = "S", A = "A", E = "E"; SPrime.Rule = S; S.Rule = (Expr)S | (A + A + A + A); A.Rule = (Expr)"a" | E; var expression = new GrammarExpression( SPrime, new[] { SPrime, S, A, E }); var grammar = expression.ToGrammar(); var parseEngine = new ParseEngine(grammar); parseEngine.Pulse(new VerbatimToken(a.TokenName, 0, "a")); //var privateObject = new PrivateObject(parseEngine); //var chart = privateObject.GetField("_chart") as Chart; var chart = parseEngine.Chart; Assert.IsNotNull(chart); Assert.AreEqual(2, chart.Count); Assert.IsTrue(parseEngine.IsAccepted()); }
public Regex Parse(string regularExpression) { var grammar = new RegexGrammar(); var parseEngine = new ParseEngine(grammar, new ParseEngineOptions()); 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 parseTree = parseEngine.GetParseTree(); var regexVisitor = new RegexVisitor(); parseTree.Accept(regexVisitor); return(regexVisitor.Regex); }
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); }
public void EbnfParserShouldParseComplexGrammarWithRepeat() { var stringBuilder = new StringBuilder() //.AppendLine("file = ws directives ws ;") .AppendLine("file = \"1\" { \"2\" } \"1\";"); //.AppendLine("directives = directive { ows directive };") //.AppendLine("directive = \"0\" | \"1\"; "); var actual = Parse(stringBuilder.ToString()); var grammar = new EbnfGrammar(); var parseEngine = new ParseEngine(grammar, new ParseEngineOptions(false)); var parseRunner = new ParseRunner(parseEngine, stringBuilder.ToString()); while (!parseRunner.EndOfStream()) { if (!parseRunner.Read()) { throw new Exception( $"Unable to parse Ebnf. Error at position {parseRunner.Position}."); } } if (!parseEngine.IsAccepted()) { throw new Exception( $"Unable to parse Ebnf. Error at position {parseRunner.Position}"); } var parseForest = parseEngine.GetParseForestRootNode(); var visitor = new LoggingForestNodeVisitor(Console.Out); parseForest.Accept(visitor); }
public IEbnfDefinition Parse(string ebnf) { var grammar = new EbnfGrammar(); var parseEngine = new ParseEngine( grammar, new ParseEngineOptions()); 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 parseTree = parseEngine.GetParseTree(); var ebnfVisitor = new EbnfVisitor(); parseTree.Accept(ebnfVisitor); return(ebnfVisitor.Definition); }
#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 void Test_AycockHorspoolAlgorithm_That_Vulnerable_Grammar_Accepts_Input() { var a = new TerminalLexerRule( new Terminal('a'), new TokenType("a")); var grammar = new GrammarBuilder("S'") .Production("S'", r => r .Rule("S")) .Production("S", r => r .Rule("A", "A", "A", "A")) .Production("A", r => r .Rule(a) .Rule("E")) .Production("E", r => r .Lambda()) .ToGrammar(); var parseEngine = new ParseEngine(grammar); parseEngine.Pulse(new Token("a", 0, a.TokenType)); var privateObject = new PrivateObject(parseEngine); var chart = privateObject.GetField("_chart") as Chart; Assert.IsNotNull(chart); Assert.AreEqual(2, chart.Count); Assert.IsTrue(parseEngine.IsAccepted()); }
public IInternalTreeNode Parse(string source) { var grammar = MakeGrammar(); var parseEngine = new ParseEngine( grammar, new ParseEngineOptions(loggingEnabled: true)); var parseRunner = new ParseRunner(parseEngine, source); while (!parseRunner.EndOfStream()) { if (!parseRunner.Read()) { throw new Exception( $"Unable to parse expression. Error at line {parseRunner.Line+1}, column {parseRunner.Column+1}."); } Console.WriteLine("-----"); Console.ReadKey(true); } if (!parseEngine.IsAccepted()) { throw new Exception( $"expression parse not accepted. Error at line {parseRunner.Line+1}, column {parseRunner.Column+1}."); } return(parseEngine.GetParseTree()); }
public void ParseEngineCanParseNullableGrammar() { var parseEngine = new ParseEngine(new NullableGrammar()); var input = "aaaa"; var tokenType = new TokenType("a"); for (int i = 0; i < input.Length; i++) { var result = parseEngine.Pulse(new Token("a", i, tokenType)); Assert.IsTrue(result, $"Error at position {i}"); } Assert.IsTrue(parseEngine.IsAccepted(), "Parse was not accepted"); }
public void RunParse(IReadOnlyList <IToken> tokens) { foreach (var token in tokens) { if (!ParseEngine.Pulse(token)) { Assert.Fail($"Parse Failed at Position {ParseEngine.Location}"); } } if (!ParseEngine.IsAccepted()) { Assert.Fail("Parse was not accepted"); } }
public void RunParse(IReadOnlyList <IToken> tokens) { for (int i = 0; i < tokens.Count; i++) { if (!ParseEngine.Pulse(tokens[i])) { Assert.Fail($"Parse Failed at Position {ParseEngine.Location}"); } } if (!ParseEngine.IsAccepted()) { Assert.Fail($"Parse was not accepted"); } }
public void Test_Bnf_That_Parses_Large_Grammar_In_File() { var bnf = File.ReadAllText(Path.Combine(TestContext.TestDeploymentDir, "Bnf", "AnsiC.bnf")); Assert.IsFalse(string.IsNullOrEmpty(bnf)); var grammar = new BnfGrammar(); var parseEngine = new ParseEngine(grammar); var parseInterface = new ParseInterface(parseEngine, bnf); while (!parseInterface.EndOfStream()) { if (!parseInterface.Read()) Assert.Fail("Error Parsing At Position {0}", parseInterface.Position); } Assert.IsTrue(parseEngine.IsAccepted()); }
public void Test_NodeWalker_That_Walks_Simple_Regex() { var regexGrammar = new RegexGrammar(); var regexParseEngine = new ParseEngine(regexGrammar); var regexParseInterface = new ParseInterface(regexParseEngine, @"[(]\d[)]"); while (!regexParseInterface.EndOfStream()) { if (!regexParseInterface.Read()) Assert.Fail("error parsing input at position {0}", regexParseInterface.Position); } Assert.IsTrue(regexParseEngine.IsAccepted()); var nodeVisitor = new NodeVisitor(); var root = regexParseEngine.GetRoot(); root.Accept(nodeVisitor); Assert.AreEqual(34, nodeVisitor.VisitLog.Count); }
public void BnfShouldParseLargeGrammarInFile() { var bnf = File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "Bnf", "AnsiC.bnf")); Assert.IsFalse(string.IsNullOrEmpty(bnf)); var grammar = new BnfGrammar(); var parseEngine = new ParseEngine(grammar); var parseRunner = new ParseRunner(parseEngine, bnf); while (!parseRunner.EndOfStream()) { if (!parseRunner.Read()) { Assert.Fail($"Error Parsing At Position {parseRunner.Position}"); } } Assert.IsTrue(parseEngine.IsAccepted()); }
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); }
public void NodeVisitorShouldWalkSimpleRegex() { var regexGrammar = new RegexGrammar(); var regexParseEngine = new ParseEngine(regexGrammar); var regexLexer = new ParseRunner(regexParseEngine, @"[(]\d[)]"); while (!regexLexer.EndOfStream()) { if (!regexLexer.Read()) { Assert.Fail($"error parsing input at position {regexLexer.Position}"); } } Assert.IsTrue(regexParseEngine.IsAccepted()); var nodeVisitor = new LoggingNodeVisitor( new SelectFirstChildDisambiguationAlgorithm()); var root = regexParseEngine.GetParseForestRootNode(); root.Accept(nodeVisitor); Assert.AreEqual(31, nodeVisitor.VisitLog.Count); }