So I am making a parser, but the program doesn't parse functions that have tuples as arguments. For example, when I use the dist
function defined as below:
def dist(p, q):
"""Returns the Euclidean distance between two points p and q, each given as a sequence (or iterable) of coordinates. The two points must have the same dimension."""
if not isinstance(p, tuple):
p = p,
if not isinstance(q, tuple):
q = q,
if not p or not q:
raise TypeError
if len(p)!=len(q):
raise ValueError
return math.sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))
Here is the result:
>> evaluate("dist(5, 2)")
3
>> evaluate("dist((5, 2), (3, 4))")
SyntaxError: Expected end of text, found '(' (at char 4), (line:1, col:5)
How can I modify the parser to accept tuple function arguments, so that evaluate("dist((5, 2), (3, 4))")
returns 2.8284271247461903
?
Here is the answer to this and all future "how do I add Feature X to my parser?" questions:
If this problem is too challenging for you, then you have more learning to do than to just copy-paste code from Google. StackOverflow is for answering specific questions, not broad questions that are actually the topic of semester courses in CS.