How does this ANTLR 3 convert to ANTLR 4?

559 views
Skip to first unread message

Bill Nicholson

unread,
May 29, 2017, 11:05:57 AM5/29/17
to antlr-discussion

This is ANTLR 3 grammar. How does it convert to ANTLR 4? The carat is a problem?


text_string_no_linebreak:
    text_string -> ^(STRING_NO_LINEBREAK_TOKEN text_string)
;
 

no viable alternative at character '^'



Here's another one that gives the same error:


create_index: // For external use only. Don't reference this in the normal grammar.
    CREATE_SYMBOL^ create_index_tail SEMICOLON_SYMBOL? EOF
;

Mike Lischke

unread,
May 29, 2017, 11:59:00 AM5/29/17
to antlr-di...@googlegroups.com
This is ANTLR 3 grammar. How does it convert to ANTLR 4? The carat is a problem?


text_string_no_linebreak:
    text_string -> ^(STRING_NO_LINEBREAK_TOKEN text_string)
;
Remove all tree rewrite code (^ and everything after ->) in all rules. This is no longer supported.


Bill Nicholson

unread,
May 29, 2017, 4:14:02 PM5/29/17
to antlr-discussion
so this:
        | {!SQL_MODE_ACTIVE(SQL_MODE_NO_BACKSLASH_ESCAPES)}? => ESCAPE_OPERATOR .  { escape_count++; }


becomes this?

        | {!SQL_MODE_ACTIVE(SQL_MODE_NO_BACKSLASH_ESCAPES)}

Mike Lischke

unread,
May 30, 2017, 3:54:54 AM5/30/17
to antlr-di...@googlegroups.com
so this:
        | {!SQL_MODE_ACTIVE(SQL_MODE_NO_BACKSLASH_ESCAPES)}? => ESCAPE_OPERATOR .  { escape_count++; }


becomes this?

        | {!SQL_MODE_ACTIVE(SQL_MODE_NO_BACKSLASH_ESCAPES)}


No, this is not tree rewriting code but a predicate. Predicates are still supported (but there is no separation anymore between semantic and syntactic predicates). Simply remove the fat arrow (=>) and it should be accepted again.

Btw, you can remove the escape count handling actually and simply the rule. While it is useful to know if there was an escape sequence, it doesn't much improve speed since this is only relevant when you retrieve a quoted string from the parse tree (which should process escape sequences). Instead just use:

DOUBLE_QUOTED_TEXT: (
DOUBLE_QUOTE (
DOUBLE_QUOTE DOUBLE_QUOTE
| {!isSqlModeActive(NoBackslashEscapes)}? (ESCAPE_SEQUENCE | ~[\\"])
| {isSqlModeActive(NoBackslashEscapes)}? ~["]
)*?
DOUBLE_QUOTE
);
fragment ESCAPE_SEQUENCE: '\\' .; // Valid chars: 0'"bnrtZ\%_;

Similarly for the other quote rules.


Reply all
Reply to author
Forward
0 new messages