From e355cd6cbf2b62be98b1206d33d79073cadf1174 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Sat, 29 Jun 2024 08:46:13 -0400 Subject: [PATCH] nitcc: add to_dot_lr0 that draws a lr0 automaton without much annontation Signed-off-by: Jean Privat --- contrib/nitcc/src/grammar.nit | 33 +++++++++++++++++++++++++++++++++ contrib/nitcc/src/nitcc.nit | 3 ++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/contrib/nitcc/src/grammar.nit b/contrib/nitcc/src/grammar.nit index 3484d0a505..0ddb646c73 100644 --- a/contrib/nitcc/src/grammar.nit +++ b/contrib/nitcc/src/grammar.nit @@ -589,6 +589,39 @@ class LRAutomaton return res.join end + # Generate a graphviz file of the automaton + # This generate a simple executable LR0 without much information + fun to_dot_lr0(path: String) + do + var f = new FileWriter.open(path) + f.write("digraph \{\n") + f.write("rankdir=TB;\n") + f.write("node[shape=box,style=rounded];\n") + + f.write("entry [style=invis];\nentry -> s{states.first.number}\n") + for s in states do + f.write "s{s.number} [label=\"" + for a in s.reduces do + if a.prod.accept then + f.write "ACCEPT\\n" + else + f.write "REDUCE {a.prod.name.escape_to_dot} WITH {a.elems.length} ELEMENTS\\n" + end + end + if s.shifts.length > 0 then + f.write "SHIFT\\n" + end + f.write "\"" + if not s.is_lr0 then f.write ",color=red" + f.write "];\n" + for t in s.outs do + f.write "s{s.number} -> s{t.to.number} [label=\"{t.elem.to_s.escape_to_dot}\"];\n" + end + end + f.write("\}\n") + f.close + end + # Generate a graphviz file of the automaton fun to_dot(path: String) do diff --git a/contrib/nitcc/src/nitcc.nit b/contrib/nitcc/src/nitcc.nit index a54044bac3..aa2e98abb2 100644 --- a/contrib/nitcc/src/nitcc.nit +++ b/contrib/nitcc/src/nitcc.nit @@ -99,8 +99,9 @@ f.write "// Concrete grammar of {name}\n" f.write pretty f.close -print "LR automaton: {lr.states.length} states (see {name}.lr.dot and {name}.lr.out)" +print "LR automaton: {lr.states.length} states (see {name}.lr.dot, {name}.lr0.dot and {name}.lr.out)" lr.to_dot("{name}.lr.dot") +lr.to_dot_lr0("{name}.lr0.dot") pretty = lr.pretty f = new FileWriter.open("{name}.lr.out") f.write "// LR automaton of {name}\n"