473,379 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

error: parse error before `(' token

Hi,
I try to use
input = new istrstream(argv[1],strlen(argv[1]));
in my code, but it always says error: " error: parse error before `('
token"

please help me!
Thanks,

#include <map>
#include<iostream>
#include<cctype>
#include<string>

using namespace std;

namespace Error {

struct Zero_divide { };

struct Syntax_error {
const char* p;
Syntax_error(const char* q) { p = q; }
};
}

namespace Lexer {

enum Token_value {
NAME, NUMBER, END,
PLUS='+', MINUS='-', MUL='*', DIV='/',
PRINT=';', ASSIGN='=', LP='(', RP=')'
};

Token_value curr_tok;
double number_value;
string string_value;

Token_value get_token();
}

namespace Parser {
double prim(bool get); // handle primaries
double term(bool get); // multiply and divide
double expr(bool get); // add and subtract

using namespace Lexer;
using namespace Error;
}

namespace Symbol_table {
map<string,double> table;
}

namespace Driver {
int no_of_errors;
std::istream* input;

void skip();
}

Lexer::Token_value Lexer::get_token()
{
char ch;

do { // skip whitespace except '\n'
if(!Driver::input->get(ch)) return curr_tok = END;
} while (ch!='\n' && isspace(ch));

switch (ch) {
case 0:
return END;
case ';':
case '\n':
return curr_tok=PRINT;
case '*':
case '/':
case '+':
case '-':
case '(':
case ')':
case '=':
return curr_tok=Token_value(ch);
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '.':
Driver::input->putback(ch);
*Driver::input >> number_value;
return curr_tok=NUMBER;
default: // NAME, NAME =, or error
if (isalpha(ch)) {
string_value = ch;
while (Driver::input->get(ch) && isalnum(ch))
string_value += ch; // string_value.push_back(ch);
// to work
around library bug
Driver::input->putback(ch);
return curr_tok=NAME;
}
throw Error::Syntax_error("bad token");
}
}
double Parser::prim(bool get) // handle primaries
{
if (get) get_token();

switch (curr_tok) {
case Lexer::NUMBER: // floating point constant
get_token();
return number_value;
case Lexer::NAME:
{ double& v = Symbol_table::table[string_value];
if (get_token() == ASSIGN) v = expr(1);
return v;
}
case Lexer::MINUS: // unary minus
return -prim(1);
case Lexer::LP:
{ double e = expr(1);
if (curr_tok != RP) throw Error::Syntax_error("`)' expected");
get_token(); // eat ')'
return e;
}
case Lexer::END:
return 1;
default:
throw Error::Syntax_error("primary expected");
}
}

double Parser::term(bool get) // multiply and divide
{
double left = prim(get);

for (;;) // ``forever''
switch (curr_tok) {
case Lexer::MUL:
left *= prim(true);
break;
case Lexer::DIV:
if (double d = prim(true)) {
left /= d;
break;
}
throw Error::Zero_divide();
default:
return left;
}
}

double Parser::expr(bool get) // add and subtract
{
double left = term(get);

for(;;) // ``forever''
switch (curr_tok) {
case Lexer::PLUS:
left += term(true);
break;
case Lexer::MINUS:
left -= term(true);
break;
default:
return left;
}
}

void Driver::skip()
{
no_of_errors++;

while (*input) { // discard characters until newline or semicolon
// note: skip doesn't know the state of the parser
// so if the erro rwas caused by a newline
// or a semicolon, we need to look for
// yet another terminator
char ch;
input->get(ch);

switch (ch) {
case '\n':
case ';':
return;
}
}
}

#include <strstream.h>

int main(int argc, char* argv[])
{
using namespace Driver;

switch (argc) {
case 1: // read from standard input
input = &cin;
break;
case 2: // read argument string
input = new istrstream(argv[1],strlen(argv[1])); //error line
break;
default:
cerr << "too many arguments\n";
return 1;
}

// insert pre-defined names:
Symbol_table::table["pi"] = 3.1415926535897932385;
Symbol_table::table["e"] = 2.7182818284590452354;

while (*input) {
cout<<"new expression:\n";
try {
Lexer::get_token();
if (Lexer::curr_tok == Lexer::END) break;
if (Lexer::curr_tok == Lexer::PRINT) continue;
cout << Parser::expr(false) << '\n';
}
catch(Error::Zero_divide) {
cerr << "attempt to divide by zero\n";
skip();
}
catch(Error::Syntax_error e) {
cerr << "syntax error:" << e.p << "\n";
skip();
}
}

if (input != &std::cin) delete input;
return no_of_errors;
}
Jul 22 '05 #1
4 5973

"learning_C++" <le********@hotmail.com> wrote in message
news:44*************************@posting.google.co m...
Hi,
I try to use
input = new istrstream(argv[1],strlen(argv[1]));
in my code, but it always says error: " error: parse error before `('
token"

please help me!
Thanks,

#include <map>
#include<iostream>
#include<cctype>
#include<string>


Well one problem is that you have not included the correct header file

#include<strstream>

You might have others, I didn't check.

john
Jul 22 '05 #2
le********@hotmail.com (learning_C++) writes:
Hi,
I try to use
input = new istrstream(argv[1],strlen(argv[1]));
in my code, but it always says error: " error: parse error before `('
token"

please help me!
Thanks,

#include <map>
#include<iostream>
#include<cctype>
#include<string>
[..]
#include <strstream.h>
The header is named sstream (without .h)
int main(int argc, char* argv[])
{
using namespace Driver;

switch (argc) {
case 1: // read from standard input
input = &cin;
break;
case 2: // read argument string
input = new istrstream(argv[1],strlen(argv[1]));

^^^^^^^^^^
the class is named istringstream

Kind regards,
Nicolas

--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pa****@sbox.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Jul 22 '05 #3

"Nicolas Pavlidis" <pa****@sbox.tugraz.at> wrote in message
news:2r************@uni-berlin.de...
le********@hotmail.com (learning_C++) writes:
Hi,
I try to use
input = new istrstream(argv[1],strlen(argv[1]));
in my code, but it always says error: " error: parse error before `('
token"

please help me!
Thanks,

#include <map>
#include<iostream>
#include<cctype>
#include<string>

[..]

#include <strstream.h>


The header is named sstream (without .h)
int main(int argc, char* argv[])
{
using namespace Driver;

switch (argc) {
case 1: // read from standard input
input = &cin;
break;
case 2: // read argument string
input = new istrstream(argv[1],strlen(argv[1]));

^^^^^^^^^^
the class is named istringstream


Huh? istrstream is a perfectly good class. And judging by his usage he wants
istrstream not istringstream.

john
Jul 22 '05 #4

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2r*************@uni-berlin.de...

Huh? istrstream is a perfectly good class.


True, but deprecated.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Cary | last post by:
Trying to install on SuSE 8.2 from source. ../configure --with-apxs=/usr/local/apache/bin/apxs --with-mysql --with-unixODBC=/usr/lib getting this error: /root/php-4.3.2/ext/odbc/php_odbc.c -o...
5
by: Andrew James | last post by:
Gentlemen, I'm running into a problem whilst testing the parsing of a language I've created with TPG . It seems that for some reason, TPG balks when I try to parse an expression whose first...
2
by: nitin | last post by:
g++ -c $INCLUDES ActualPEResult.cpp In file included from /usr/include/c++/3.2.2/backward/iostream.h:31, from /home/pradeepks/Linux_Porting/dcpfrontier/dcpdev/dcp_components/trap...
22
by: Ram Laxman | last post by:
Hi all, I have a text file which have data in CSV format. "empno","phonenumber","wardnumber" 12345,2234353,1000202 12326,2243653,1000098 Iam a beginner of C/C++ programming. I don't know how to...
2
by: Herbert Straub | last post by:
What could be wrong with this simple test program: #include <string> #include <iostream> class foo { std::string s; public: foo (const char *a) : s(a) {};
22
by: nick | last post by:
#include <stdio.h> #define BALANCE 5000 int main(){ int balance = BALANCE; return 0; } when i compile it, an error occurs,what's happen?
0
by: tsivaraman | last post by:
I am trying to build php-5.2.1 in RedHat Linux 9. I have installed libxml2-2.6.11,mysql-5.0.33,httpd-2.2.4(apache) successfully.When i do 'make' from the php directory,i get the following...
1
by: lucasstark | last post by:
I have the following XML File: I want to parse the file so that I can add each node to a database: So I need to parse the bottom node and say: admissions, application. Then go up a level and...
0
by: Anish Chapagain | last post by:
Hi, i tried to compile the swig .i file but am having probel with the error: parse error before % token example.i 1. %module example 2. %{ 3. #include <header.h> 4. %}
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.