我目前正在尝试使用tatsu语法解析库和包含逻辑语句的字符串来基于我的语法生成抽象语法树:
简单的例子:
test = 'OR(functionA("valueA", "valueB", "valueC"),functionB("valueA", "valueB", "valueC"))'
哪个应该解析成这个json:
{
"OR",
"[",
"{"
"functionA": ["valueA", "valueB", "valueC"]
"}",
"{"
"functionB": ["valueA", "valueB", "valueC"]
"}"
"]"
}
我的语句还包含嵌套的AND / OR。
到目前为止我的语法:
GRAMMAR = '''
@@grammar::TEST
start = operator $ ;
operator
=
| operatorvalue'(' operand ')'
| operatorvalue'(' operand ')'
;
operatorvalue
=
| 'AND'
| 'OR'
;
operand
=
| operandvalue'(' listofvalues ')'
| operandvalue'(' listofvalues ')'
;
operandvalue
=
| operator
| 'functionA'
| 'functionB'
;
listofvalues
=
/(.*?)/
;
'''
if __name__ == '__main__':
import pprint
import json
from tatsu import parse
from tatsu.util import asjson
test = 'OR(functionA("valueA", "valueB", "valueC"),functionB("valueA", "valueB", "valueC"))'
ast = parse(GRAMMAR, test)
print('# JSON')
print(json.dumps(asjson(ast), indent=2))
print()
目前,它只是抛出:
tatsu.exceptions.FailedParse: (1:4) no available options :
有人可以帮助我完成此设置吗?
感谢和问候!