Andrea Censi
2016-07-02 23:16:41 UTC
Hi,
I have been using PyParsing for some major projects so far, but I still
don't understand how to properly parse some specific types of recursive
grammars, for which the function operatorPrecedence does not help.
In particular, take Python's syntax for indexing using square brackets.
These are some examples:
array[0]
array[0][1]
array[0][1][2]
I understand why the following solution does not work:
value = Forward()
value_ref = Word(alphas)
value_index = value + "[" + index + "]"
value_ ... = ...
....
value << (value_ref ^ value_index ^ value_... ^ ...)
(This is a very simplified version of what I am dealing with, in which I
have about a dozen possible expressions for value).
I understand why something like the above gives an infinite recursion
exception. The problem is clear. Yet I don't see the solution.
Another example that appears also in python is dealing with attributes:
object
object.attribute
object.attribute.attribute2
(expression ...).attribute
I suspect that this is a general problem that people have.
Any hints?
thanks,
A.
I have been using PyParsing for some major projects so far, but I still
don't understand how to properly parse some specific types of recursive
grammars, for which the function operatorPrecedence does not help.
In particular, take Python's syntax for indexing using square brackets.
These are some examples:
array[0]
array[0][1]
array[0][1][2]
I understand why the following solution does not work:
value = Forward()
value_ref = Word(alphas)
value_index = value + "[" + index + "]"
value_ ... = ...
....
value << (value_ref ^ value_index ^ value_... ^ ...)
(This is a very simplified version of what I am dealing with, in which I
have about a dozen possible expressions for value).
I understand why something like the above gives an infinite recursion
exception. The problem is clear. Yet I don't see the solution.
Another example that appears also in python is dealing with attributes:
object
object.attribute
object.attribute.attribute2
(expression ...).attribute
I suspect that this is a general problem that people have.
Any hints?
thanks,
A.