来自PyParsing的ParseException通过dot2tex

时间:2016-01-11 18:43:18

标签: python cygwin dot pyparsing dot2tex

将以下点文件作为输入传递给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专家,但似乎使用python setup.py install从源安装dot2tex,也安装了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)

问题出在哪里?

1 个答案:

答案 0 :(得分: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 }
  }