パーサーを完全に機能させるには、ちょっとしたトリックが必要です。ブールクエリを解析するためにantlrを使用します。
クエリは要素で構成され、ands、ors、notsでリンクされています。
だから私は次のようなものを持つことができます:
"(P or not Q or R) or (( not A and B) or C)"
つまり、要素は長くなる可能性があり、通常は次の形式になります。
a an_operator b
例えば :
"New-York matches NY"
トリック、an_operatorの1つは「好きではない」
したがって、「not like」演算子を含む要素の解析を回避するために、notがその後にlikeがないことをチェックするようにレクサーを変更したいと思います。
私の現在の文法はここにあります:
// save it in a file called Logic.g
grammar Logic;
options {
output=AST;
}
// parser/production rules start with a lower case letter
parse
: expression EOF! // omit the EOF token
;
expression
: orexp
;
orexp
: andexp ('or'^ andexp)* // make `or` the root
;
andexp
: notexp ('and'^ notexp)* // make `and` the root
;
notexp
: 'not'^ atom // make `not` the root
| atom
;
atom
: ID
| '('! expression ')'! // omit both `(` andexp `)`
;
// lexer/terminal rules start with an upper case letter
ID : ('a'..'z' | 'A'..'Z')+;
Space : (' ' | '\t' | '\r' | '\n')+ {$channel=HIDDEN;};
どんな助けでもいただければ幸いです。ありがとう !