Recently I've discovered the python module pyparsing
, a wonderful tool for parsing data by writing the grammar, not the parser. I'm new to the idea of context-free grammars, so please correct any false assumptions in this question.
Pyparsing can implement a BNF (Backus–Naur Form) context-free grammar. This grammar can be recursive, but can it have a forward look-ahead? I've been wondering about the answer to this since I stumbled across this question. Let me give you a concrete example. Consider the string:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Let the grammar look something like:
<number> :: __<digit>__
<block> :: <number>(x) (<number> <number> <number> .... x times)
i.e. read the first number token, save it as x
and then consume the next x
numbers and group them together. The parsed line should look like:
[[1, 2], [3, 4, 5, 6], [7, 8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]]
I've written a simple python MWE not using pyparsing so it's clear what I'm trying to do:
A = range(1,31)
B, sub_b = [], []
consume = 0
for a in A:
if consume:
sub_b.append(a)
consume -= 1
else:
if sub_b: B.append(sub_b)
sub_b = [a,]
consume = a
B.append(sub_b)
print B
Two (related) questions: Can this be done with a BNF context-free grammar? If yes/no how can I do it with pyparsing
?