I have a string with several unix statements, joined with (, ), ||, &&, |. I want to separate them out into arrays (nested?) in the order they would be evaluated in a unix command line. I preferably want them in a prefix notation, but anything would do.
like
a --foo "b|a||r" && a -b || (a || a) | c | d
should become
["|", ["|", ["||", ["&&", "a --foo \"b|a||r\"", "a -b"], ["||", "a", "a"]], "c"], "d"]
(Really tried to give an example that involves all possibilities.
How should I approach this?
I can understand code in most of the popular high level (OOP) languages (ruby, python, etc), or even a pseudocode would help.
If you think about it, the task is not different from parsing a mathematical expression, e.g.
a * (b + c) / d==>[/, [*, a, [+, b, c]], d]. Thus, you can use the same tools:You need is an infix parser.
(Infix, because your operators are between the operands: e.g.
a && b)Browse over the Wikipedia article for a bit of theoretical background, then grab an existing parser for the language you would like to use and define the operators as well as their precedence.