Skip to content

Commit

Permalink
nitcc: add to_dot_lr0 that draws a lr0 automaton without much annonta…
Browse files Browse the repository at this point in the history
…tion

Signed-off-by: Jean Privat <[email protected]>
  • Loading branch information
privat committed Jun 29, 2024
1 parent 97b707e commit e355cd6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
33 changes: 33 additions & 0 deletions contrib/nitcc/src/grammar.nit
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion contrib/nitcc/src/nitcc.nit
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit e355cd6

Please sign in to comment.