how to parse mathematical functions in C++ [duplicate] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can you give me some ideas about how can I make a simple mathematical expression parser in C?
User enters a mathematical function in a string and from the string I want to create the function in C.
eg. x + sin(2*x)
-> return x + sin(2x);
Thanks in advance.

You can parse the expression based "Shunting-Yard Algorithm" http://en.wikipedia.org/wiki/Shunting-yard_algorithm. You will need to extend to handle the function calls such as sin, cos etc...

This is not a simple thing to do at all, in face, it's a hard thing. You need a full grammar parser, combined with pre-defined constants/functions (sin, log, pi, etc).
If you have no extensive previous experience with C I would disrecommend doing this, but if you really want to do this look at recursive descent parsing which is arguably the easiest way to do this (without putting a burden on the user, like reverse polish notation).
Last but not least you say you want to create a C function from the user-generated input. This is almost always a wrong thing to do - generating code from user input, instead the easiest approach is pre-processing to create a intermediate representation that can be efficiently executed.

Writing an expression parser and evaluator is one of the usual examples used when discussions parser writing techniques.
For example you could look the documentation for flex/bison or lex/yacc. That will have examples of constructing parsers/expression evaluators.

One way to do it is to use reverse polish notation for the expressions and a stack for the operands.
Some quick pseudo-code:
if element is operand
push in stack
else if element is operation
pop last 2 elements
perform operation
push result in stack
Repeat till end of expression. Final result is the only element in stack.

Related

Where did the function and the variable come from? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Please sorry, I am JavaScript and TypeScript guy, not a c++ one.
But the JS engine V8 is written in c++ and here is a code piece from there:
// Convert the result to an UTF8 string and print it.
v8::String::Utf8Value utf8(isolate, result);
printf("%s\n", *utf8);
In the code above there are two lines.
First line contains utf8 function... where does it come from? I didn't see it before in the file and it wasn't imported (or was it)?
Second line contains utf8 variable (right?), though with * modifier which I am not aware of. Where did the variable come from? What is the role of the star modifier?
Sorry for this kind of questions, but at this point I cannot delve into the documentation of one of the most complex languages, which is c++...
utf8 is not a function, but a variable. The snippet (isolate, result) is the arguments passed to its constructor.
This could be rewritten as follows to be functionally identical, and in a way that is more familiar to a JavaScript programmer:
auto utf8 = v8::String::Utf8Value(isolate, result);
where auto infers the type of a variable.
As for the * in *utf8, the meaning of this will depend the implementation. * as a prefix operator can be given a user-defined meaning, though usually it has the semantics of "reach into and get the value from," as with raw pointers and things like std::unique_ptr and std::optional.
I'm not familiar with v8 personally. You should look for documentation on the * operator for the v8::String::Utf8Value type, to see exactly what it does.
You should also be very aware that C++ takes a long time to learn, and it's terribly easy to misunderstand or do things very wrong. If you want to commit to learning C++, I would suggest reading a good C++ book to get a foundational understanding.

insert parentheses in a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a mathematical expression which is given via a string.For example:
-4^2+3*(-23^2+5), I want the resultat to be (0-4)^2+3*((0-23)^2+5)
I need to write a function which does that kind of thing for the full string.
Can anyone help?
thanx in advance
I suggest you (1) create a parser for your expressions, then (2) parse the string into an abstract syntax tree (AST), and then (3) pretty print the AST according to your needs.
(1): Writing a parser by hand gets messy quickly, so I suggest you write your grammar in some nice BNF format and use a parser generator. See for instance this question: C++ parser generator
(2): Parsing the expression should boil down to calling a parse method on your generated parser.
(3): To pretty print the AST you can either write a recursive function that traverses the tree, or use a visitor, or put the pretty print methods directly on the different nodes and rely on polymorphism.

Is it OK to mix string parsing while learning reg ex? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm performing some regular expression exercises in Python 2.7.3, on Windows 7. Some of these exercises have me looking for similar patterns on the same line. For example, I want to use regex to capture name1 and name2...
<XML tag><more tags>[name1]</XML tag><XML2 tag>[name2]<XML2 tag></more tags>
Would it be "cheating" or "missing the point" if I used any string parsing to capture name2? I feel like using regex the correct way alone should be able to capture both of those names, but string parsing is what I've always been familiar with.
An analogy would be like someone studying recursion in C++, but using a While loop. Recursion should NOT have any While loops (although of course it may be part of some other grand design).
Good question! Many beginners come into it believing they should be able do everything with one regex match. After all, people are always saying how powerful regexes are, and what you're trying to do is so simple...
But no, the regex is responsible for finding the next match, that's all. Retrieving the substring that it matched, or finding multiple matches, or performing substitutions, that's all external to the act of matching the regex. That's why languages provide methods like Python's findall() and sub(); to do the kind of "string parsing" operations you're talking about, so you don't have to.
It occurred to me a while back that the process of mastering regexes is one of learning everything you can't do with them, and why not. Understanding which parts of the regex matching operation are performed by the regex engine, and which parts are the responsibility of the enclosing language or tool, is a good start.

Right sequence of brackets [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Please help with writing program on C++. We have a sequence of brackets. It consists from 4 kinds - (), [], {}, <>. Required to find the shortest sequence with the right placement of brackets, for which the initial sequence would be a subsequence, i. e. would be obtained from the resulting correct sequence by deleting some (possibly zero) number of brackets.
Example:
initial sequence <]}} {([])
the answer: <[] {} {} ([]) <>>
Your proposed answer doesn't seem to fit the requirements. For example, it doesn't look (at least to me) like you can generate the }}{ sequence by deleting elements from <[] {} {} ([]) <>>. You also seem to have a completely unnecessary pair of angle brackets. Presumably, your intent is also that the brackets in the generated sequence are balanced--otherwise, the correct answer is to simply leave the original sequence unchanged. With no other requirements, that's clearly the shortest sequence from which you can generate that sequence by deleting (zero) items.
If the requirement for balancing is correct, it looks like your original input has four possible correct results:
<[]{}{}{([])}>
<[]{}{}{}([])>
<>[]{}{}{}([])
<>[]{}{}{([])}
All these are the same length, so I don't see a particular reason to prefer one over the other. This looks enough like homework that I'm not going just give a direct solution to the problem, but I think the simplest code you could write for the job would probably produce the first of these four solutions (and that may provide at least some guidance about how I'd solve the problem).
I'm reasonably certain this can be done entirely using counters--shouldn't need any sort of "context stacks" (though a stack-based solution is certainly possible as well).

Techniques for simplifying a regular expression **by hand** [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm trying to prove that the RE
s(bs)*[(abb*a*)*ab]*aa(a∪b) where s=b*a*ab
can be simplified to
(a∪b)*abaa(a∪b),
but all the general transformations I know such as (ab)*a=a(ba)* or (a*b)*a*=(a∪b)* seem to have no effect.
So the questions are:
Is a step-by-step transformation process here viable? If so, can you show me how it is done?
If other types of technique are needed for the proof, what are they?
Is there a clear & comprehensive reference explaining techniques used in simplifying formal RE's?
Thanks.
There are lots of ways to do this.
One simple way to do it is to convert the regular expressions to NFAs. To see if two NFAs recognize the same language:
Consider the starting state of both NFAs.
Take the epsilon closure of the states you are considering for each NFA.
If the set of states for one NFA contains at least one accepting state but the set for the other NFA contains no accepting states, then the NFAs are not equal and you are done.
Otherwise, for each symbol, follow that symbol to get a new set of states for each NFA, and go back to step 1.
This will take a finite number of steps because there are a finite number of sets of states to consider.
You could also convert both regular expressions to minimal DFAs and show that they are isormorphic, but this technique is really just the same thing as the above technique but with the steps in a different order.
Illustration
Consider the REs aa* and a*a. It's obvious that they recognize the same language, but we'll use them to illustrate the algorithm.
First we consider the starting states for each NFA. These are {1} for the left RE and {1} for the right RE, which we will write as {1},{1}.
Then we take the epsilon closure, which is {1},{1,2}.
Neither set contains an ending state, so we continue.
The outgoing symbols are only a, so we take all a transitions to get {2},{1,3}.
The epsilon closure is {2,3},{1,2,3}.
Both contain ending states, so we continue.
Again, the outgoing symbols are a, so we take all a transitions to get {2},{1,3}.
The epsilon closure is {2,3},{1,2,3}. This is the same as step #5, so we don't do it again.
Since there is nothing else to examine, we are done, and the two have been proven identical.