syntactic predicates - Upgrading from Antlr 3 to Antlr 4

后端 未结 1 1789
梦如初夏
梦如初夏 2020-12-11 22:26

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 th

相关标签:
1条回答
  • 2020-12-11 22:53

    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
    )
    ;
    
    0 讨论(0)
提交回复
热议问题