C Arithmetic Expression Calculator

user2976897

I have to implement an expression calculator without using stacks. I'm just really confused as to how to start this. If anyone could explain to me what I'm supposed to do, I would really appreciate it. I realize this isn't the best question, nor is it specific, but I'm just having a really hard time even getting started.

I just don't understand (at all) how I'm supposed to use recursion to implement this calculator. How am I supposed to use the m_exp l_op h_op etc that they defined? I just don't understand at all, sorry for the lack of better words :(

enter image description here

MBlanc

Given the expression 2.3 * 4 - 7.8 / 9, you want to build a parse tree:

                         "-"
                    ._____|_____.
                    |           |
                   "*"         "/"
                 .__|__.     .__|__.
                 |     |     |     |
                2.3    4    7.8    9

It can be generated by recursively applying the definitions given in the last part of the exercise:

Start:  EXP
Rule 4: EXP L_OP M_EXP
Rule 5: M_EXP "-" M_EXP
Rule 2: M_EXP H_OP M_EXP "-" M_EXP H_OP M_EXP
Rule 6: M_EXP "*" M_EXP "-" M_EXP "/" M_EXP 
Rule 1: 2.3 * 4 - 7.8 / 9

A way of approaching this is making a series of functions that handle each rule:

void m_exp();
void   exp();
etc...

Each of these functions will try to match the next part of the input, and update some global variables accordingly. Wikipedia has a great article about recursive descent parsers, which includes a C example.

How exactly am I supposed to "recurisvely apply the definitions given in the last part of the exercise", the stuff you wrote?

Here is an excerpt from the aforementioned article, slightly modified:

// Implements Rule 1
void num(void) {
    // Reads a number
}

// Implements Rule 2
void m_exp(void) {
    num();
    while (sym == '*' || sym == '/') {
        getsym();
        num();
        // Does more work...
    }
}

// Implements Rule 4
void exp(void) {
    m_exp();
    while (sym == '+' || sym == '-') {
        getsym();
        m_exp();
        // Does more work...
   }
}

Rules 5 and 6 are already built-in. Rule 3 is the entry point.

Here, the [tail recursion] has been replaced by loops. Note how exp calls m_exp many times, and m_exp does the same with num.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Checking Valid Arithmetic Expression in Lex (in C)

From Dev

c++ overflow while calculating an arithmetic expression

From Dev

c# split arithmetic expression to string[]

From Dev

c# split arithmetic expression to string[]

From Dev

Regular expression for arithmetic expression

From Dev

Switch menu calculator will not display arithmetic

From Dev

Infix expression calculator racket

From Dev

Variable as expression - advanced calculator

From Dev

Prolog dynamic arithmetic expression

From Dev

arithmetic expression reader in python

From Dev

Tokenizing an arithmetic expression?

From Dev

Parse arithmetic expression with javascript

From Dev

Evaluate arithmetic expression recursively

From Dev

Why this arithmetic expression is not equal?

From Dev

Prolog dynamic arithmetic expression

From Dev

arithmetic expression in Prolog

From Dev

Is a string an arithmetic expression?

From Dev

Bash arithmetic expression with variable

From Dev

Bash scripting for arithmetic expression

From Dev

Arithmetic in Regular Expression

From Dev

Forth expression arithmetic operations

From Dev

RPN PostFix Calculator output not as expected for simple arithmetic

From Dev

How to transform a arithmetic expression to a Python arithmetic function?

From Dev

Regular expression for calculator with decimal point

From Dev

Gnome's calculator: "Malformed expression"

From Dev

"Simple" Expression Language - arithmetic operators?

From Dev

Is it possible to use arithmetic expression in FXML?

From Dev

DRY arithmetic expression evaluation in Prolog

From Dev

Prolog: Unification of Arithmetic Expression and Constant