Is there an easy way to perform a partial parse with python ply?
In other words: instead of parsing the entire source at once, is it possible to parse up to
the end of an expression, yield
its result and hand back over control?
For instance, the following toy grammar produces the intended output.
import ply.lex as lex
import ply.yacc as yacc
data = '''\
a sentence. another
sentence.
'''
tokens = ('WORD', 'DOT')
t_WORD = r'\w+'
t_DOT = r'\.'
t_ignore = ' \t\n'
lexer = lex.lex()
def p_text(p):
'''text : text sentence DOT
| sentence DOT'''
p[0] = '\n'.join(p[1:-1])
def p_sentence(p):
'''sentence : sentence WORD
| WORD'''
p[0] = ' '.join(p[1:])
parser = yacc.yacc()
print(parser.parse(data)) # parses all sentences at once!
However, how can one consume sentence per sentence (as with a generator) and not all sentences at once?
# intended behaviour: does not work!
for sent in parser.partial_parse(data):
do_something_with(sent)