Skip to content

Yacc based expression calculator

Luc Yriarte edited this page Apr 6, 2014 · 3 revisions

LR grammar

  • Read input from left to right
  • Rightmost derivation

Bare-bones parser

git checkout simple_expression_parser
cat parser.y

Example expression:

a + b + c

Parse tree:

    +
   / \
  +   a
 / \
b   c

Left-recursive expression calculator

git checkout interpreter
git clean -f
make
echo "(123 - 5 * 4 - 3) / 4" | ./parser

Stack automaton

git checkout yydebug
git clean -f
make
cat y.output
echo "2 * 3" | ./parser