1

我正在尝试使用 treetop 编写一个解析器来将一些乳胶命令解析为 HTML 标记。通过以下内容,我在生成的代码中得到了一个死角。我已经构建了源代码tt并逐步完成,但它并没有真正阐明根本问题是什么(它只是在_nt_paragraph中旋转)

Test input: "\emph{hey} and some more text."

grammar Latex
  rule document
    (paragraph)* {
      def content
        [:document, elements.map { |e| e.content }]
      end
    }
  end

  # Example: There aren't the \emph{droids you're looking for} \n\n. 
  rule paragraph
    ( text / tag )* eop {
      def content
        [:paragraph, elements.map { |e| e.content } ]
      end
    }
  end

  rule text
    ( !( tag_start / eop) . )* {
      def content
        [:text, text_value ]
      end
    }
  end

  # Example: \tag{inner_text}
  rule tag
    "\\emph{" inner_text '}' {
      def content
        [:tag, inner_text.content]
      end
    }
  end 

  # Example: \emph{inner_text}
  rule inner_text
    ( !'}' . )* {
      def content
        [:inner_text, text_value]
      end
    }
  end

  # End of paragraph.
  rule eop
    newline 2.. {
      def content
        [:newline, text_value]
      end
    }
  end

  rule newline
    "\n"
  end

  # You know, what starts a tag
  rule tag_start
    "\\"
  end

end
4

1 回答 1

0

对于任何好奇的人,树顶开发谷歌小组的克利福德(Clifford)发现了这一点。

问题在于段落和文本。

文本是 0 个或多个字符,并且一个段落中可以有 0 个或多个文本,所以发生的情况是在第一个 \n 之前有无限数量的 0 长度字符,导致解析器死机。修复方法是将文本调整为:

( !( tag_start / eop) . )+

所以它必须至少有一个字符才能匹配。

于 2013-01-23T22:23:40.703 回答