당신은 자바에 관심이있을 것 같아요. (미래에 그렇게 말하는 것이 좋을 것입니다.)하지만 언어를 선택할 수 있다면 파이썬과 parsy을 사용하는 것이 좋습니다. ANTLR보다 훨씬 가볍습니다.
import attr
from parsy import string, regex, generate
@attr.s
class Variable():
name = attr.ib()
@attr.s
class Compound():
left_value = attr.ib()
right_value = attr.ib()
operator = attr.ib()
@attr.s
class Expression():
value = attr.ib()
# You could put an `evaluate` method here,
# or `generate_sql` etc.
whitespace = regex(r'\s*')
lexeme = lambda p: whitespace >> p << whitespace
AND = lexeme(string('AND'))
OR = lexeme(string('OR'))
OPERATOR = AND | OR
LPAREN = lexeme(string('('))
RPAREN = lexeme(string(')'))
variable = lexeme((AND | OR | LPAREN | RPAREN).should_fail("not AND OR ()") >> regex("\w+")).map(Variable)
@generate
def compound():
yield LPAREN
left = yield variable | compound
op = yield OPERATOR
right = yield variable | compound
yield RPAREN
return Compound(left_value=left,
right_value=right,
operator=op)
expression = (variable | compound).map(Expression)
난 단순 데이터 구조 attrs를 사용 : 여기
적절한 데이터 구조로 샘플을 구문 분석 내가 함께 노크 몇 가지 예제 코드입니다.
분석의 결과는 식의 계층 구조입니다 :
>>> expression.parse("((f1 OR f2) AND (f3 OR (f4 AND f5)))")
Expression(value=Compound(left_value=Compound(left_value=Variable(name='f1'), right_value=Variable(name='f2'), operator='OR'), right_value=Compound(left_value=Variable(name='f3'), right_value=Compound(left_value=Variable(name='f4'), right_value=Variable(name='f5'), operator='AND'), operator='OR'), operator='AND'))
ANTLR은 매우 강력한 도구입니다, 아직 구문 분석 및/또는-표현식 ANTLR을 사용하여 너무 작은 조금 (이 작동 할 수 있지만, 혜택은 적음). 또는 파서를 손으로 코딩하거나 간단한 파서를 사용할 수도 있습니다. 어쩌면 https://tomassetti.me/parsing-in-java가 도움이 될 수 있습니다. – CoronA
이 질문은 단일 Q & A 스타일을 선호하는 SO에는 적합하지 않습니다. 힌트, 팁, 참고 자료 등을 요구하고 있는데, 이는 단 하나의 대답으로는 거의 대답 할 수 없습니다. 대신 파서 작성을 고려하고 특정 문제에 대해 질문하십시오. –