$\begin{array}{lcl} \mathit{oddLenPal} & \longrightarrow & \texttt{"A"} \\ & | & \texttt{"B"} \\ & | & \texttt{"C"} \\ & | & \ldots\;\textit{and so on for all the other Unicode letters} \\ & | & \texttt{"A"}\;\mathit{oddLenPal}\;\texttt{"A"} \\ & | & \texttt{"B"}\;\mathit{oddLenPal}\;\texttt{"B"} \\ & | & \texttt{"C"}\;\mathit{oddLenPal}\;\texttt{"C"} \\ & | & \texttt{"D"}\;\mathit{oddLenPal}\;\texttt{"D"} \\ & | & \ldots\;\textit{and so on for all the other Unicode letters} \\ \end{array}$
Note that using the built-in rule $letter$ is tempting, but it can’t work. Grammars don’t have the notion of backreferences as in the regexes of most modern programming languages.
$\begin{array}{lcl} \mathit{edcba} & \longrightarrow & \texttt{"e"}^*\;\texttt{"d"}^*\;\texttt{"c"}^*\;\texttt{"b"}^*\;\texttt{"a"}^*\; \end{array}$
$\begin{array}{lcl} s & \longrightarrow & x\;\texttt{"2"}^* \mid \texttt{"0"}^*\;y \\ x & \longrightarrow & (\texttt{"0"}\;x\;\texttt{"1"})? \\ y & \longrightarrow & (\texttt{"1"}\;y\;\texttt{"2"})? \end{array}$
$\begin{array}{lcl} \mathit{noThreeStraightZeros} & \longrightarrow & (\mathit{atMostTwoZeros}\;\texttt{"1"})^*\;\mathit{atMostTwoZeros} \\ \mathit{atMostTwoZeros} & \longrightarrow & \varepsilon \mid \texttt{"0"} \mid \texttt{"00"} \end{array}$
$\begin{array}{lcl} \mathit{twiceAB} & \longrightarrow & \mathit{chunk}^* \\ \mathit{chunk} & \longrightarrow & \texttt{"a"}\;\mathit{twiceAB}\;\texttt{"a"}\;\mathit{twiceAB}\;\texttt{"b"} \\ & | & \texttt{"a"}\;\mathit{twiceAB}\;\texttt{"b"}\;\mathit{twiceAB}\;\texttt{"a"} \\ & | & \texttt{"b"}\;\mathit{twiceAB}\;\texttt{"a"}\;\mathit{twiceAB}\;\texttt{"a"} \end{array}$
$\begin{array}{lcll} \mathit{anbnanbn} & \longrightarrow & (\texttt{"a"}\;0\;\texttt{"b"}\;\texttt{"a"}\;1\;\texttt{"b"})? & \textit{Place marks between as and bs} \\ 0 & \longrightarrow & \texttt{"a"}\;0\;\texttt{"b"}\;x & \textit{Gen new as and bs in left half} \\ x\;\texttt{"b"} & \longrightarrow & \texttt{"b"}\;x & \textit{Move xs to right of the first bs} \\ x\;\texttt{"a"} & \longrightarrow & \texttt{"a"}\;x & \textit{Move xs to the right of the second as} \\ x\;1 & \longrightarrow & \texttt{"a"}\;1\;\texttt{"b"} & \textit{When x hits the 1, gen as and bs at right} \\ 0 & \longrightarrow & \varepsilon & \textit{Drop mark} \\ 1 & \longrightarrow & \varepsilon & \textit{Drop mark} \\ \end{array}$
$\begin{array}{lcl} \mathit{Program} & \longleftarrow & \mathit{FunDecl}^*\;\mathit{Exp} \\ \mathit{FunDecl} & \longleftarrow & \mathit{id}\;\texttt{"="}\;\mathit{Params}\;\texttt{"=>"}\;\mathit{Exp} \\ \mathit{Params} & \longleftarrow & \mathit{id}\;(\texttt{","}\;\mathit{id}\,)^* \\ \mathit{Exp} & \longleftarrow & \mathit{Exp}\;\texttt{"?"}\;\mathit{Exp}\;\texttt{":"}\;\mathit{Exp} \\ & / & \mathit{Exp}\;(\texttt{"+"}/\texttt{"-"}/\texttt{"*"}/\texttt{"/"}/\texttt{"%"}/\texttt{"<"}/\texttt{"<="}/\texttt{">"}/\texttt{">="}/\texttt{"=="}/\texttt{"!="})\;\mathit{Exp} \\ & / & \texttt{"-"}\;\mathit{Exp}\;/\;\mathit{Exp}\;\texttt{"!"}\\ & / & \mathit{numlit}\;/\;\mathit{strlit}\;/\;\mathit{Call}\;/\;\mathit{id} \;/\;\texttt{"("}\;\mathit{Exp}\;\texttt{")"} \\ \mathit{Call} & \longleftarrow & \mathit{id}\;\texttt{"("}\;(\mathit{Exp}\;(\texttt{","}\;\mathit{Exp})^*)?\;\texttt{")"} \\ \mathit{numlit} & \longleftarrow & \textit{digit}^+\;(\texttt{"."}\;\textit{digit}^+\;)?\;((\texttt{"E"}/\texttt{"e"})\;(\texttt{"+"}/\texttt{"-"}\;)?\;\textit{digit}^+)? \\ \mathit{strlit} & \longleftarrow & \texttt{"\""}\;\textit{char}^*\;\texttt{"\""} \\ \mathit{char} & \longleftarrow & \texttt{"\\\\"}\;(\texttt{"'"}\;/\;\texttt{"\\""}\;/\;\texttt{"n"}\;/\; \texttt{"\\\\"}\;/\;\texttt{"u\{"}\;\textit{hexes}\;\texttt{"\}"}) \\ & / & \sim\texttt{"\\\\"}\;\sim\texttt{"\\""}\;\textit{any} \\ \mathit{hexes} & \longleftarrow & \textit{hexDigit}\;\textit{hexDigit}?\;\textit{hexDigit}?\;\textit{hexDigit}?\;\textit{hexDigit}?\;\textit{hexDigit}? \\ \textit{id} & \longleftarrow & (\textit{letter}/\texttt{"\$"})\;(\textit{letter}/\textit{digit}/\texttt{"_"}/\texttt{"\$"})^* \end{array}$
$\begin{array}{l} n : \textsf{Numlit} \\ s : \textsf{Strlit} \\ i : \textsf{Identifier} \\ d : \textsf{FunDec} = i\;i^*\;e \\ e : \textsf{Exp} = n \mid s \mid i \mid -e \mid e\texttt{!} \mid e+e \mid e-e \mid e*e \mid e\,/\,e \mid e\;\%\;e \mid e\texttt{?}e\texttt{:}e \mid i\;e^* \\ p : \textsf{Program} = d^*\;e \end{array}$
Remember that the tree grammar should treat all lexical categories as primitive.gcd = (x, y) => y ? gcd(y, x % y) : x cube = (x) => x * x * x "The answer is" + cube(gcd(30, 4!)) + "😦😦"
The drawing program I used did not pick up emojis as text so I used escapes in my picture. Hopefully your drawing program was better:
import x from "x" console.log(93.8 * {x} << x.r[z])
super
is a reserved word.`
is not part of any token