Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Obtain a list of syntax trees consistent with a valid pattern of parentheses
ResourceFunction["ParenthesesTrees"][str] lists valid syntax trees for reading pairs of parentheses from the input string str. |
| "()" → xi | xi=Tree[Null,{"(",")"}] | close |
| "(xj)" → xk | xk=Tree[Null,{"(",xj,")"}] | continue |
| "xmxn" → xp | xp=Tree[Null,{xm,xn}] | combine |
There is only one parenthesis tree with two leaves:
| In[1]:= |
| Out[1]= | ![]() |
The smallest input with non-unique output has three pairs of open-close parentheses:
| In[2]:= |
| Out[2]= | ![]() |
It's not too difficult to find ever-larger inputs with unique syntax trees:
| In[3]:= |
| Out[3]= | ![]() |
It's also easy to find larger inputs with many equivalent syntax trees:
| In[4]:= |
| Out[4]= | ![]() |
The Null string is uniquely interpreted as a tree consisting only of a root node:
| In[5]:= |
| Out[5]= | ![]() |
Using the option setting TreeLayout→"SerializedLeafs" causes ParenthesesTrees to use a unique layout, making leaves easier to read across:
| In[6]:= | ![]() |
| Out[6]= | ![]() |
The "SerializedLeafs" layout also works with AspectRatio:
| In[7]:= | ![]() |
| Out[7]= | ![]() |
Enumerate valid inputs up to length 14 and count them:
| In[8]:= | ![]() |
| In[9]:= |
| Out[9]= |
Also enumerate and count the number of unique syntax trees:
| In[10]:= |
| Out[10]= |
This work is licensed under a Creative Commons Attribution 4.0 International License