には、 3.14.0search_query.ebnf
の次の文法定義があります。grako
@@grammar :: SearchQuery
start = search_query $;
search_query = parenthesized_query | combined_query | search_term;
parenthesized_query = '(' search_query ')';
combined_query = search_query binary_operator search_query;
binary_operator = '&' | '|';
search_term = /\w+/;
でパーサーを生成します
grako search_query.ebnf --outfile search_query_parser.py
結果は、これらの入力に対して期待どおりに機能します。
import search_query_parser
parser = search_query_parser.SearchQueryParser()
parser.parse('a') # -> 'a'
parser.parse('(a)') # -> ['(', 'a', ')']
parser.parse('a & b') # -> ['a', '&', 'b']
parser.parse('a | b') # -> ['a', '|', 'b']
parser.parse('(a|b)&c') # -> ['(', ['a', '|', 'b'], ')', '&', 'c']
しかし、演算子の右側に括弧で囲まれた式がある場合、パーサーはエラー メッセージを表示します。
parser.parse('c&(a|b)')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/das-g/.virtualenvs/tmp-d0fd5a9428f7612a/search_query_parser.py", line 82, in parse
return super(SearchQueryParser, self).parse(text, *args, **kwargs)
File "/home/das-g/.virtualenvs/tmp-d0fd5a9428f7612a/lib/python3.5/site-packages/grako/contexts.py", line 227, in parse
result = rule()
File "/home/das-g/.virtualenvs/tmp-d0fd5a9428f7612a/lib/python3.5/site-packages/grako/contexts.py", line 86, in wrapper
return self._call(rule, name, params, kwparams)
File "/home/das-g/.virtualenvs/tmp-d0fd5a9428f7612a/lib/python3.5/site-packages/grako/contexts.py", line 475, in _call
node, newpos, newstate = self._invoke_rule(rule, name, params, kwparams)
File "/home/das-g/.virtualenvs/tmp-d0fd5a9428f7612a/lib/python3.5/site-packages/grako/contexts.py", line 511, in _invoke_rule
rule(self)
File "/home/das-g/.virtualenvs/tmp-d0fd5a9428f7612a/search_query_parser.py", line 87, in _start_
self._check_eof()
File "/home/das-g/.virtualenvs/tmp-d0fd5a9428f7612a/lib/python3.5/site-packages/grako/contexts.py", line 650, in _check_eof
self._error('Expecting end of text.')
File "/home/das-g/.virtualenvs/tmp-d0fd5a9428f7612a/lib/python3.5/site-packages/grako/contexts.py", line 450, in _error
item
grako.exceptions.FailedParse: (1:2) Expecting end of text. :
c&(a|b)
^
start
私は何か間違ったことをしていますか?