antlr html pcdata
我正在尝试使用 ANTLR 编写非常简单的 HTML 解析器,但我面临着问题,即 ~ 规则应该匹配所有内容,直到指定的字符不起作用。
我的词法分析器语法:
lexer grammar HtmlParserLexer;
HTML: OHTML PCDATA CHTML;
PCDATA :(~'<') ; //match all until <
OHTML: '<html>';
CHTML: '</html>';
我试图匹配:
<html>foo bar</html>
来自 Eclipse ANTLR 插件解释器的错误:
MismatchedTokenException: line 1:7 mismatched input UNKNOW expecting '<'
这意味着我的语法忽略了 PCDATA 规则,我不知道为什么。 预先感谢您的帮助。
Im trying to write very simple HTML parser with ANTLR and Im facing problem, that ~ rule which should match all until specified character is not working.
My lexer grammar:
lexer grammar HtmlParserLexer;
HTML: OHTML PCDATA CHTML;
PCDATA :(~'<') ; //match all until <
OHTML: '<html>';
CHTML: '</html>';
Im trying to match:
<html>foo bar</html>
Error from Eclipse ANTLR plugin Interpreter:
MismatchedTokenException: line 1:7 mismatched input UNKNOW expecting '<'
Which means, that my grammar ignore PCDATA rule and I dont know why.
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
规则
PCDATA :(~'<') ;
匹配'<'
之外的单个字符。您需要重复一次或多次:PCDATA :(~'<')+ ;
(注意+
)。您可能还希望允许
(
和
之间没有任何内容)。在这种情况下,您不应将
PCDATA :(~'<')+ ;
更改为PCDATA :(~'<')* ;
,但应这样做相反:因为您不应该创建可能与空字符串匹配的词法分析器规则。
The rule
PCDATA :(~'<') ;
matches a single character other than'<'
. You'll need to repeat it once or more:PCDATA :(~'<')+ ;
(notice the+
).You may also want to allow
<html></html>
(nothing in between<html>
and</html>
). In that case, you shouldn't changePCDATA :(~'<')+ ;
intoPCDATA :(~'<')* ;
, but do this instead:because you shouldn't create lexer rules that could potentially match an empty string.