WEB开发网
开发学院软件开发Python 可爱的 Python: 使用 Spark 模块解析 阅读

可爱的 Python: 使用 Spark 模块解析

 2007-03-29 12:07:02 来源:WEB开发网   
核心提示: 清单 6. 删节后的 markupbuilder.py Spark 脚本class MarkupBuilder(GenericASTBuilder):"Write out HTML markup based on matched markup"def p_para(se

清单 6. 删节后的 markupbuilder.py Spark 脚本    class MarkupBuilder(GenericASTBuilder):
  "Write out HTML markup based on matched markup"
  def p_para(self, args):
    '''
    para  ::= plain
    para  ::= markup
    para  ::= para plain
    para  ::= para emph
    para  ::= para strong
    para  ::= para module
    para  ::= para code
    para  ::= para title
    plain ::= whitespace
    plain ::= alphanums
    plain ::= contraction
    plain ::= safepunct
    plain ::= mdash
    plain ::= wordpunct
    plain ::= plain plain
    emph  ::= dash plain dash
    strong ::= asterisk plain asterisk
    module ::= bracket plain bracket
    code  ::= apostrophe plain apostrophe
    title ::= underscore plain underscore
    '''
  def nonterminal(self, type_, args):
    # Flatten AST a bit by not making nodes if only one child.
    if len(args)==1: return args[0]
    if type_=='para': return nonterminal(self, type_, args)
    if type_=='plain':
      args[0].attr = foldtree(args[0])+foldtree(args[1])
      args[0].type = type_
      return nonterminal(self, type_, args[:1])
    phrase_node = AST(type_)
    phrase_node.attr = foldtree(args[1])
    return phrase_node

上一页  2 3 4 5 6 7 8 9  下一页

Tags:可爱 Python 使用

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接