how to skip "and" with skip rule?

827 Views Asked by At

I'm working on a new antlr grammar which is similar to nattys and should recognize date expressions, but I have problem with skip rules. In more detail I want to ignore useless "and"s in expressions for example:

Call Sam, John and Adam and fix a meeting with Sarah about the finance on Monday and Friday.

The first two "and"s are useless. I wrote the rule bellow to fix this problem but it didn't work, why? what should I do?

    NW : [~WeekDay];
    UselessAnd : AND NW -> skip;
1

There are 1 best solutions below

1
On

"Useless AND" is a semantic concept.

Grammars are about syntax, and handle semantic issues poorly. Don't couple these together.

Suggestion: when you write a grammar for a language, make your parser accept the language as it is, warts and all. In your case, I suggest you "collect" the useless ANDs. That way you can get the grammar "right" more easily, and more transparently to the next coder who has to maintain your grammar.

Once you have the AST, it is pretty easy to ignore (semantically) useless things; if nothing else, you can post-process the AST and remove the useless AND nodes.