0

将以下点文件作为输入传递会dot2tex导致解析异常。如果我删除“,2”,错误就会消失。

  digraph G {
    node [shape="circle"];
    1,2 [style="filled"];
    1 -> { 2; 3; 4 }
    2 -> { 5; 6 }
    4 -> { 7; 8 }
    5 -> { 9; 10 }
    7 -> { 11; 12 }
  }

我过去曾在 Ubuntu 下看到过​​它的工作原理。目前我正在使用 cygwin64,Python 2.7.10 和 dot2tex 2.9.0。我不是 Python 专家,但似乎从源代码安装 dot2tex,使用python setup.py install,也安装了 PyParsing 的一个版本。dot2tex使用开关运行后--debug,似乎dot2tex.log我现在有了 PyParsing 的 2.0.7 版本:

File "/usr/lib/python2.7/site-packages/pyparsing-2.0.7-py2.7.egg/pyparsing.py", line 1129, in parseString
    raise exc
ParseException: Expected "}" (at char 46), (line:3, col:6)

问题出在哪里?

4

1 回答 1

1

我猜dot2tex中的pyparsing语法不支持这种形式:

1,2 [style="filled"];

这行得通吗?

1 [style="filled"];
2 [style="filled"];

编辑:

您可以在自动生成的 .dot 文件上尝试使用此转换器,然后再将其提供给 dot2tex:

from pyparsing import *

sample = """\
  digraph G {
    node [shape="circle"];
    1,2 [style="filled"];
    3,4 [size = 27.1];
    1 -> { 2; 3; 4 }
    2 -> { 5; 6 }
    4 -> { 7; 8 }
    5 -> { 9; 10 }
    7 -> { 11; 12 }
  }"""

# just guessing on these, they are probably close
node_id = Word(alphanums+"_")
attr_id = Word(alphas, alphanums+"_")
attr_val = quotedString | pyparsing_common.fnumber
attr_spec = attr_id + '=' + attr_val

multiple_node_attr = (Group(delimitedList(node_id))("nodes")
                      + originalTextFor("[" + delimitedList(attr_spec) + "]" + ";")("attrs"))
# ignore definitions that have only one node, no need to mess with them
multiple_node_attr.addCondition(lambda t: len(t.nodes) > 1)

# define parse-time callback to expand node lists to separate node definitions
def expand_multiple_node_specs(s,l,tokens):
    indent = (col(l,s)-1)*" "
    join_str = '\n' + indent
    return join_str.join("{} {}".format(nd, tokens.attrs) for nd in tokens.nodes)
multiple_node_attr.addParseAction(expand_multiple_node_specs)

# use transformString to apply changes in parse actions
modified = multiple_node_attr.transformString(sample)
print(modified)

印刷:

  digraph G {
    node [shape="circle"];
    1 [style="filled"];
    2 [style="filled"];
    3 [size = 27.1];
    4 [size = 27.1];
    1 -> { 2; 3; 4 }
    2 -> { 5; 6 }
    4 -> { 7; 8 }
    5 -> { 9; 10 }
    7 -> { 11; 12 }
  }
于 2016-08-12T15:25:39.927 回答