public void ResolvingAmbiguities() { using (var interp = new Interpreter<AmbiguousCalculator>()) { interp.Parse("2+8/2"); Assert.AreEqual(6, interp.Context.Result.Value); interp.Parse("8/4/2"); Assert.AreEqual(1, interp.Context.Result.Value); interp.Parse("2+3"); // can be interpreted as a "2 * (+3)" Assert.AreEqual(5, interp.Context.Result.Value); // Check that implicit multiplication works interp.Parse("2 3"); Assert.AreEqual(6, interp.Context.Result.Value); // A lot of ambiguities: interp.Parse("1+-+6/3"); Assert.AreEqual(-1, interp.Context.Result.Value); #if false using (var g = new GvGraphView("expr.tmp.gv")) { interp.BuildTree("1+-+6/3").WriteGraph(g, interp.Grammar, true); } #endif } }
public void Test() { var context = new SAdBLang(); using (var interp = new Interpreter<SAdBLang>(context)) { interp.Parse("d"); Assert.IsTrue(context.IsFinalRuleCalledLast); Assert.IsTrue(context.WasMergeCalled); // with SPPF producer var sppf = interp.BuildTree("d"); using (var graph = new GvGraphView(typeof(SAdBLang).Name + "_sppf.gv")) { sppf.WriteGraph(graph, interp.Grammar, true); } } }
private static void Benchmarks( string path, string title, Type langDef, int trialCount = 1, BenchFlags flags = BenchFlags.All) { var timer = new Stopwatch(); timer.Start(); var lang = Language.Get(langDef); using (var interp = new Interpreter(lang)) { interp.Parse("a+a"); } timer.Stop(); Log("--------------- " + title + " ---------------"); Log("heatup time = {0}", timer.Elapsed); timer.Reset(); using (var testFile = new StreamReader(path)) { timer.Start(); testFile.ReadToEnd(); timer.Stop(); Log("read time = {0}", timer.Elapsed); } using (var interp = new Interpreter(lang)) { for (int trial = 0; trial != trialCount; ++trial) { timer.Reset(); using (var testFile = new StreamReader(path)) { timer.Start(); int count = interp.Scan(testFile, path).Count(); timer.Stop(); Log("scan time = {0} tokenCount = {1}", timer.Elapsed, count); } } // Recognize if ((flags & BenchFlags.Recognize) == BenchFlags.Recognize) { for (int trial = 0; trial != trialCount; ++trial) { timer.Reset(); using (var testFile = new StreamReader(path)) { timer.Start(); bool success = interp.Recognize(testFile, path); timer.Stop(); Assert.IsTrue(success); Log("parse (recognize) = {0}", timer.Elapsed); } } } // Parse actions if ((flags & BenchFlags.ParseActions) == BenchFlags.ParseActions) { for (int trial = 0; trial != trialCount; ++trial) { timer.Reset(); using (var testFile = new StreamReader(path)) { timer.Start(); bool success = interp.Parse(testFile, path); timer.Stop(); Assert.IsTrue(success); Log("parse (actions) = {0}", timer.Elapsed); } } } // Parse tree if ((flags & BenchFlags.ParseTree) == BenchFlags.ParseTree) { for (int trial = 0; trial != trialCount; ++trial) { timer.Reset(); using (var testFile = new StreamReader(path)) { timer.Start(); interp.BuildTree(testFile, path); timer.Stop(); Log("parse (tree) = {0}", timer.Elapsed); } } } } }
public void Test2Sppf() { var filePath = DataSamples.CompileSample2FilePath; using (var source = new StreamReader(filePath)) using (var interpreter = new Interpreter<ILLanguage>()) { SppfNode sppf = interpreter.BuildTree(source, filePath); using (var graph = new GvGraphView("Cil_Sample2_sppf.gv")) { var lang = Language.Get(typeof(ILLanguage)); sppf.WriteGraph(graph, lang.Grammar, true); } } }