Skip to content

Instantly share code, notes, and snippets.

@vmchale
Last active April 24, 2024 03:37
Show Gist options
  • Save vmchale/6503e1dc7b7feadca328d48f1e531108 to your computer and use it in GitHub Desktop.
Save vmchale/6503e1dc7b7feadca328d48f1e531108 to your computer and use it in GitHub Desktop.
Parser in Curry (fl-parser 3.0.0)
-- cypm add fl-parser
-- ported from: https://www-ps.informatik.uni-kiel.de/~cpm/DOC/fl-parser-3.0.0/Parser_curry.html
import Parser
num = Parser.some digit l >>> numeric_value l
where l free
numeric_value ds = foldl1 (\acc x -> 10*acc+x) (map (\c -> ord c-ord '0') ds)
expr = term m Parser.<*> terminal '+' Parser.<*> expr n >>> m+n
<||> term m Parser.<*> terminal '-' Parser.<*> expr n >>> m-n
<||> term
where m,n free
term = factor m Parser.<*> terminal '*' Parser.<*> term n >>> m*n
<||> factor m Parser.<*> terminal '/' Parser.<*> term n >>> div m n
<||> num
where m,n free
factor = terminal '(' Parser.<*> expr e Parser.<*> terminal ')' >>> e
<||> num
where e free
digit = satisfy isDigit
-- expr n "(10+5*2)/4" =:= [] where n free
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment