How can the negation meta-character, ~
, be used in ANTLR's lexer- and parser rules?
相关问题
- Using Antlr to get identifiers and function names
- boolean and arithmetic expression grammar in ANTLR
- antlr 4.2.2 output to console warning (157)
- What happened to options in rules in ANTLR 4?
- ANTLR How to use lexer rules having same starting?
相关文章
- In ANTLR, how do you specify a specific number of
- Slow ANTLR4 generated Parser in Python, but fast i
- Ignore some part of input when parsing with ANTLR
- Working example of wikitext-to-HTML in ANTLR 3
- How to parse a parenthesized hierarchy root?
- Interpreting custom language
- Two basic ANTLR questions
- How to deal with list return values in ANTLR
Negating can occur inside lexer and parser rules.
Inside lexer rules you can negate characters, and inside parser rules you can negate tokens (lexer rules). But both lexer- and parser rules can only negate either single characters, or single tokens, respectively.
A couple of examples:
lexer rules
To match one or more characters except lowercase ascii letters, you can do:
(the negation-meta-char,
~
, has a higher precedence than the+
, so the rule above equals(~('a'..'z'))+
)Note that
'a'..'z'
matches a single character (and can therefor be negated), but the following rule is invalid:Because
'ab'
(obviously) matches 2 characters, it cannot be negated. To match a token that consists of 2 character, but not'ab'
, you'd have to do the following:parser rules
Inside parser rules,
~
negates a certain token, or more than one token. For example, you have the following tokens defined:If you now want to match any token except the
A
, you do:And if you want to match any token except
B
andD
, you can do:However, if you want to match any two tokens other than
A
followed byB
, you cannot do:Just as with lexer rules, you cannot negate more than a single token. To accomplish the above, you need to do:
Note that the
.
(DOT) char in a parser rules does not match any character as it does inside lexer rules. Inside parser rules, it matches any token (A
,B
,C
,D
orE
, in this case).Note that you cannot negate parser rules. The following is illegal: