www.delorie.com/gnu/docs/bison/bison_34.html   search  
Buy the book!

Bison 1.875

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.5 Multi-Function Calculator: mfcalc

Now that the basics of Bison have been discussed, it is time to move on to a more advanced problem. The above calculators provided only five functions, `+', `-', `*', `/' and `^'. It would be nice to have a calculator that provides other mathematical functions such as sin, cos, etc.

It is easy to add new operators to the infix calculator as long as they are only single-character literals. The lexical analyzer yylex passes back all nonnumber characters as tokens, so new grammar rules suffice for adding a new operator. But we want something more flexible: built-in functions whose syntax has this form:

function_name (argument)

At the same time, we will add memory to the calculator, by allowing you to create named variables, store values in them, and use them later. Here is a sample session with the multi-function calculator:

$ mfcalc
pi = 3.141592653589
alpha = beta1 = 2.3

Note that multiple assignment and nested function calls are permitted.

2.5.1 Declarations for mfcalc  Bison declarations for multi-function calculator.
2.5.2 Grammar Rules for mfcalc  Grammar rules for the calculator.
2.5.3 The mfcalc Symbol Table  Symbol table management subroutines.

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003