yapps/examples/expr.g
Matthias Urlichs b2fef34742 more Py3 fixes
2014-05-07 05:47:38 +02:00

21 lines
912 B
Text

parser Calculator:
token END: "$"
token NUM: "[0-9]+"
rule goal: expr END {{ return expr }}
# An expression is the sum and difference of factors
rule expr: factor {{ v = factor }}
( "[+]" factor {{ v = v+factor }}
| "-" factor {{ v = v-factor }}
)* {{ return v }}
# A factor is the product and division of terms
rule factor: term {{ v = term }}
( "[*]" term {{ v = v*term }}
| "/" term {{ v = v/term }}
)* {{ return v }}
# A term is either a number or an expression surrounded by parentheses
rule term: NUM {{ return int(NUM) }}
| "\\(" expr "\\)" {{ return expr }}