Skip to content

Instantly share code, notes, and snippets.

@grondilu
Last active September 30, 2016 15:10
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 grondilu/2042abc2d847bcd480f6f9caaf513fac to your computer and use it in GitHub Desktop.
Save grondilu/2042abc2d847bcd480f6f9caaf513fac to your computer and use it in GitHub Desktop.
=begin comment
In this grammar, <expression> loops endlessly when tried with an empty string.
It first fails at <variable>, which is fine, it then fails at <function> which is also fine,
but then it keeps trying <application> recursively, even if <application> contains
at least the characters '(' and ')'.
Shouldn't Perl 6 be smart enough to understand that <application> will never match with an empty string?
=end comment
say grammar {
rule TOP { ^^ <expression> $$ }
rule expression { <variable> | <function> | <application> }
rule function { sub [ '(' <variable>? ')' ]? '{' <expression>? '}' }
rule application { <expression> '(' <expression>? ')' }
token variable { '$'<ident>+ }
}.parse: q{ sub {} };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment