C++ :: Evaluating Arithmetic Expressions Using Recursive Descent Parser?

Feb 25, 2013

I have the following code to calculate arithmetic expressions :

#include <iostream>
using namespace std;
using namespace std;
const char * expressionToParse = "6.5-2.5*10/5+2*5";
char peek(){
return *expressionToParse;

[code]....

The problem is that it does not work properly with decimal numbers for example it evaluates 6-2*10/5+2*5 = 12 which is correct but for 6.5-2.5*10/5+2*5 it returns 6 instead of 11.5 .

View 3 Replies


ADVERTISEMENT

C/C++ :: Recursive Descent Parser?

Mar 22, 2014

I am implementing a recursive descent parser that recognizes strings in the language below.

The grammar:
A -> I = E | E
E -> T + E | T - E | T
T -> F * T | F / T | F
F -> P ^ F | P
P -> I | L | UI | UL | (A)
U -> + | - | !
I -> C | CI
C -> a | b | ... | y | z
L -> D | DL
D -> 0 | 1 | ... | 8 | 9

My input file has the following two strings:
a=a+b-c*d
a=a**b++c

The desired output:

String read from file: a=a+b-c*d
The string "a=a+b-c*d" is in the language.
String read from file: a=a**b++c
The string "a=a**b++c" is not in the language.

[Code].....

When I test the code without reading the text file and just write a string in the source code, it appears to parse fine. I believe my main problem is in the int main function and how i am reading the text file and outputting it. I was able to write the same program fine in Java.

View 3 Replies View Related

C++ :: Evaluating Logical Boolean Expressions

Jan 10, 2013

I am looking for a library to aid me in evaluating Boolean expressions. For example, i have an expression like this:

(1*(5+3)+9*65/5-(354*4565*5643+98) >= 12345) && ( 654*987+123 || (2345 > 23423 && 1 != 2)))

(It can also be much longer!) and would like to evaluate them to a true/false boolean.

There are tons of libraries to calculate the (numerical) result of a mathematical expression, but this is not what i want to do.

View 5 Replies View Related

C++ :: Implement Program For Evaluating Infix Expressions

Dec 3, 2014

Also, can't use namespace std for this.

#include<iostream>
#include<stack>
#include<fstream>
#include<iomanip>
#include<queue>
#include<cassert>

[Code] ....

/* It will read in a infix expression from a text file.check if the parentheses in the input expression are balanced.convert the infix expression into a postfix expression and evaluate the expression.*/

int main() {
string expression;
string postfixExpression;
double result;
testBalanced();

[Code] ....

View 8 Replies View Related

C/C++ :: Recursively Building Cons Cell Structure Via Recursive Descent

Apr 25, 2015

I'm currently working on a Scheme Interpreter written in C. I'm trying to create a cons cell structure via recursive descent parsing, except instead of just having the car and cons, I also have a field that holds the token that I receive from the lexical analyzer (which was provided to us). The struct for what I'm describing is as such:

typdef struct node node;
typedef node* list;
struct node {
char* symbol;
list car;
list cdr;
};

Thus a cons cell would be (with a node represented as [symbol][car][cdr]), [null][car][cdr], while a symbol would be [symbol][null][null].

In the top answer for a similar question on Stack Overflow, one of the suggestions was to put the tokens into a stack as the input is recursively parsed, and then from that stack input it into the cons cell structure.

This is something that I'm working towards now, as it is easier for me to understand and I already have implemented a stack in C before, but I know that I can just build the structure recursively, I'm just not sure how. The following is code that I have:

list s_expression() {
list local;
list temp;
if (strcmp(token, "(") == 0) {
strcpy(token, getToken());

[Code] .....

s_expression is supposed to return a pointer to a recursively built cons cell structure. I'm just having issues figuring out when to call getToken(), as I either call getToken in the wrong spot and unintentionally skip over a token, or I call getToken() when I'm done getting all of the tokens, thus causing my program to continue searching for a token from user input instead of continuing on with the rest of the program.

When should I be calling getToken()?

In addition, what would be better, recursively building the cons cell structure as you go through the user's input, or putting all of the tokens into a stack and then building the cons cell structure using that stack?

If needed, I can post the lexical analyzer that's been provided to us. Also, for S_expression(); showing up as S_exp<b></b>pression();, I'm not sure what happened there. I copied this post from my question on stack overflow.

View 12 Replies View Related

C++ :: Libjson Non-recursive Parser Function

Oct 8, 2013

how to write a non-recursive JSON parser function using libjson in C++. libjson is quite useful librray. It's source code of libjson comes with an example C++ parser but it uses recursion to parse JSON arrays and child nodes. I am looking for parser function based on libjson that does not use recursion to parse JSON arrays and child nodes.

View 1 Replies View Related

C++ :: Evaluating Postfix Expression Using Stacks

Nov 3, 2013

i feel like im really close to completing this but i cant seem to get the result printed out i dont think im calling my evaluate function correctly and its not performing the operations..

#include <iostream>
#include <stack> //stack header file
using namespace std;

[Code].....

View 1 Replies View Related

C/C++ :: Infix To Postfix Conversion And Evaluating Expression

Apr 12, 2015

I am trying to convert from infix to postfix, and then evaluating the postfix expression to get the final answer. I am having a severe problem though, because for somereason the conversion is not working at all. For example, when I input the first infix expression:

24 + 33 * ( 7 - 5 ) + 8 / 3 it ouputs 24 33 7 5 - 5 ) + 8 / 3( 7 - 5 ) + 8 / 3* ( 7 - 5 ) + 8 / 3+ 33 * ( 7 - 5 ) + 8 / 3 8 3/ 3+ 8 / 3

Which is obviously very wrong. I am not really sure where the problem is though.

Below I have included all of the code needed. I also had to create my own stack class, so I will include it too .

#include "stacks.h"
#include <iostream>
using namespace std;
bool IsOperand(char C) {
if(C >= '0' && C <= '9') return true;
if(C >= 'a' && C <= 'z') return true;

[Code] .....

View 1 Replies View Related

C++ :: Simple Multiplication And Division Not Evaluating Correctly

May 29, 2013

At one point in my C++, non-CLR program, the following code:

Code:

unsigned int size = 3;
float maxX = (float)(int(size-1))/2.0f;
std::cout << maxX;

outputs 107. Is it something about a conversion from unsigned int to float?

View 6 Replies View Related

C :: Cannot Use Array Name In Expressions

Nov 27, 2014

Code:

int main() {
char *arr[] = {"Hello", "World", "Good", "Morning"};
display(arr);
return;
}

[code]....

The code works fine and prints the 4 strings. Where i m riddled is whether "ptr" in display function is a pointer or a string ? If it's a pointer then what is the type of the pointer? If it's an array, then as per my understanding, we cannot use an array name in expressions such as ptr++ (K&R).

View 2 Replies View Related

C++ :: Regular Expressions Not Working?

Aug 25, 2014

I have this bit of code that I found here: [URL] and at first regex would work, but then when I hit "[]" it wouldn't. Here's the code:

#include <iostream>
#include <string>
#include <regex>

[Code]....

View 2 Replies View Related

C# :: Cannot Convert Lambda Expressions

Mar 31, 2015

I get the following error:

Cannot convert lambda expression to type system delegate because it's not a delegate type (on invoke).

The Second error is: Client.PrivateChat.txtReceive is inaccessible due to its protection level..

private PrivateChat pChat;
private void client_Received(Client sender, byte[] data) {
this.Invoke(() =>
{
for (int i = 0; i < clientList.Items.Count; i++) {
var client = clientList.Items[i].Tag as Client;
if (client == null || client.Ip != sender.Ip) continue;

[Code] .....

View 4 Replies View Related

C++ :: Use Boost / Regex For Regular Expressions?

Sep 5, 2013

So I need to use boost/regex for regular expressions. someone told me that it needs to be built. the first problem is boost doesn't tell you how to build it and the second is i did sudo apt-get install libboost something. I don't remember the exact name of the package. it installed but i dont know how i would build it when its installed.

View 2 Replies View Related

C++ :: Email Validation Without Using Regular Expressions?

Dec 11, 2013

writing a program for "Validating the e-mail id without using regular expressions in c/c++"?

View 4 Replies View Related

Visual C++ :: Regular Expressions - How To Get Iterator

May 22, 2014

In order to parse mathematical expressions I am trying regular expressions and a recursive algorithm, but I have a problem with the four basic operations: +, -, *, /.

Trying to analyze a string like "a+(b+c)", if I use the pattern for a sum "(.+)+(.+)" the program matches it recognizing as subpatterns: "a+(b" and "c". How could I achieve the program to try also the other possibility?

I think that it would be great something like an regex_iterator which worked with regex_match instead of regex_search. I mean, an iterator that iterates over all the possible ways to match a given regular expression and a given string. This way I could loop through all these possibilities until the two subpatterns produced were correct mathematical expressions.

View 3 Replies View Related

C :: Shunting-yard Algorithm To Evaluate Expressions?

Sep 16, 2014

Can I use Shunting-yard algorithm to evaluate Expressions (arithmetic, relational, and logical) ?

View 8 Replies View Related

C++ :: Mixed Expressions / Loops And Formatted Output

Feb 22, 2015

1) ask the user to input a mathematical expression in the following format:

NUMBER Operator NUMBER Operator NUMBER
Example: 17 + 15 - 3
Example: 2 * 3 - 4

How to output the answer of the users equation. Is there a function that includes all math operators (+,-,/,*)? Would i need to write each possible scenario using if statements?

View 2 Replies View Related

C# :: Join Tables In LINQ With Lambda Expressions

Dec 5, 2014

i have stuck in a join and i cant figure out where the problem is, i have those tables

public class Themes
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }

[Code].....

View 12 Replies View Related

C++ :: Overloading Addition And Comparing Expressions In A While Loop

Apr 28, 2015

I have two questions that are related to each other. The first one is about overloading the addition operator.

I have defined a struct as the following:

Code:
#include <iostream>
#include <string>
struct Sales_data {

[Code] ....

I then overloaded the I/O operators so I could print to the screen information related Sales_data.

Code:
// overload ostream in order for cout to work
std::ostream& operator << (std::ostream & out,
const Sales_data & cSales_data) {
out << cSales_data.bookNo << ", " << cSales_data.units_sold << ", "

[Code] ....

My first issue is with overloading the addition operator. Everyone works correctly except for std::cout << item << std:endl; will no not output the ISBN number only the units_sold and revenue when added together.

Code:
// addition operator rules
Sales_data Sales_data::operator + (const Sales_data & data2) {
units_sold += data2.units_sold;
revenue += data2.revenue;
return *this;
}

Now here is the code in its entirety

Code:
#include <iostream>
#include <string>
// Sales_data structure
struct Sales_data {
std:: string bookNo;
unsigned units_sold = 0;

[code] .....

After total = total + item;, I would like to print the total for this particular ISBN. However, I only get: blank, total units, total revenue where blank is where the ISBN would go but doesn't print after addition. My second question has to do with comparing the ISBN's of the books entered during the while loop. I would like to do something like

Code:
if (item_i.bookNo == item_i+1.bookNo) {
total = total + item;
} else {
std::cerr << "Books entered must have the same ISBNs" << std::endl;
}

Unfortunately, I cannot figure out how to set up a comparison of the bookNos. If I used #include <casset> in the overload + rule, it will immediately exit since I have no way to compare the ISBNs.

View 3 Replies View Related

C :: Simple File Parser

Oct 5, 2013

I am writing a simple file parser for use in another project (for config file). The trickiest thing seems to be skipping unwanted characters (comments, spaces). It works partly, but after the second line of an inputed file processes only the first three characters.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TEXT_FILE "data.txt"
[code]....

NOTE: Currently I am just trying to process and remove unwanted data, the actual processing of extracted data should be much simpler.

View 6 Replies View Related

C++ :: Expression Parser Implementation?

May 5, 2014

How can we implement an expression parser in C++.

View 3 Replies View Related

C++ :: Running A SLR Parser Program?

Apr 27, 2012

I need to run a program that makes a SLR Parser Table.

Here is the code :

Code to find first and follow:

saved as SLR.h
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream.h>
#define epsilon '^'

since I didn't know how to type epsilon symbol temporarily I am using ^

char prod[20][20],T[20],NT[20],c[10][10],foll[10][10],fir[10][10];
int tt,tnt,tp,a;
int follow[20][20],first[20][20];
void first_of(char);
int count(int j);
void rhs(int j);

[code]....

View 3 Replies View Related

C++ :: Parser Does Not Name A Type Error

Oct 22, 2014

I am trying to write a program that reads in an XML file, parses it and prints out information about each tag. I am getting the following errors when trying to build the program:

Parser.cpp:24:1: error: 'Parser' does not name a type
Parser::getXMLData() {
^
Parser.cpp:120:1: error: 'Parser' does not name a type
Parser:rocessXMLData(

[Code] ....

Here is my code for the 5 files.

[ATTACH]main.cpp
[/ATTACH][ATTACH]Parser.h
[/ATTACH][ATTACH]Parser.cpp
[/ATTACH][ATTACH]Element.h
[/ATTACH][ATTACH]Element.cpp[/ATTACH]

View 5 Replies View Related

C :: Text Parser - String Separation

Jan 4, 2015

I would like to make a sort of text parser, in which one enters a string, and it is broken by the whitespaces into chunks, and those chunks compared to different "dictionaries" where the words are assigned a value. For example if the operator enters "take lamp" it would separate "take" and "lamp" and then produce preassigned values for each of these words.

View 3 Replies View Related

C :: Simple Parser / How To Deal With Slashes

Feb 13, 2013

I've knocked up a rough C parser for the purpose of colourizing code into XHTML/CSS, which makes me feel fancy. However, it doesn't quite handle comments properly. I can't quite work out how to deal with the slashes. Plus I'm sure there are other places that it slips up that don't feature in my simple tests, so here you go:-

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FLAG_OUTPUT1
#define FLAG_EVEN2
#define NUMKEYWORDS32
}

[code]....

View 6 Replies View Related

C++ :: Getting Segmentation Fault In Parser Code

Dec 23, 2014

For some reason when i put cout<<endl i'm getting segmentation faults in my Parser code. It's really weird some inputs of strings using cout is already faulting. However sometimes printf doesn't fault, but sometimes it will, it seems really unstable.

The court can basically be anywhere in the Parse(string) function, i have a cout at line 254 which faults.

Here is the code:

#include "Parser.h"
#include <stdio.h>
#include <iostream>
using namespace std;
Parser::Parser() {

[Code] ....

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved