Happy !

CamilleTeruel / PetitPratt

Project infos

License MIT
Tags left-recursion, petitparser, pratt parser, precedence
Creation date 2015-06-09
Website

Monticello registration

About PetitPratt

PetitParser extension for Pratt parsers. A Pratt parser (a.k.a Top Down Operator Precedence parser) handles left-recursion and operator precedence. It handles grouping, prefix, postfix, infix (right- or left-associative) and "multifix" (e.g. "if ... then ... else ...", "... ? ... : ...", Smalltalk keyword messages) operators. Normally a Pratt Parser needs a tokenization phase but here tokenization is done on the fly with other petit parsers. Apart from tokenization, no backtracking is needed so parsing is quite fast.

Exemple: Calculator

parser := PPPrattParser new.
"Numbers"
parser terminal: #digit asParser plus do: [ :token | token inputValue asNumber ]. 
"Parentheses"
parser groupLeft: $( asParser right: $) asParser. 
"Addition, substraction, multiplication, division: all left infix, * and / have higher precedence than + and -"
parser leftInfix: $+ asParser precedence: 1 do: [ :left :op :right | left + right ].
parser leftInfix: $- asParser precedence: 1 do: [ :left :op :right | left - right ].
parser leftInfix: $* asParser precedence: 2 do: [ :left :op :right | left * right ].
parser leftInfix: $/ asParser precedence: 2 do: [ :left :op :right | left / right ].
"Power: right infix with higher precedence than multiplication and division"
parser rightInfix: $^ asParser precedence: 3 do: [ :left :op :right | left raisedTo: right ].
"Unary minus: prefix with highest precedence"
parser prefix: $- asParser precedence: 4 do: [ :op :right | right negated ].

parser parse: '2*3+4^(1/2)*3' ----> 12