Skip to content
Snippets Groups Projects
Commit 9c320949 authored by René Schöne's avatar René Schöne
Browse files

First version for generation of graphviz representation of the grammar

parent 7ff40d14
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,8 @@ larceny-bin
**/*.scm~
*.class
*.out
*.dot
*.dot.pdf
# Test and measurment directories, files and output
test
......
......@@ -6,6 +6,7 @@ import properties, utils, install, sockets
import ilp_test as test
import ilp_measurement as measure
import ilp_check as check
import mkdot
import os
from fabric.api import local, task, get, hosts, run
......
mkdot.py 0 → 100644
from utils import local_quiet
try:
from fabric.api import task
except ImportError:
from fabric_workaround import task
header = 'digraph G {\nnode [shape = "record"]\n'
footer = '\n}'
@task(default=True)
def convert(f, output=None):
if output is None:
output = f+".dot"
with open(f) as fd:
lines = fd.readlines()
with open(output, 'w') as fd:
fd.write(header)
for line in [l.strip() for l in lines if l.strip().startswith('(ast-rule')]:
rule = line[line.index('(ast-rule \'')+11:line.rindex(')')]
# inheritance-arrows are "arrowhead = empty"
print rule
lhand, rhand = rule.split('->')
superclass = lhand[lhand.index(':')+1:] if ':' in lhand else None
if superclass:
lhand = lhand[:lhand.index(':')]
children = rhand.split('-') if rhand.strip() else []
terminals = filter(lambda x: x.islower(), children)
nonterminals = filter(lambda x: not x.islower(), children)
# define the node of the left hand side
fd.write('{0} [label = "{{{0} | {1}}}"]\n'.format(lhand, '|'.join(terminals)))
# add inheritance
if superclass:
fd.write('{0} -> {1} [arrowhead = "empty"]\n'.format(lhand, superclass))
# add nonterminal children
for child in nonterminals:
print ' child:', child
options = ''
# handle contexts
if '<' in child:
child, context = child.split('<')
options = 'label = "{0}"'.format(context)
# handle lists
if child.endswith('*'):
child = child[:-1]
if options:
# it had a context
options = 'headlabel = "{0}", label = "{1}"'.format('*', context)
else:
options = 'headlabel = "*"'
fd.write('{0} -> {1}[{2}]\n'.format(lhand, child, options))
fd.write(footer)
local_quiet('dot {0} -Tpdf > {0}.pdf'.format(output))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment