I recently discovered a python module pyparsing, a great tool for analyzing data by writing a grammar, not a parser. I am new to the concept of context-free grammars, so please correct any false assumptions in this matter.
Pyparsing can implement BNF ( Backsu-Naur Form ) contextual grammar. This grammar can be recursive, but can it have a forward-looking forecast? I was interested to know about this since I came across this question . Let me give you a concrete example. Consider the line:
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 this:
<number> :: __<digit>__
<block> :: <number>(x) (<number> <number> <number> .... x times)
i.e. read the first token of a number, save it as x, and then use the following numbers xand group them together. The designed line should look like this:
[[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 wrote a simple Python MWE that does not use pyparsing, so what is 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 using a context-free BNF grammar? If yes / no, how can I do this with pyparsing?