【问题标题】:How to parse repeated attributes with antlr?如何用antlr解析重复的属性?
【发布时间】:2016-12-11 21:46:01
【问题描述】:

我有以下语法。

meta : '<' TAG attribute* '>';

attribute : NAME '=' VAL;

TAG : [A-Z0-9]+;

NAME : [A-Z_-]+;

VAL : '"'.*?'"';

我想匹配下面的字符串。

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">

但我收到以下错误。

ParseError extraneous input 'CONTENT' expecting {'>', NAME}  clj-antlr.common/parse-error (common.clj:146)

我能够解析一个属性。

<META HTTP-EQUIV="Content-Type">

如何解析重复的属性?给attribute* 无效。

更新:其实是词法分析器造成的。如果我将TAGNAME 结合起来,那么它可以工作。

meta : '<' NAME attribute* '>';
NAME : [A-Z0-9_-]+;

但我不想让NAME 包含数字。有没有办法让这个工作?

【问题讨论】:

    标签: antlr antlr4 ebnf


    【解决方案1】:

    你可以使用两个独立的词法规则,然后用一个解析器规则分别组合它们

    ID: [A-Za-z]+ ;
    NUMBER: [0-9]+ ;
    
    tag: ID+ tag? | NUMBER+ tag? ;
    name: ID+ name?  | ('_' | '-')+ name? 
    

    如果您在忽略元素之间的空格时遇到问题,您可以使用不同的通道,仅在上述解析器规则中启用它... 甚至可以将上述解析器规则定义为词法分析器规则,但我不确定......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      • 1970-01-01
      • 2018-08-18
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多