public static InStep<Tuple<CommonTokenStream, CommonTree>> PrintAst() { return new InStep<Tuple<CommonTokenStream, CommonTree>>(t => { DotTreeGenerator gen = new DotTreeGenerator(); var st = gen.ToDot(t.Item2); Console.WriteLine(st); }); }
public void TestCreateDot() { ANTLRStringStream input = new ANTLRStringStream( "3 * x" ); SimpleExpressionLexer lexer = new SimpleExpressionLexer( input ); CommonTokenStream tokens = new CommonTokenStream( lexer ); SimpleExpressionParser parser = new SimpleExpressionParser( tokens ); var result = parser.expression(); var tree = result.Tree; var adaptor = parser.TreeAdaptor; DotTreeGenerator gen = new DotTreeGenerator(); string output = gen.ToDot( tree, adaptor ); string newline = Environment.NewLine; string expected = @"digraph {" + newline + newline + @" ordering=out;" + newline + @" ranksep=.4;" + newline + @" bgcolor=""lightgrey""; node [shape=box, fixedsize=false, fontsize=12, fontname=""Helvetica-bold"", fontcolor=""blue""" + newline + @" width=.25, height=.25, color=""black"", fillcolor=""white"", style=""filled, solid, bold""];" + newline + @" edge [arrowsize=.5, color=""black"", style=""bold""]" + newline + newline + @" n0 [label=""""];" + newline + @" n1 [label=""*""];" + newline + @" n1 [label=""*""];" + newline + @" n2 [label=""3""];" + newline + @" n3 [label=""x""];" + newline + @" n4 [label=""""];" + newline + newline + @" n0 -> n1 // """" -> ""*""" + newline + @" n1 -> n2 // ""*"" -> ""3""" + newline + @" n1 -> n3 // ""*"" -> ""x""" + newline + @" n0 -> n4 // """" -> """"" + newline + newline + @"}" + newline + @""; Assert.AreEqual( expected, output ); }
static void Do(bool dot) { var input = new ANTLRStringStream( @" char c; int x; int foo(int y, char d) { int i; for (i=0; i!=3; i=i+1) { x=3; y=5; } }"); var lexer = new CMinusLexer(input); var tokens = new CommonTokenStream(lexer); var parser = new CMinusParser(tokens); var r = parser.program(); var t = (CommonTree)r.Tree; if (dot) { DotTreeGenerator gen = new DotTreeGenerator(); var st = gen.ToDot(t); Console.WriteLine(st); } else { Console.WriteLine(t.ToStringTree()); } var nodes = new CommonTreeNodeStream(t); nodes.TokenStream = tokens; var walker = new CMinusWalker(nodes); walker.program(); }
internal void WriteToDotFile(CommonTree tree, string fileName) { var gen = new DotTreeGenerator(); string st = gen.ToDot(tree); string path = Path.Combine(@"C:\dat\repository\personal\dev\projects\loblang\test-documents\output-dot-files", fileName); File.WriteAllText(path, st); }
public string Compile(ICharStream input) { try { LSLTreeAdaptor lslAdaptor = new LSLTreeAdaptor(); LSLLexer lex = new LSLLexer(input); CommonTokenStream tokens = new CommonTokenStream(lex); LSLParser p = new LSLParser(tokens); p.TreeAdaptor = lslAdaptor; p.TraceDestination = _traceDestination; lex.TraceDestination = _traceDestination; LSLParser.prog_return r = p.prog(); if (p.NumberOfSyntaxErrors > 0) { if (_listener != null) { _listener.Error(Convert.ToString(p.NumberOfSyntaxErrors) + " syntax error(s)"); } return null; } CommonTree t = (CommonTree)r.Tree; CommonTreeNodeStream nodes = new CommonTreeNodeStream(lslAdaptor, t); nodes.TokenStream = tokens; SymbolTable symtab = new SymbolTable(tokens, Defaults.SystemMethods.Values, DefaultConstants.Constants.Values); if (this.Listener != null) { symtab.StatusListener = this.Listener; } Def def = new Def(nodes, symtab); def.TraceDestination = _traceDestination; def.Downup(t); CommonTreeNodeStream nodes2 = new CommonTreeNodeStream(lslAdaptor, t); nodes2.TokenStream = tokens; Types types = new Types(nodes2, symtab); types.TraceDestination = _traceDestination; types.Downup(t); if (_listener != null) { if (_listener.HasErrors()) return null; } CommonTreeNodeStream nodes4 = new CommonTreeNodeStream(lslAdaptor, t); nodes4.TokenStream = tokens; CommonTreeNodeStream nodes3 = new CommonTreeNodeStream(lslAdaptor, t); nodes3.TokenStream = tokens; TextReader fr = new StreamReader("ByteCode.stg"); StringTemplateGroup templates = new StringTemplateGroup(fr); fr.Close(); DotTreeGenerator dotgen = new DotTreeGenerator(); string dot = dotgen.ToDot(t); TextWriter tw = new StreamWriter("ast.txt"); tw.WriteLine(dot); tw.Close(); Analyze analyze = new Analyze(nodes4, symtab); types.TraceDestination = _traceDestination; analyze.Downup(t); foreach (FunctionBranch b in analyze.FunctionBranches.Where(pred => pred.Type != null)) { if (!b.AllCodePathsReturn()) { if (_listener != null) { _listener.Error("line: " + b.Node.Line + ":" + b.Node.CharPositionInLine + " " + b.Node.Text + "(): Not all control paths return a value"); } } } Gen g = new Gen(nodes3, symtab); g.TemplateGroup = templates; g.TraceDestination = _traceDestination; StringTemplate template = g.script().Template; if (template != null) { string bcOut = template.ToString(); Console.WriteLine("** byte code **\n" + bcOut); return bcOut; } } catch (TooManyErrorsException e) { if (_listener != null) { _listener.Error(String.Format("Too many errors {0}", e.InnerException.Message)); } } catch (RecognitionException e) { if (_listener != null) { _listener.Error("line: " + e.Line.ToString() + ":" + e.CharPositionInLine.ToString() + " " + e.Message); } } catch (Exception e) { if (_listener != null) { _listener.Error(e.Message); } } return null; }
public VM.CompiledScript Compile(ICharStream input) { try { LSLTreeAdaptor lslAdaptor = new LSLTreeAdaptor(); // // Initial parse and AST creation // LSLLexer lex = new LSLLexer(input); CommonTokenStream tokens = new CommonTokenStream(lex); LSLParser p = new LSLParser(tokens); p.TreeAdaptor = lslAdaptor; p.TraceDestination = _traceRedirect; lex.TraceDestination = _traceRedirect; LSLParser.prog_return r = p.prog(); if (p.NumberOfSyntaxErrors > 0) { _listener.Error(Convert.ToString(p.NumberOfSyntaxErrors) + " syntax error(s)"); return null; } // // Definitions // CommonTree t = (CommonTree)r.Tree; CommonTreeNodeStream nodes = new CommonTreeNodeStream(lslAdaptor, t); nodes.TokenStream = tokens; SymbolTable symtab = new SymbolTable(tokens, Defaults.SystemMethods.Values, DefaultConstants.Constants.Values); symtab.StatusListener = _listener; Def def = new Def(nodes, symtab); def.TraceDestination = _traceRedirect; def.Downup(t); nodes.Reset(); if (_listener.HasErrors() || def.NumberOfSyntaxErrors > 0) { return null; } // // Type and more semantic checks // Compiler.Types types = new Compiler.Types(nodes, symtab); types.TraceDestination = _traceRedirect; types.Downup(t); nodes.Reset(); if (_listener.HasErrors() || types.NumberOfSyntaxErrors > 0) { return null; } StringTemplateGroup templates; using (TextReader fr = new StreamReader(Path.Combine(_templatePath, "ByteCode.stg"))) { templates = new StringTemplateGroup(fr); fr.Close(); } if (_outputAstGraph) { DotTreeGenerator dotgen = new DotTreeGenerator(); string dot = dotgen.ToDot(t); TextWriter tw = new StreamWriter("ast.txt"); tw.WriteLine(dot); tw.Close(); } Analyze analyze = new Analyze(nodes, symtab); analyze.TraceDestination = _traceRedirect; analyze.Downup(t); nodes.Reset(); foreach (Compiler.BranchAnalyze.FunctionBranch b in analyze.FunctionBranches.Where(pred => pred.Type != null)) { if (!b.AllCodePathsReturn()) { if (_listener != null) { _listener.Error("line: " + b.Node.Line + ":" + b.Node.CharPositionInLine + " " + b.Node.Text + "(): Not all control paths return a value"); } } } if (_listener.HasErrors() || analyze.NumberOfSyntaxErrors > 0) { return null; } // // Bytecode generation // Gen g = new Gen(nodes, symtab); g.TemplateGroup = templates; g.TraceDestination = _traceRedirect; Gen.script_return ret = g.script(); if (_listener.HasErrors() || g.NumberOfSyntaxErrors > 0) { return null; } if (ret.Template == null) { return null; } StringTemplate template = ret.Template; if (_byteCodeDebugging) { _byteCode = template.ToString(); } // // Bytecode compilation // AssemblerLexer alex = new AssemblerLexer(new ANTLRStringStream(template.ToString())); CommonTokenStream atokens = new CommonTokenStream(alex); AssemblerParser ap = new AssemblerParser(atokens); BytecodeGenerator bcgen = new BytecodeGenerator(Defaults.SystemMethods.Values); ap.SetGenerator(bcgen); ap.TraceDestination = _traceRedirect; try { ap.program(); if (_listener.HasErrors() || p.NumberOfSyntaxErrors > 0) { _listener.Error(Convert.ToString(ap.NumberOfSyntaxErrors) + " bytecode generation error(s)"); return null; } return bcgen.Result; } catch (GenerationException e) { _listener.Error(e.Message); } return null; } catch (TooManyErrorsException e) { _listener.Error(String.Format("Too many errors {0}", e.InnerException.Message)); } catch (RecognitionException e) { _listener.Error("line: " + e.Line.ToString() + ":" + e.CharPositionInLine.ToString() + " " + e.Message); } catch (Exception e) { _listener.Error(e.Message); } return null; }
private void CreateASTGraph(CommonTree commonTree) { if (string.IsNullOrEmpty(Config.DotPath)) return; var dotpath = string.Format("{0}\\dot.exe", Config.DotPath); if (!Directory.Exists(Config.DotPath)) { throw new Exception("Caminho da ferramenta dot.exe inválido."); } var gen = new DotTreeGenerator(); var dotgraph = gen.ToDot(commonTree); dotgraph = new DotGraphStyle { FontSize = AstView.FontSize }.Apply(dotgraph); var writer = new StreamWriter("graph.dot"); writer.Write(dotgraph); writer.Close(); if (File.Exists("graph.png")) File.Delete("graph.png"); var psi = new ProcessStartInfo(dotpath, "-Tpng graph.dot -o graph.png"); var p = new Process { StartInfo = psi }; p.Start(); p.WaitForExit(); AstView.LoadAstImage("graph.png"); }