句法谓词 - 从 Antlr 3 升级到 Antlr 4 [英] syntactic predicates - Upgrading from Antlr 3 to Antlr 4

查看:39
本文介绍了句法谓词 - 从 Antlr 3 升级到 Antlr 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have syntactic predicated that I have to convert into the Antlr 4. The grammar is not written my me so I have no idea how to convert them in a meaningful way. These are the main variations of the grammar that I have to convert.

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)+
    ;

推荐答案

句法谓词仅用于解决 ANTLR 3 中在 ANTLR 4 中不存在的预测弱点.您可以在过渡到 ANTLR 4 期间简单地删除它们.

Syntactic predicates were only used to work around a prediction weakness in ANTLR 3 that is not present in ANTLR 4. You can simply remove them during your transition to ANTLR 4.

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

A syntactic predicate in ANTLR 3 had the following form:

(stuff) =>

无论您在语法中的何处看到该形式,只需将其删除即可.这是删除谓词后的第二个示例的样子.

Wherever you see that form in your grammar, just remove it. Here's what your second example looks like with the predicates removed.

    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
)
;

这篇关于句法谓词 - 从 Antlr 3 升级到 Antlr 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆