B
B
becks2014-10-22 14:52:27
C++ / C#
becks, 2014-10-22 14:52:27

How to properly parse a function entry and what data structures to use to store it?

There is a string containing a function, for example:
func1( 123, func2( func3( a1, a2), 234, func3( a3, a4) ) , 345)
The task is to parse the string, requesting its value from the server for each function. If the function among the parameters contains functions, then first you need to calculate their value.
Tell me what are the simple ways to parse such a string and store functions (including, if necessary, replacing\adding them with values)?
It occurred to me to parse from the end of the record, find the last function and add its value to the array of arguments.
Maybe there are already some ready-made implementations for parsing such records?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Koss1024, 2014-10-22
@Koss1024

The algorithm is known more than
https://ru.wikipedia.org/wiki/Algorithm_sorting... We
replace the example in the article with the names of functions and that's it. If we are not talking about prosini expressions additionally.
Question exactly in C++? Since the request from the server here looks a little out of place
. Everything has its own tool.

A
Armenian Radio, 2014-10-22
@gbg

The frontal solution is as follows:
0) Remove spaces and their equivalents from the string.
1) Check the correctness of the input string so that at least the brackets are correct.
2) Find the most deeply nested brackets, to the left of them before the separator (brackets or comma) is the name of the function, and they contain arguments.
3) Call the function.
4) Insert the result of step 4 in place of the function and its arguments.
5) Return to 2.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question