Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Consider the following conversion from String to Mathematica Expression:

n = 10;
ToExpression[Table["{", {n}] <> Table["}", {n}]]

Everything is fine.

However, when the string is longer:

n = 510;
ToExpression[Table["{", {n}] <> Table["}", {n}]]
ToExpression::sntx: Invalid syntax in or before "                                                                               


My actual string that I'm trying to convert is much longer, about 2 million characters. How can I convert it to a Mathematica expression? A short example is:

"{"GAME", {"version", "0.23.5"}, {"linkURL", ""}, {"Mode", "1"}, {"PARAMETERS", {"FLIGHT", {"CanQuickSave", "True"}, {"CanQuickLoad", "True"}}}, {"SCENARIO", {"name", "ScenarioDiscoverableObjects"}, {"scene", "7, 8, 5"}, {"", "703987854"}, {"sizeCurve", {"key", "0 0 1.5 1.5"}, {"key", "0.3 0.45 0.875 0.875"}}}}"

UPDATE Reading a Stream also fails:

n = 510;
string = Table["{", {n}] <> Table["}", {n}];
stream = StringToStream[string]
Read[stream, Expression]
Read::readt: Invalid input found when reading {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{\[Ellipsis]
 from StringToStream[{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{\[Ellipsis]
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}]. >>
share|improve this question
fyi, tried it on V10 at WPC, and it failed there at 351 not 182. Not sure if this makes any difference. !Mathematica graphics – Nasser Jul 4 '14 at 23:40
Not true for any string, though. n = 510; ToExpression[Table["1+1+", {n}] <> Table["1+1", {n}]] – Dr. belisarius Jul 4 '14 at 23:51
@shrx How do you create your data? I suppose you do this in Mathematica as well? If you do create your data in Mathematica, I might have a workaround. – halirutan Jul 5 '14 at 0:08
I can reproduce this with V.9.0.1 on OS X. It looks like a bug to me. The string of braces is built with no problem. It's ToString that's stumbling. I've found another place where Mathematica has trouble with deeply nested lists. See this question. I would say it's a bug. – m_goldberg Jul 5 '14 at 3:45
@halirutan see my previous question. You can see the original structure of the file I'm trying to parse. – shrx Jul 5 '14 at 8:46

(All observations made in version 7.)

There seems to be a limitation for input even in the Front End (Notebook interface), in that if I enter more than 766 levels of nested lists I get a MaxFormatDepthExceeded expression and an error beep. The help text is:

A box structure with a depth exceeding the maximum allowed depth was encountered.

We can at least get this 766 level input by using the function UndocumentedTestFEParserPacket shown by John Fultz here, which I will bundle as:

parseString[s_String, prep : (True | False) : True] := 
  FrontEndExecute @ FrontEnd`UndocumentedTestFEParserPacket[s, prep]

We can then do:

n = 766;
string = Table["{", {n}] <> Table["}", {n}];
expr = ToExpression @@ parseString[string];
ToString[expr] === string

To extend this further would apparently require avoiding an intermediate Box structure which is an interesting problem. However it's the Fourth of July and I'll be spending my evening doing something else! :-)

Update: Using version 10, and increasing $RecursionLimit as Michael commented, we can use the method above for up to n = 5514:

$RecursionLimit = 1*^6;

n = 5514;

string = Table["{", {n}] <> Table["}", {n}];
expr = ToExpression @@ parseString[string];
ToString[expr] === string

However a higher value crashes Mathematica without warning. (Version under Windows.)

share|improve this answer
FYI: This seems to work up to n = 2941 if I Block or set $RecursionLimit = 2942 or more in V9.0.1. – Michael E2 Jul 5 '14 at 2:23

What you experience here seems to be some kind of stack limit when you have nested expressions. It doesn't seem to matter whether you nest lists or function calls. Look for instance at this example here which is nothing more than a nested call f[f[f[...f[a]]..]

Mathematica graphics

On the other hand, if the parser doesn't need to build up such a large stack, it seems to work. In an example of a+a+...+a which you can find here the Get works like a charm:

Mathematica graphics

Please note that it doesn't seem to make a difference whether you use Get or Import on a file, or ToExpression or ImportString on strings. The error stays the same here.

I'm not sure what I can propose you as workaround. Have you noticed that when you copy&paste the nested expressions directly, it works? It really seem to be a limitation of the reading from a stream.

share|improve this answer

I guess my solution maybe pretty easy,but it can works:

 n = 5;
 ToExpression[Table["{", {n}] <> Table["}", {n}]] == Nest[{#} &, {}, n - 1]


And if I set n a large number:

Nest[{#} &, {}, n];


Also works.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.