public Debug(Parser parser, Action<string> infoWriter = null, Action<string> errorWriter = null) { _parser = parser; _builder = new StringBuilder(); _infoWriter = infoWriter; _errorWriter = errorWriter ?? _infoWriter; }
private static async Task MainAsync(string[] args, CancellationToken token) { // // the following program produces a parse table for the following grammar // for infix expressions, and appropriately applies operator precedence of // the + - * / operators, otherwise evaluating the leftmost operations first // // S' -> e // e -> i // e -> ( e ) // e -> e * e // e -> e / e // e -> e + e // e -> e - e // var grammar = new Grammar( new[] {"S'", "e", "+", "-", "*", "/", "i", "(", ")"}, new PrecedenceGroup(Derivation.None, //S' -> e new Production(0, (_, x) => x[0], 1), //e -> i new Production(1, (_, x) => x.Length == 1 ? x[0] : null, 6), //e -> ( e ) new Production(1, (_, x) => x[1].ContentType == ContentType.Nested ? x[1].Nested : null, 7, 1, 8) ), new PrecedenceGroup(Derivation.LeftMost, //e -> e * e new Production(1, RewriteConstBinaryExpr, 1, 4, 1), //e -> e / e new Production(1, RewriteConstBinaryExpr, 1, 5, 1) ), // productions are left associative and bind less tightly than * or / new PrecedenceGroup(Derivation.LeftMost, //e -> e + e new Production(1, RewriteConstBinaryExpr, 1, 2, 1), //e -> e - e new Production(1, RewriteConstBinaryExpr, 1, 3, 1) ) ); // generate the parse table var parser = new Parser(grammar); var debugger = new Debug(parser, Console.Write, Console.Error.Write); debugger.DumpParseTable(); debugger.Flush(); var parseTime = System.Diagnostics.Stopwatch.StartNew(); #if DEBUG // (24 / 12) + 2 * (3-4) var inputSource = new[] { new Token(6, 2), new Token(2, "+"), new Token(7, "("), new Token(6, 0), new Token(4, "*"), new Token(6, int.MaxValue), new Token(8, ")") }; #else var inputSource = TestLarge(); #endif Token result; using (var tokenIterator = new AsyncLATokenIterator(inputSource.AsAsync())) { result = await parser.ParseInputAsync(tokenIterator, debugger, allowRewriting: true); } parseTime.Stop(); var timeElapsed = string.Format("{0} ms", parseTime.Elapsed.TotalMilliseconds); debugger.WriteFinalToken( string.Format("Accept ({0}): ", timeElapsed), string.Format("Error while parsing ({0}): ", timeElapsed), result); }