Comments on a suggested
​SyntaxQ emulator
​|​
​D. Vasholz

Introduction

As part of this Wolfram Community thread, Eric Rembley suggested the function IsExpression as a rough approximation to a SyntaxQ emulator. He used an approach in which my expertise is presently very limited. However no such expertise is required to directly compare the two functions.

Definition of IsExpression

The following has been suggested by Eric Rembley:
In[]:=
ReduceExpressionStep[token_String][exp_String]:=With[{NumberPattern=RegularExpression["(\\d+\\.\\d+)|(\\d+\\.)|(\\.\\d+)|(\\d+)"],StringPattern=RegularExpression["\"[^\"]\""],SymbolPattern=RegularExpression["[[:alpha:]]+"],HeadedPattern="$[]",SequencePattern=RegularExpression["\\[(\\$,)*\\$\\]"]},StringReplace[StringDelete[exp,Whitespace],{NumberPattern->token,StringPattern->token,SymbolPattern->token,HeadedPattern->token,SequencePattern->""}]];​​ReduceExpression[token_String][exp_String]:=FixedPoint[ReduceExpressionStep[token],exp];​​IsExpression[exp_String]:="$"==ReduceExpression["$"][exp]

IsExpression versus SyntaxQ

Direct comparisons

We have chosen a highly restricted 3 - element character set with which to form strings . This set is can in principle form all possible s combinators.
In[]:=
dirtest@nval_Integer:=Module[{allchars,q0,q1,q2},​​allchars={"s","[","]"};​​q0=StringJoin/@Tuples[allchars,nval];​​q1=Grid[Transpose@{Range@Length@q0,q0,IsExpression/@q0,SyntaxQ/@q0},Frame->All,AlignmentLeft];​​q1/.{a_,b_,c_,True}:>{a,b,c,True,FullForm@ToExpression@b}]

Discrepancies

In[]:=
discreps@nval_Integer:=Module[{q1,q2,q3},​​q1=(dirtest@nval)[[1]];​​q2=Cases[q1,{___,True,False,___}];​​q3=Cases[q1,{___,False,True,___}];​​Grid[Union[q2,q3],Frame->All,AlignmentLeft]]

Discrepancy tables as nval ranges from 1 through 6

In[]:=
Grid[Transpose@{Range@6,discreps/@Range@6},Frame->All,AlignmentLeft]
Part
:The expression s cannot be used as a part specification.
Out[]=
1
​
2
​
3
​
4
34
[s]s
True
False
​
16
s[]s
False
True
Times[s,s[]]
5
88
[ss]s
True
False
​
100
[s]ss
True
False
​
16
ss[]s
False
True
Times[s,ss[]]
34
s[s]s
False
True
Times[s,s[s]]
45
s[[]]
False
True
s
46
s[]ss
False
True
Times[ss,s[]]
6
250
[sss]s
True
False
​
262
[ss]ss
True
False
​
295
[s[]]s
True
False
​
298
[s]sss
True
False
​
303
[s]s[]
True
False
​
16
sss[]s
False
True
Times[s,sss[]]
34
ss[s]s
False
True
Times[s,ss[s]]
45
ss[[]]
False
True
ss
46
ss[]ss
False
True
Times[ss,ss[]]
88
s[ss]s
False
True
Times[s,s[ss]]
100
s[s]ss
False
True
Times[ss,s[s]]
133
s[[]]s
False
True
Power[s,2]
136
s[]sss
False
True
Times[sss,s[]]
141
s[]s[]
False
True
Power[s[],2]
151
s[][]s
False
True
Times[s,s[][]]
IsExpression is clearly on the right track, but far from perfect .