1 min read

The grammar to end all grammars!

by Vihan Bhargava
  • Compilers
  • Grammars

Have you been stuck, trying to make an expression parser but you just can't? Well just put this in your parser gen, and you’ll be good to go!

Have you been stuck, trying to make an expression parser but you just can’t? Well just put this in your parser gen, and you’ll be good to go!

EEα;EβEα(;E;)γσpre;EEβσin;Eσpost;ϵ\begin{array}{rl} E \rightarrow & E_\alpha;E_\beta \\ E_\alpha \rightarrow & (;E;) \\ & \gamma \\ & \sigma_{pre};E \\ E_\beta \rightarrow &\sigma_{in};E\\ & \sigma_{post};\\ & \epsilon \end{array}

And to wrap it all up:

N{E}ΣγσSσ\begin{aligned} N &\in \lbrace{E\rbrace} \\ \Sigma &\in \gamma \cup \sigma \\ S &\in \sigma \end{aligned}

and:

N,Σ,R,S\left\langle N,\Sigma, R, S \right\rangle

Where:

γ=Terminal literalsσ={apre=Prefix operatorsain=Infix operatorsapost=Postfix operators\begin{array}{rl} \gamma &= \text{Terminal literals} \\ \sigma &= \begin{cases} \begin{array}{ll} a_{pre} &= \text{Prefix operators} \\ a_{in} &= \text{Infix operators} \\ a_{post} &= \text{Postfix operators} \end{array} \end{cases} \\ \end{array}

TIL greek letters make you look smart.