Skip to content

Instantly share code, notes, and snippets.

@darius
Created December 12, 2023 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darius/a5d2eb682886f9e4b4ace7ed55c56d95 to your computer and use it in GitHub Desktop.
Save darius/a5d2eb682886f9e4b4ace7ed55c56d95 to your computer and use it in GitHub Desktop.
grammar-examples
(let grammar (grammar<- "
shifts: shift* :end.
shift: start [nap* :list] :list.
start: timestamp :drop 'Guard #' :count ' begins shift\n'.
nap: timestamp 'falls asleep\n'
timestamp 'wakes up\n' :list.
timestamp: '[' date _ time ']' _.
date: (!_ 1)+.
time: :count :drop ':' :count.
_: :whitespace.
"))
(let grammar (grammar<- "
main: clause* :list '\n\n' program. # N.B. no :end
clause: 'Before: [' [:count (', ' :count)^3 :list] ']\n'
[:count (' ' :count)^3 :list] '\n'
'After: [' [:count (', ' :count)^3 :list] ']\n\n' :list.
program: insn* :list.
insn: :count (' ' :count)^3 '\n' :list.
"))
(let grammar (grammar<- "
main: '^' e '$'.
e: f**'|' :Alt.
f: g* :Seq.
g: dir | '(' e ')'.
dir: {'N' | 'S' | 'E' | 'W'} :Dir.
"))
(let grammar (grammar<- "
main: army**separator :end.
army: army_name ':\n' [group* :list] :list.
army_name: { (!':' 1)* }.
group: :count ' units each with ' :count ' hit points ' opt_qualities
'with an attack that does ' :count ' ' word
' damage at initiative ' :count '\n' :Group.
opt_qualities: '(' qualities ') ' | :list.
qualities: quality_list++'; ' :list.
quality_list: {'weak'|'immune'} ' to ' [word++', ' :list] :list.
word: {:letter+}.
separator: '\n'.
"))
(let g (grammar<- "
rule: bag _ 'contain' _ [bags :list] '.' :end.
bag: {word _ word} _ ('bags'|'bag').
bags: 'no' _ 'other' _ 'bags' | [:count _ bag :list] ++ (','_).
word: :letter+.
_: :whitespace.
"))
(let g1 (grammar<- "
line: expr :end.
expr: factor (_ '*' _ factor :mul | _ '+' _ factor :add)*.
factor: :integer | '(' expr ')'.
_: :whitespace.
"))
(let g2 (grammar<- "
line: expr :end.
expr: e1 (_ '*' _ e1 :mul)*.
e1: factor (_ '+' _ factor :add)*.
factor: :integer | '(' expr ')'.
_: :whitespace.
"))
(let g1 (grammar<- "
start: main :end.
main: :count ': ' rhs.
rhs: '\"' {1} '\"' :Lit | alt.
alt: seq ('|'_ alt :Alt)?.
seq: :count :Ref _ (seq :Seq)?.
_: :whitespace*.
"))
(let grammar (grammar<- "
regex : exp :end.
exp : term ('|' exp :either)*
| :empty.
term : factor (term :then)*.
factor : primary ( '*' :star
| '+' :plus
| '?' :maybe
)?.
primary : '(' exp ')'
| '[' rune* ']' :join :oneof
| '.' :dot
| '\\\\' {1} :literal
| !( '.' | '(' | ')'
| '*' | '+' | '?'
| '|' | '[' | ']')
{1} :literal.
rune : '\\\\' {1}
| !']' {1}.
"))
(let junk "
main: r*.
r: .
s: 'hey' r.
")
(let bal "
bal : ('(' bal ')' bal)?.
")
(let balanced "
bal : '(' c* ')'.
c : !('(' | ')') :anyone
| bal.
")
(let a-and-b-equal-counts "
start : S :end.
S : 'a' B
| 'b' A
| .
A : 'a' S
| 'b' A A.
B : 'b' S
| 'a' B B.
")
(let text "
split : (p | chunk :join) split | . # XXX why not a *?
chunk : p
| :anyone chunk.
p : :whitespace.
")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment