Skip to main content

One post tagged with "f#"

View All Tags

Parsing polynomials with F# and FParsec

· 9 min read

Parsing polynomials with F# and FParsec

Parsing a polynomial expression is a simple but interesting example of parsing. We need to handle operator precedence and associativity, for example * has a higher precedence than + and the exponentiation is right associative x^y^z=x^(y^z) while subtraction is left associative x-y-z=(x-y)-z. Moreover a straighforward definition of the grammar is left recursive and a recursive descent parser does not allow to parse such grammar. We need to arrange our grammar in order to avoid the left recursion.