Yacc/Lex/Readline-- Documentation

0.4

logo.png
This is an example of using Yacc/Lex-- and Readline-- together to form a Command Line Interface (CLI) based toy calculator. It's written completly in C++, and all constructs are done in an Object-Oriented way.

FeaturesOverview

The calculator has the following features:

  • Normal arithmatic on real valued numbers, such as '+', '-', '*', and '/':
            toycalc> 2 + 3
              2 + 3  => 5
            toycalc> 3 - 1
              3 - 1  => 2
            toycalc> 3.14 * 2.17
              3.14 * 2.17  => 6.8138
            toycalc> 4 / 2
              4 / 2  => 2
    
  • Scopeing partial expressions using '(' and ')':
            toycalc> 2 + 3 * 4  
              2 + 3 * 4  => 14
            toycalc> (2 + 3) * 4
              (2 + 3) * 4  => 20
    
  • Variable declaration and assignment using '=':
            toycalc> a = 10
              a = 10  => 10
            toycalc> b = 20
              b = 20  => 20
            toycalc> a = 30
              a = 30  => 30
            toycalc> a
              a  => 30
    
  • Arithmatic on variables:
            toycalc> c = a + b
              c = a + b  => 50
            toycalc> c
              c  => 50
    
  • Definition and evaluation of user functions:
      toycalc> f(x) { x + 10 }
              f  => 0
      toycalc> f(a)           
        f(a)  => 40
    
  • Built in functions and variables:
            toycalc> list
            Functions:
                    sin(x) {  ...  }
                    cos(x) {  ...  }
                    tan(x) {  ...  }
                    asin(x) {  ...  }
                    acos(x) {  ...  }
                    atan(x) {  ...  }
                    exp(x) {  ...  }
                    log(x) {  ...  }
                    f(x) { x + 10 }
            Variables:
                    pi      = 3.14159
                    e       = 2.71828
                    a       = 30
                    b       = 20
                    c       = 50
            toycalc> sin(pi/2)
              sin(pi / 2)  => 1
    
  • Normal Readline editing of the command-line, such as <th> Key-sequence</th><th>Effect</th> <th> Key-sequence</th><th>Effect</th>
    C-a Beginning of line C-d End of line
    C-@ Mark C-w Cut
    C-y Paste M-w Copy
    ... and loads more. All ofcourse configurable via users ~/.inputrc in the group toycalc.
  • History! Recall old commands by pressing C-p (Up-arrow), and C-n (Down-arrow), and all such things you would expect from a CLI.

Motivation

I'm using this project as a studie tool to get to know Yacc/Lex and Readline. My long term project is to make an interactive C++ interpretor written entirely in - you guessed it C++. Well, not entirely.

Some parts will be in Yacc/Lex, some in C. The C stuff comes in when I when to use an existing GNU library for some stuff, like Yacc and Lex, but also the Libtool API for loading shared libraries and modules at run-time is planned to be used. However, I'll try to wrap these C API's in C++ classes to make the code consistent and neat.

Top of page
Last update Wed Feb 5 03:34:02 2003
Christian Holm
Created by DoxyGen 1.2.15