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


ADVERTISEMENT

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 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 :: 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

C++ :: CSV Parser - Infile Using Variable Filename?

Jan 18, 2013

So. I have a CSV Parser that I built. It works very well. The way it currently works is that I have the parser in a header file "CSV_Parser". So my .cpp code would look like this:

#include <iostream>
#include <eigen3/Eigen/Dense>
#include "CSV_Parser.h"
using namespace std;
using Eigen;

[Code] ....

This all works great. I am running Eclipse in Linux (Xubuntu to be precise). In the header file the .csv is opened as follows:

ifstream infile;
infile.open("/home/karrotman/Desktop/Matrix.csv");

What I would like is for the user to be able to change the file that the parser is opening through the main .cpp file. In other words, is there a way to create some variable, say "FileName" and do the following:

CSV firstMat;
string MatrixA = "/home/karrotman/Desktop/Matrix1.csv";
firstMat.extract(MatrixA);
CSV secondMat;
string MatrixB = "/home/karrotman/Desktop/Matrix2.csv";
secondMat.extract(MatrixB);

And then the .h would now say:

void extract(string FileName)
...
ifstream infile;
infile.open(FileName);

Obviously this does not work. Ultimately the goal is for me to open 4 .csvs at one time so I can do numerical operations on them.

View 2 Replies View Related

C/C++ :: How To Do Error Handling In Predictive Parser

May 9, 2014

#include<iostream>
using namespace std;
void E();
void T();
void F();
void match(char m);

[Code] ....

I have written this code for predictive parser. It is working for valid input but it is not working for invalid input when i give invalid input it says its valid. How to do error handling in predictive parser.

View 3 Replies View Related

C++ :: SIP Parser And Header File Including (using Sofia-sip)

Nov 6, 2012

I am currently developing a small quick'n dirty SIP (Session Initiation Protocol) parser. Its task is to read a file (or command-line argument) with a SIP requst and parse it using the sofia-sip library. It should exit either with "1" on parsing failure or "0" on parsing success.

I have taken the code snipplet from here: [URL] .... resulting in this source code:

Code:
#include <iostream>
#include <stdio.h>
#include <sofia-sip/msg.h>
#include <sofia-sip/msg_parser.h>
#include <sofia-sip/msg_mclass.h>
int main() {
char *msg_data = "...";

[Code] ....

When I try to compile the code using

Code:
g++ -o parser -I/usr/include/sofia-sip-1.12 parser.cpp

I get the following failure:

Code:
parser.cpp:10:45: error: "sip_default_mclass" was not declared in this scope ....

View 4 Replies View Related

Visual C++ :: Writing A Lexical Analyzer / Parser

Oct 6, 2012

I'm trying to learn more about how Lexical Analyzers/Parsers work. I haven't coded any classes yet, because i'm not really sure how the entire process from a Lexer to working code goes.

My goal is to write a simple made up programming language and translate that to another language, like Javascript. The first thing i have to do is give the code to a Lexical Analyzer.

The lexer will split the source into tokens and assign a label to it. So suppose i have the following code:

Code:
def myVar = 10;

The lexer will split that into token like this:

Code:
def -> keyword
myVar -> Identifier
= -> Operator
10 -> Number

That's basically as far as i understand what a lexer does. How can i translate the tokens to a language like Javascript? From what i understand i need to write a Parser class. But i couldn't find any info on what that class exactly does. So what is exactly the next step i have to take?

View 2 Replies View Related

C++ :: Program That Handles Configuration Files - Config Parser?

Apr 9, 2013

I wrote a small program that handles configuration files. I was wondering if this code is considered "good?" I am also wondering if there are ways to optimize it.

class configFile {
std::vector<std::string> variables;
std::vector<std::string> values;
public:
/* declare the constructor function */
configFile(std::string loc) {

[Code] ....

View 3 Replies View Related

C :: Writing A Simple File / Text Parser To Read A Config File

Feb 6, 2014

I am writing a simple file/text parser to read a config file for some code I am working on. It's dead simple and not particularly smart but it should get the job done. The code reads a config file:

Code:

readlength=2500000
start=0
finish=25000000
cutoff=20000
samplingfreq=250000
poles=10
filterpadding=500
}

[code]....

Here is where it gets wierd. You'll notice that there is an unused variable (filepath) in the config struct. This variable is not referenced or used anywhere in the code, ever. Yet if I comment out the declaration of char filepath[1024], the code segfaults partway through the read_config() function.

My best guess is that there is a buffer overflow elsewhere and it just so happens that the memory allocated for filepath happened to be there to catch it up until now, but I can't work out where it might be happening. With the declaration commented out, the read_config() function gets as far as reading the "padding" variable before it crashes. Yet when the declaration is there, then all the variabled are read correctly and everything seems to work.

View 10 Replies View Related

C++ :: Recursive Modification Of Map

Jun 23, 2014

I am having trouble with recursively modifying a <string, int> map (NCPB) -- when I call for what should be existing values in the map, I get "junk" values back (integers, but sometimes negative, large numbers, etc.). I've posted only the problematic function here:

int Count_Noncrossing(string RNA, map<string, int> &NCPB) {
map <string, int>::iterator it;
if (RNA.length() <= 2)//perfect short interval can only have 1 match; return 1 {
return 1;

[Code] ....

The problem is that when I ask for existing map values in a subsequent recursive call, they don't seem to be there. I imagine I'm missing something straightforward but can't seem to find it. I've tried declaring the map globally, passing it (as shown above), nothing seems to work.

View 3 Replies View Related

C :: Feedback On Recursive Function

Nov 16, 2013

The recursive function is bolded, i got feedback and was told that the static variable made the function seem a lot like a iterative function but he did not say why.

Code:
#define MAX 100
#include <string.h>
#include <stdio.h>
int checkPalindrome(char string[MAX]);
int checkRecPalindrome(char string[MAX]);

[Code] .....

View 7 Replies View Related

C :: Recursive Crashes Program?

May 13, 2014

cause I cant find why it crashes. It compiles without any error,but crushes when i run it and I can't find where is wrong the code.

Code:

#include <stdio.h>
#include <time.h>
#include <math.h>

[Code].....

View 7 Replies View Related

C :: Way Recursive Calls Actually Works

Apr 27, 2013

This is simple recursive solution of Fibonacci number:

Code:

int fibo(int n)
{
if(n<=1)
return 1;
else
return fibo(n-1)+fibo(n-2);
}

Now the recursion will generate a large recursion tree, like if n=5, 5 will call (5-1), (5-2) or 4,3 . What I want to know is, will fibo(n-1) will be called 1st go all the way to the base case 1, then do the summation or fibo(n-2) will be called right after fibo(n-1) ?

View 6 Replies View Related

C :: Recursive Function - Add All Even Numbers From N To 2

May 5, 2014

I'm writing a program that starts at a given number n and adds all the way to 2:

n + (n-2) + (n-4) + (n-6) + .....

The following is my code, it compiles but after I enter an integer the program crashes.

Code:
#include<stdio.h>
#include<stdlib.h>
int sum_even(int n);
int sum_even(int n){
if(n==1){

[Code] ....

View 11 Replies View Related

C :: Recursive Function Not Working

Nov 9, 2013

the functions checks if the word is a palindrome like"level" "madam" etc. but with input "dfdfdfdffdfd" my recursive function fails.

Code:

/* main.c - Created on: Nov 9, 2013 - Author: Kaj P. Madsen*/
#define MAX 100
#include <string.h>
#include <stdio.h>
int checkPalindrome(char checkString[MAX]);
int checkRecPalindrome(char checkString[MAX], int strLgt, int a);
}

[code]....

results from "dfdfdfdffdfd" added some print to see that its the variables a and strLgt not functioning properly

Code:

dfdfdfdffdfd.
The word is not a palindrome(iterative)
strLgt: 11 a: 0
a: d strLgt: dstrLgt: 10 a: 1
a: f strLgt: fstrLgt: 9 a: 2
a: d strLgt: dstrLgt: 8 a: 3
a: f strLgt: fstrLgt: 7 a: 4

The word is palindrome (recursive)

View 4 Replies View Related

C++ :: How To Turn Recursive Into Iterative

Jul 10, 2014

map< int, int > cache;
int count( int n ){
if( cache[n] != 0 ){
return cache[n];

[Code] ....

I don't know how to turn this recursive function into an iterative...

View 7 Replies View Related

C++ ::  From Recursive To Iterative Function

Jan 5, 2015

I am trying to make from f_rec (recursive function) to f_iter (iterative function) but I can't.

(My logic was to create a loop to calculate the results of f_rec(n-1), another loop for 2*f_rec(n-2) and one loop for f_rec(n-3);

But I'm wrong)

int f_rec(int n) {
if(n>=3)
return f_rec(n-1)+2*f_rec(n-2)+f_rec(n-3);

[Code] .....

I also think that my run time for the f_rec is 3^n ...

View 2 Replies View Related

C/C++ :: Looping Recursive Functions?

Feb 10, 2014

how to recursively modify my program. The problem I'm hacing is the the program is not looping correctly and also not printing the correct number. I've calculated the payoff correctly, also I've only been able to print the first section of R3. I can't figure out how to loop it to get R2 to stay at 2 then go to 3 after all possiblities of R2 at 2. Enventually, R1 will change to 2 then 3; 3 being the highest number earned. To be mentioned that will be three recursive function loopR1, loopR2, and loopR3 for each column.

The result of the program should look like:

R1 R2 R3
1 1 1 payoff is 1
1 1 2 .......... 1
1 1 3 .......... 1
1 2 1 .......... 1
1 2 2 .......... 1
...
...
...
3 3 2 .............. 5
this is what I have so far:

#include <stdio.h>
#include <stdlib.h>
int payOff(int r1, int r2, int r3);
void loopR3(int R3, int upto);
void loopR2(int R2, int upto);

[Code].....

View 12 Replies View Related







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