ANTLR3 Dynamic quotes in lexer

2019-08-06 13:41发布

问题:

I need to match something like the Perl regexp matcher

m/my regex!*/

where the quotes can be any character from a range. So the above is the same as

m%my regex!*%

A naive guess of a lexer rule would be

REGEX: 'm' quote=. (~(quote))* quote;

but that does not work, because the latter quote is not referring to the quote= but to some rule.

I can do it with a lot of own code, like

REGEX: 'm' quote=. { ... implement the loop and final match myself ... } ;

but somehow I think there should be a canonical way to do such things.

回答1:

... but somehow I think there should be a canonical way to do such things.

There is not. You'll have to do this with custom code.



回答2:

Take a look at PL/SQL parser (here). Oracle also supports those Perl style quoted strings.

Like:

q':select * from employees where last_name = 'smith':'

Use the custom code as an example. (It contains C and Java implementation). Maybe in your case it can be even simplified.

Ivan



标签: antlr antlr3