The complete grammar for expressing Boolean queries, in a BNF-like format, is included in this topic.
The following sample code expresses Boolean queries, in a BNF-like format:
orexpr: andexpr ; | andexpr OR orexpr ; andexpr: parenexpr ; | parenexpr andexpr ; | parenexpr AND andexpr ; | parenexpr andnotexpr ; andnotexpr: AND NOT orexpr ; | NOT orexpr ; parenexpr: LPAREN orexpr RPAREN ; | terms ; terms: word_or_phrase KEY_RESTRICT keyexpr ; | word_or_phrase NEAR/NUM word_or_phrase ; | word_or_phrase ONEAR/NUM word_or_phrase ; | multiple_word_or_phrase ; multiple_word_or_phrase: word_or_phrase ; | word_or_phrase multiple_word_or_phrase ; keyexpr: LPAREN nr_orexpr RPAREN ; | word_or_phrase ; nr_orexpr: nr_andexpr ; | nr_andexpr OR nr_orexpr ; nr_andexpr: nr_parenexpr ; | nr_parenexpr nr_andexpr ; | nr_parenexpr AND nr_andexpr ; | nr_parenexpr nr_andnotexpr ; nr_andnotexpr: AND NOT nr_orexpr ; | NOT nr_orexpr ; nr_notexpr: nr_parenexpr ; | NOT nr_parenexpr ; nr_parenexpr: LPAREN nr_orexpr RPAREN ; | nr_terms ; nr_terms: multiple_word_or_phrase ; word_or_phrase: word ; | phrase ; AND: '[Aa]' '[Nn]' '[Dd]' ; OR: '[Oo]' '[Rr]' ; NOT: '[Nn]' '[Oo]' '[Tt]' ; NEAR: '[Nn]' '[Ee]' '[Aa]' '[Rr]' ; ONEAR: '[Oo]' '[Nn]' '[Ee]' '[Aa]' '[Rr]' ; NUM: '[0-9] ; | NUM NUM ; LPAREN: '(' ; RPAREN: ')' ; KEY_RESTRICT: ':' ;