How to find syntax error

When I evaluate a function in one cell, Mathematica says

  ToExpression :: notstrbox: {At Line = 6, the input was:, $ Failed, InString [6]} 
 is not a string or a box.  ToExpression can only interpret strings or boxes 
 as Mathematica input.  >>

And the right edge of the cell turns red.

How to find error location?

+6
source share
3 answers

In fact, the kernel always sends the position of the first syntax error to the input line in FrontEnd (if this input line contains an error). This can be demonstrated with the following MathLink code:

In[32]:= link = LinkLaunch[First[$CommandLine] <> " -mathlink"]; LinkRead[link]; LinkWrite[link, EnterTextPacket["2+"]] LinkRead[link] LinkRead[link] LinkRead[link] Out[35]= MessagePacket[Syntax, "sntxi"] Out[36]= TextPacket["Syntax::sntxi: Incomplete expression; more input is needed. "] Out[37]= SyntaxPacket[5] 

An integer in SyntaxPacket "indicates the position at which a syntax error was detected in the input string" as described in the documentation. At first it is puzzling that this position in the case of the input string “ 2+ ” is obviously outside the input string. But it seems that in fact this position is calculated for the InputForm line of the input line, which in this case is equal to: " 2+\n\n ".

We can check how this works with $SyntaxHandler , defined as follows:

 In[41]:= link = LinkLaunch[First[$CommandLine] <> " -mathlink"]; LinkRead[link] LinkWrite[link, EnterTextPacket[ "$SyntaxHandler= Function[{str,pos}, Print["Input string: ",ToString[str,InputForm]]; Print["Position of syntax error: ",pos]; $Failed ]; "]] LinkRead[link] LinkWrite[link, EnterTextPacket["2+"]] While[Head[packet = LinkRead[link]] =!= InputNamePacket, Print[packet]]; Print[packet] Out[42]= InputNamePacket["In[1]:= "] Out[44]= InputNamePacket["In[2]:= "] During evaluation of In[41]:= MessagePacket[Syntax,sntxi] During evaluation of In[41]:= TextPacket[Syntax::sntxi: Incomplete expression; more input is needed. ] During evaluation of In[41]:= TextPacket[Input string: "2+\n\n" ] During evaluation of In[41]:= TextPacket[Position of syntax error: 6 ] During evaluation of In[41]:= SyntaxPacket[5] During evaluation of In[41]:= InputNamePacket[In[2]:= ] 

You can see inconsistency between the positions of the same syntax error reported by SyntaxPacket and $SyntaxHandler . But it seems possible to understand how they calculate the position: both use the InputForm input line and the position before the input line is number 0 for $SyntaxHandler and number 1 in the case of SyntaxPacket . Thus, we can define $SyntaxHandler to get an accurate visual representation of the position of the syntax error inside the input string ( Cell input must have the RawInputForm style ) as follows:

 $SyntaxHandler = Function[{str, pos}, Print["Input string: ", ToString[str, InputForm], "\n", "Position of syntax error: ", pos, "\n", StringInsert[ToString[str, InputForm], ToString[Style["\[DownArrowBar]", Red, Background -> Yellow], StandardForm], pos + 2]]; $Failed]; 

Once again, I emphasize that the input cell MUST have a "RawInputForm" style! Such a cell can be created by creating a regular empty input cell and then converting it to the "RawInputForm" cell using the appropriate command in the menu Cell -> Convert To .

Let's see how it works:

screenshot

The reason we should use the "RawInputForm" cell is probably applied by $SyntaxHandler ony, when the input is sent to the kernel as a String rather than Boxes , as it happens with the default StandardForm input cells.

+9
source

Are you calling ToExpression yourself on a string? The SyntaxLength function will give you the character offset of the first syntax error when applied to a string, for example:

 In[26]:= SyntaxLength["2+"] Out[26]= 4 In[27]:= SyntaxLength["x[1]+x[2]]"] Out[27]= 9 

Note that, as indicated in the docs, when SyntaxLength returns a position outside the input line, this means that the expression is syntactically correct so far, but not completely. Otherwise, SyntaxLength effectively returns the position of the first syntax error.

Unless you explicitly call ToExpression on something, this will probably help to see this cell.

+6
source

Syntax errors, such as unbalanced functional brackets, are usually indicated by coloring characters; an unsurpassed bracket turns pink or so. In this case, there was an input type error. Apparently you called (or the function you used) ToExpression using the result of another failed function. The first step is to search for this ToExpression and find out which function provides its input, returns $ Failed.

Mathematica has a bare-bone debugger, which you can find in the Grade menu. A better debugger can be found in the Matimatica Workbench, which is free if you have a Premier Service license.

If your code is not too big, I would suggest publishing it as part of your question. However, we are not a code verification site.

+4
source

Source: https://habr.com/ru/post/891067/


All Articles