A function is in the shape of
(operator arg1 arg2 ... argn)
is the operation symbol, parameter 1, parameter 2, up to parameter n. The parameter itself can also be a function in this format.
For example, a string like this
String="(add (add 1 2) (mul 2 1) 2 )"
We need to split its operands and parameters, that is, split it into
["add","(add 1 2)","(mul 2 1)","2"]
How should such a character array be divided?
My current approach is to remove the outermost brackets every time, and then want to use spaces to split the string, but the spaces in the middle will also become the place to be split. If you use regular expressions, since each parameter may still have nested parentheses inside, how should this situation be matched?
Prefix notation
,S-expression
,Lisp expression
lisp's
S-expression
is a multi-layer nested tree structure, which is closer toAbstract Syntax Tree (AST)
.Regular is difficult to parse
S-expression
without recursive grammar.The following is a simple example in python. I have commented it and it should be easy to understand.
S-expression
Regular:
Note that this regex has a Global parameter
If there is only one layer of nested
(op arg...)
inarg1, arg2, arg3, ... argn
, you can use this method