Analysis of user input according to search criteria

I am looking for a way to parse user input. The input should show which searches should be performed and how to combine them.

  • 1 and 2
  • (3 AND 2) OR 1
  • (3 AND 2) OR (1 AND 4)
  • ((3 OR 4) AND 1) OR 2
  • and etc.

The first example is to combine the search results 1 and 2 in the AND mode. The second example is to combine the search results 3 and 2 in the AND mode and combine the results of this combination with the search results 1 in the OR mode. Etc.

Any ideas on how to do this?

+3
source share
5 answers

"" , / :

public interface AndOrCapable<T> {
  public T and(T anOtherResult);
  public T or(T anOtherResult);
}

- :

Result total = r2.or(r1.and(r3.or(r4))); // your fourth example

, - , .

, - /, (), , .

, !

+2

JavaCC - . , , java , .

( (3 OR 4) AND 1) OR 2

(OR (AND (OR 3 4) 1) 2)

AND/OR

+2

, ...

java, python. pyparsing, , , .

http://pyparsing.wikispaces.com/file/view/searchparser.py

293 , . , ...

+1

infix parser; . , ..

: StreamTokenizer .

+1

( , , ):

  • Condition, Condition , 2 (IE ANDCondition parent node RangeCondition EqualsCondition).

    . O (mn), m - , n - , , . , .

  • 2: (, ) , HashSet<Key> . , , , . , , .

Note: these approaches mimic the operation of an SQL database — if your system is large or complex enough, you should probably investigate using the database instead of writing your own code to do the same.

+1
source

Source: https://habr.com/ru/post/1723772/


All Articles