语法谓词 - 从Antlr 3升级到Antlr 4

时间:2013-08-25 16:29:11

标签: antlr3 antlr4

我有句法断言我必须转换成Antlr 4.语法不是写我的,所以我不知道如何以有意义的方式转换它们。这些是我必须转换的语法的主要变体。

1

    simpleSelector
    : elementName 
        ((esPred)=>elementSubsequent)*

    | ((esPred)=>elementSubsequent)+
    ;

    esPred
    : HASH | DOT | LBRACKET | COLON
    ;

    elementSubsequent
    : HASH
    | cssClass
    | attrib
    | pseudo
    ;

2

        fragment    EMS         :;  // 'em'
        fragment    EXS         :;  // 'ex'
        fragment    LENGTH      :;  // 'px'. 'cm', 'mm', 'in'. 'pt', 'pc'
        fragment    ANGLE       :;  // 'deg', 'rad', 'grad'
        fragment    TIME        :;  // 'ms', 's'
        fragment    FREQ        :;  // 'khz', 'hz'
        fragment    DIMENSION   :;  // nnn'Somethingnotyetinvented'
        fragment    PERCENTAGE  :;  // '%'

        NUMBER
            :(
              '0'..'9' ('.' '0'..'9'+)?
            | '.' '0'..'9'+
            )
            (
              (E (M|X))=>
                E
                (
                      M     { $type = EMS;          } //action in lexer rule 'NUMBER' must be last element of single outermost alt  

                    | X     { $type = EXS;          }
                )
            | (P(X|T|C))=>
                P
                (
                      X     
                    | T
                    | C
                )
                            { $type = LENGTH;       }   
            | (C M)=>
                C M         { $type = LENGTH;       }
            | (M (M|S))=> 
                M
                (
                      M     { $type = LENGTH;       }

                    | S     { $type = TIME;         }
                )
            | (I N)=>
                I N         { $type = LENGTH;       }

            | (D E G)=>
                D E G       { $type = ANGLE;        }
            | (R A D)=>
                R A D       { $type = ANGLE;        }

            | (S)=>S        { $type = TIME;         }

            | (K? H Z)=>
                K? H    Z   { $type = FREQ;         }

            | IDENT         { $type = DIMENSION;    }

            | '%'           { $type = PERCENTAGE;   }

            | // Just a number
        )
    ;

3. 

    URI :   U R L
        '('
            ((WS)=>WS)? (URL|STRING) WS?
        ')'
    ;

非常感谢一些指导。

修改

如下所示。

    simpleSelector
    : elementName 
        (elementSubsequent)*

    | (elementSubsequent)+
    ;

1 个答案:

答案 0 :(得分:4)

句法谓词仅用于解决ANTLR 3中不存在的ANTLR 3中的预测弱点。您可以在转换到ANTLR 4期间删除它们。

修改

ANTLR 3中的句法谓词具有以下形式:

(stuff) =>

无论您在语法中看到哪个表单,只需将其删除即可。以下是删除谓词后第二个示例的样子。

    NUMBER
    :(
      '0'..'9' ('.' '0'..'9'+)?
    | '.' '0'..'9'+
    )
    (
        E
        (
              M     { $type = EMS;          }
            | X     { $type = EXS;          }
        )
    |   P
        (
              X     
            | T
            | C
        )
                    { $type = LENGTH;       }   
    |   C M         { $type = LENGTH;       }
    |   M
        (
              M     { $type = LENGTH;       }

            | S     { $type = TIME;         }
        )
    |   I N         { $type = LENGTH;       }

    |   D E G       { $type = ANGLE;        }
    |   R A D       { $type = ANGLE;        }

    |   S        { $type = TIME;         }

    |   K? H    Z   { $type = FREQ;         }

    | IDENT         { $type = DIMENSION;    }

    | '%'           { $type = PERCENTAGE;   }

    | // Just a number
)
;