Search code examples
cstringexpression-evaluation

what are the steps of coverting a string into a mathmatical function in c?


I am trying to write a program in c that converts a string into a mathematical function to be used in my program. For example: the user will input x+3+7 as a string then the program will convert it to an equation that can be calculated.

Until now I have been able to run a program that parses the string.. but im stuck there. I just want to know the steps in general and then I will complete the code.


Solution

  • Parsing strings and building some representation of a mathematical expression is a large subject, too large for a Stack Overflow question. Parsing theory is taught as a semester or longer subject in computer science after prerequisites including finite state automata and discrete mathematics.

    Roughly speaking, individual input characters are grouped into tokens, and then the tokens are processed to drive a “machine” that parses the input. The machine embodies grammar rules that specify the structure of the language. Each grammar rule is associated with software that transforms some of the input into a software representation (such as turning a variable name into a pointer into a data structure with information about variable names) and/or building data structures that represent expressions or other constructions. For example, a grammar rule for recognizing a + operator with its operands might construct a tree node labeled as a + operation and containing links to subtrees that represents its left and right operands.

    Later, evaluation of the expression would be performed by traversing the tree and evaluating the variables and operations it indicates.