đź“ťPratt parser

tags

§ Programming language implementation

A generalized parsing technique for hand-crafted parsers.

  1. build a table of operators (both infix and postfix)

    • for each operator: its precedence, associativity, a prefix parser function, an infix parser function

  2. parse_precedence function takes minimum precedence to parse as input

    • get first token

    • call prefix parser for the given token (if there is no infix parser → syntax error)

    • then, while the next token precedence is ≥ input precedence

      • call infix parser for the next token, passing previously parsed expression as parameter

      • if there is not infix parser → return previously parsed expression

Pros

See also

References

Backlinks