Skip to content

A tiny, efficient and simple calculator using a Recursive Descent Parser (RDP) implementation

License

Notifications You must be signed in to change notification settings

ricardofares/rdp-calc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rdp-calc

🖋️ About

rdp-calc is a tiny, efficient and simple calculator built using compiler concepts.

📖 Brief

rdp-calc as has been said before it was built using compiler concepts, mainly, lexical and syntax analysis concepts. More specifically,

  • The lexer has been implemented by the direct finite-state machine implementation.
  • The parser has been implemented by the direct implementation of a recursive descent parser (RDP).

That said, we can jump to the operations supported in this calculator, how to build it and how to use it.

✖️ Operations

This section describes the available mathematical opereations and constants supported.

Mathematical Operations

The mathematical operations can either be represented by its symbol (e.g., the sum operation represented by +) or by its function name. Moreover, it is not explicited how the user should choose which one to use and, therefore, this is arbitrary.

Operations Symbol Equivalent Function Name
Sum + X
Subtract - X
Multiply * X
Divide / X
Pow ** X
Factorial [] X
Sine X sin
Cosine X cos
Tangent X tan
Cosecant X csc
Secant X sec
Cotangent X cot
Floor X floor
Ceil X ceil
Square Root X sqrt
Cube Root X cbrt
Log10 X log10
Log2 X log2

Table 1: Supported mathematical operations showing the respective its symbol or function name.

X: Indicates absence.

Mathematical Constants

The mathematical constants it is always represente by its name. Therefore, the following table show the supported mathematical constants.

Operations Constant Name Value
Euler's Number e 2.7182818284590452354
PI pi 3.14159265358979323846

Table 2: Supported mathematical constants showing the respective its constant name.

✨ Features

Constants Support

This calculator allows the definition of constants.

Goals

  • Enable the user to break a lengthy expression into a distinct parts providing not only higher semantics for the expression but also higher semantics for each pieece composing the expression.

  • Remove the concern from the user if the expression is being written correctly since it is hard when it is needed to write everything in one line, mainly, the lengthy ones.

  • Enable the user to define factors to be used along the expressions and changing these factors without the need to transverse the expression searching these numbers.

Description

A constant may be defined as

$tau = 6.283185307;

Note that EVERY constant definition MUST start with $ and end with a ;. Therefore, that said, we can use the previously defined constants in the resulting expression as.

tau / 2

the evaluation of the previous expression results in an approximate value of $\pi$.


Comments Support

This calculator allows the insertion of line comments.

Goals

  • Provide a way to the user to insert an example of the expression result.

  • Enable the user to justify some pieces of the expressions providing a higher semantics about the expression being evaluated.

  • Enable the user to insert an introduction about the expression or other information valuable to the user.

Description

A line commant may be inserted as

# This is a line comment!
# The expression below evaluates to 15.
10 + 5

Every LINE COMMENT starts with # and all characters after the # that are in the same line are considered part of the line comment.

🛠️ Compiling

Once you have arrived in this section, then we are going to show you how to compile the calculator.

1. Installing the gcc.

Install the gcc the C compiler executing the following line.

sudo apt install gcc

🔔 Note: If you already have the gcc installed, then you can jump to the next step.

2. Installing the make.

Install the make easies the compilation of our calculator enabling compile the entire project using just one command.

sudo apt install make

🔔 Note: If you already have the make installed, then you can jump to the next step. Further, if you do not want to use make then you can jump the next step.

3. Compiling the calculator (using make).

Execute the following line to compile the calculator (using make)

make

🔔 Note: If you do not have the make installed, then you can jump to the next step. Otherwise, you can jump to the next section.

3. Compiling the calculator (without make).

Execute the following line to compile the calculator (without make)

gcc main.c lexer/lexer.c parser/parser.c -o rdp_calc -lm

🚀 Running

If you already followed the steps provided in the previous section, then you are ready to run our calculator, for that run the following line.

If you would like to execute our calculator on an input file, try

./rdp_calc examples/sum

Otherwise, if you would like to execute our calculator taken as input the standard input, try

./rdp_calc

After that, have fun!

About

A tiny, efficient and simple calculator using a Recursive Descent Parser (RDP) implementation

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages