static void Main(string[] args) { var sampleBnf = @" <syntax> ::= <rule> | <rule> <syntax> <rule> ::= <identifier> ""::="" <expression> <line-end> <expression> ::= <list> | <list> ""|"" <expression> <line-end> ::= <EOL> | <line-end> <line-end> <list> ::= <term > | <term> <list> <term> ::= <literal > | <identifier> <identifier> ::= ""<"" <rule-name> "">"" <literal> ::= '""' <text> '""' | ""'"" <text> ""'"""; var grammar = new BnfGrammar(); for (long i = 0; i < 10000; i++) { var parseEngine = new ParseEngine(grammar); var parseInterface = new ParseInterface(parseEngine, sampleBnf); var stringReader = new StringReader(sampleBnf); while (!parseInterface.EndOfStream() && parseInterface.Read()) { } var result = parseInterface.ParseEngine.IsAccepted(); } }
public bool Parse() { var grammar = new BnfGrammar(); var parseEngine = new ParseEngine(grammar); var parseRunner = new ParseRunner(parseEngine, sampleBnf); while (!parseRunner.EndOfStream() && parseRunner.Read()) { } return parseRunner.ParseEngine.IsAccepted(); }
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_Bnf_That_Parse_Produces_Bnf_Chart() { var grammar = new BnfGrammar(); var parseEngine = new ParseEngine(grammar); var parseInterface = new ParseInterface(parseEngine, _bnfText); while (!parseInterface.EndOfStream()) { if (!parseInterface.Read()) { var position = parseInterface.Position; var startIndex = 0; for (int i = position; i >= 0; i--) { if (_bnfText[i] == '\n' && i > 0) if (_bnfText[i - 1] == '\r') { startIndex = i; break; } } var endIndex = _bnfText.IndexOf("\r\n", position); endIndex = endIndex < 0 ? _bnfText.Length : endIndex; var length = endIndex - startIndex; var stringBuilder = new StringBuilder(); stringBuilder .AppendFormat("Error parsing input string at position {0}.", parseInterface.Position) .AppendLine() .AppendFormat("start: {0}", startIndex) .AppendLine() .AppendLine(_bnfText.Substring(startIndex, length)); Assert.Fail(stringBuilder.ToString()); } } Assert.IsTrue( parseInterface.ParseEngine.IsAccepted(), "error at position {0}", parseInterface.Position); }