syntactic predicates - Upgrading from Antlr 3 to A

2019-01-20 13:31发布

问题:

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

some guidance is greatly appreciated.

Edit:

Is it as below.

    simpleSelector
    : elementName 
        (elementSubsequent)*

    | (elementSubsequent)+
    ;

回答1:

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.

Edit:

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


标签: antlr3 antlr4