Skip to content

Parser grammars

Karsten Schmidt edited this page Jul 17, 2020 · 6 revisions

S-expression grammar

Lisp / Clojure / WASM-style languages & data structures

list: '('! <expr> ')'! ;
vec: '['! <expr> ']'! ;
sym: ( <ALPHA_NUM> | [?!$+\u002d*/.~#^=<>] )+ => join ;
expr: ( <FLOAT> | <STRING> | <sym> | <list> | <vec> | <WS1> )* ;
main: <START> <expr> <END> => hoist ;

Playground

Forth style language grammar

# forth style grammar w/ additional line & stack comments, quotations
csym: [a-z0-9]+ => join ;
sym: [a-z0-9+\-*/$#!]+ => join ;
comment: '#'! <WS0> .(?-<LEND>) => join ;
sfxpart: ( <csym> | <WS1>)+ ;
stackfx: <WS1> '('! <sfxpart> "--"! <sfxpart>? ')'! ;
prim: (<FLOAT> | <STRING>) ;
quote: '['! ( <comment> | <prim> | <sym> | <quote> | <WS1> )* ']'! ;
def: ':'! <WS1> <sym> <stackfx>? <WS1> ( <comment> | <prim> | <sym> | <quote> | <WS1> )+ ';'! ;
main: ( <comment> | <prim> | <sym> | <def> | <quote> | <WS1> )+ ;

Playground

XML(ish)

sym: [A-Za-z0-9:\-_.]+ => join ;
aval: .(?+'"'!) => join ;
vattrib: <WS1> <sym> "=\""! <aval> ;
battrib: <WS1> <sym>(?-<WS1>) => hoist ;
attrib: (<vattrib> | <battrib>) ;
elstart: '<'! <sym> <attrib>* '>'! ;
elend: "</"! <sym> '>'! => discard ;
voidtag: '<'! <sym> <attrib>* <WS0> "/>"! ;
body: .(?-'<'!) => join ;
elem: <elstart> (<body> | <elem> | <voidtag>)* <elend> ;
main: (<elem> | <voidtag> | <WS1>)* ;

Playground