# What to expect from Interpreter[“Expression”]?

Sometimes it works ok

Interpreter["Expression"]["1"]


1

sometimes not:

Interpreter["Expression"]["Graphics[{}]"]


Why doesn't it work for the second case? What is the rule of thumb here?

• Works on "plot[Sin[x],{x,0,10}]", fails on "Plot[Sin[x],{x,0,10}]". Work on "plot", fails on "Plot". Maybe certain symbols are disallowed for security or something like that. – Michael E2 Feb 12 '18 at 12:11
• @MichaelE2 yes, makes sense but it seems to be be quite sensitive which makes it pretty useless for a general scenario. Unless well documented. – Kuba Feb 12 '18 at 12:15

In interpreting an expression, only some symbols are allowed. A list of allowed symbols may be found (V11.2) with:

Interpreter["Expression"]["1"]; (* to autoload code *)
InternalCheckCache[
InterpreterTransformPackagePrivateiInterpreterObject[
"Expression", <|"Parser" ->
InterpreterPackageScopeexpressionParser,
"iRestrictionFunction" ->
InterpreterPackageScopeexpressionRestriction|>][
"SymbolRestriction"]] // Sort


System symbols not on this list seem to be disallowed. Also other symbols not in the Global context seem to be disallowed.

Interesting inclusions/omissions:

Interpreter["Expression"]["AiryAiZero"]
Interpreter["Expression"]["AiryAi"]
Interpreter["Expression"]["\[FormalX]"]
Interpreter["Expression"]["foox"]
(*
Failure[..]
AiryAi
Failure[..]
Failure[..]
*)


It may be there are other rules in play, but this is my first approximation.

Update: Workarounds. If you wish to throw caution to the wind, you can override the security check at your own risk:

Block[{SecurityInsecureExprQ = False &},
Interpreter["Expression"]["Graphics[{}]"]
]


Targeting exceptions. SecurityInsecureExprQ does have hooks to allow/disallow specific symbols and contexts:

Security$AllowedContexts Security$AllowedSymbols
Security$DisallowedContexts Security$DisallowedSymbols


However Interpreter overrides these. So we can override Interpreter with the Villegas-Gayley trick. Since Interpreter passes the second element allowedSymbols with a head of HoldComplete and the other three as Lists, you have to do the same.

interp[s_String,
{allowedContexts_, allowedSymbols_, disallowedContexts_, disallowedSymbols_}] :=
InternalInheritedBlock[{SecurityInsecureExprQ},
SecurityInsecureExprQ[e_HoldComplete, data_] /; ! TrueQ[$in] := Block[{$in = True},
SecurityInsecureExprQ[e,
{data,
{allowedContexts, allowedSymbols, disallowedContexts, disallowedSymbols}}]]
];
Interpreter["Expression"][s]
];


None of the graphics primitives and directive are on the default allowed list, so we have to add all we use:

interp["Graphics[{Text[Style[foox,Large],{0,0}]},Frame->True]",
{{"foo"}, HoldComplete[Graphics, Text, Style, Large, Frame], {}, {}}]


• +1 Recommend that you use Sort to make the list easier to use. – Bob Hanlon Feb 12 '18 at 13:12
• @BobHanlon Thanks, Bob. I might use MemberQ, too. – Michael E2 Feb 12 '18 at 13:25
• Btw, I meanwhile found and use Interpreter[Restricted["Expression", All]]["Print[1]"]. It is undocumented ofc :) but you can add it for completeness. – Kuba Sep 16 at 10:39
• Also, InternalCheckCache piece does not seem to work in 12.1 – Kuba Sep 17 at 7:09