Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nitcc: add LRAutomaton::to_dot_lr0 #2840

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading