Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7571171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:36:34+00:00 2026-05-30T15:36:34+00:00

So I know this is a pretty common error, but I’m very new to

  • 0

So I know this is a pretty common error, but I’m very new to C++, we are using it for a programing languages class, and this error has me stuck. The delete parser; line is the line that causes this error, and I cannot figure out why.

Turtle2.cpp

#include <iostream> 
#include <sstream> 
#include <string> 
#include <fstream> 
#include "scanner.h" 
#include "parser.h"

using namespace std; 

int main(int argc, char* argv[]) { 
//string line; 
if (argc != 2) { 
    cout << "Usage: turtle filename" << endl; 
    return 0; 
} 
char* filename = argv[1];
cout << "filename is " << filename << endl; 
ifstream in(filename); 
Parser * parser = new Parser(&in); 
AST* tree = parser->parse(); 

tree->evaluate(); 

delete tree; 
delete parser;   //This is the line that causes the error!
return 0; 
} 

parser.cpp

#include "parser.h"
#include "calcex.h"
#include <string>
#include <sstream>

Parser::Parser(istream* in) {
scan = new Scanner(in);
}

Parser::~Parser() {
try {
  delete scan;
} catch (...) {
}
}

AST* Parser::parse() {
AST* returnValue = Prog();
cout << "We are still ok1" << endl;
return returnValue;
}

AST* Parser::Prog() {
AST* result = StmtList();
Token* t = scan->getToken();

if (t->getType() != eof) {
    cout << "Syntax Error: Expected EOF, found token at column " << t->getCol() << endl;
throw ParseError;
}
cout << "We are still ok2" << endl;
return result;
}

AST* Parser::StmtList() {
AST* result = Statement();
Token* t = scan->getToken();
scan->putBackToken();   

if (t->getType() != eof) {
  AST* result = StmtList();
        return result;
}

return result;
}

AST* Parser::Statement() {
float num1;
float num2;
stmtNode* node;
Token* t = scan->getToken();
/*if (turtle->getType() != keyword || turtle->getLex() != "turtle"){
    cerr <<"expected turtle" << endl;  //print to standard error stream
    throw ParseError;
}*/
if (t->getType() == keyword && t->getLex() == "turtle"){
    t = scan->getToken();
    if (t->getType() == dot){
        t = scan->getToken();
        if (t->getType() == keyword && t->getLex() == "goto"){
            t = scan->getToken();
            if (t->getType() == lparen){
                t = scan->getToken();
                if (t->getType() == number){
                    istringstream in(t->getLex());
                    in >> num1;
                    t = scan->getToken();
                    if (t->getType() == comma){
                        t = scan->getToken();
                        if (t->getType() == number){
                            istringstream in(t->getLex());
                            in >> num2;
                            t = scan->getToken();
                            if (t->getType() == rparen){
                                cout << "Good" << endl;
                                node = new stmtNode(num1,num2);
                                cout << "X is " << node->getX() << endl;
                                cout << "Y is " << node->getY() << endl;
                            }
                        }
                    }
                }
            }
        }
    }
}
else{
    cout << "bad" << endl;
}
return node;
}

scanner.cpp

#include "scanner.h"
#include "calcex.h"
#include <iostream>
#include <string>

using namespace std;

//Uncomment this to get debug information
//#define debug

const int numberOfKeywords = 2;

const string keywd[numberOfKeywords] = { //defining the keywords
string("turtle"), string("goto")
};

int isLetter(char c) { //c is bigger or equal to 'a' and small or equal to 'z'. same     for captial versions
return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
}

int isDigit(char c) { //c is bigger or equal to '0' or smaller or equal to '9' 
return (c >= '0' && c <= '9');
}

int isWhiteSpace(char c) { //c is a space, tab or newline
return (c == ' ' || c == '\t' || c == '\n');
}

Scanner::Scanner(istream* in): //scanner constructor
inStream(in),
lineCount(1),
colCount(0),
needToken(true),
lastToken(0)
{}

Scanner::~Scanner() { //scanner de-constructor
try {
  delete inStream;
} catch (...) {}
}

void Scanner::putBackToken() { //method?
needToken = false;
}

Token* Scanner::getToken() { //method?
if (!needToken) {
  needToken=true;
  return lastToken;
}

Token* t;
int state=0;
bool foundOne=false;
char c;
string lex;
TokenType type;
int k;
int column, line;

c = inStream->get();

while (!foundOne) {
  colCount++;
  switch (state) {
     case 0 : 
        lex = "";
        column=colCount;
        line = lineCount;
        if (isLetter(c)) state=1;
        else if (isDigit(c)) state=2;
        else if (c=='.') state=3;
        else if (c==',') state=4;
        else if (c=='(') state=5;
        else if (c==')') state=6;
        else if (c=='\n') {
           colCount=0;
           lineCount++;
        }
        else if (isWhiteSpace(c));
        else if (inStream->eof()) {
           foundOne=true;
           type=eof;
        }
        else {
           cout << "Unrecognized Token found at line " << line <<
              " and column " << column << endl;
           throw UnrecognizedToken;
        }
        break;
     case 1 :
        if (isLetter(c) || isDigit(c)) state=1;
        else {
           for (k=0;k<numberOfKeywords;k++)
              if (lex == keywd[k]) {
                 foundOne = true;
                 type = keyword;
              }
           if (!foundOne) {
              type = identifier;
              foundOne = true;
           }
        }
        break;
     case 2 :
        if (isDigit(c)) state=2;
        else {
           type = number;
           foundOne=true;
        }
        break;
     case 3 :
        type = dot;
        foundOne = true;
        break;
     case 4 :
        type = comma;
        foundOne = true;
        break;
     case 5 :
        type = lparen;
        foundOne=true;
        break;
     case 6 :
        type = rparen;
        foundOne=true;
        break;
  }

  if (!foundOne) {
     lex = lex + c;
     c = inStream->get();
  }
 }

 inStream->putback(c);
 colCount--;
 if (type == number || type == identifier || type == keyword) {
  t = new Token(type,new string(lex),line, column);
 }
 else {
  t = new Token(type,new string(lex),line,column);
 }

 #ifdef debug
 cout << "just found " << lex << " with type " << type << endl;
 #endif

 lastToken = t;
 return t;

}

I don’t know if that is enough code to help you guys out or not, if you need anything more just ask, its not a secret or anything 🙂 I know its the de-constructor for parser, but I have no idea how to fix it, or really why this is happening… I’ve added scanner.cpp, because some ponited out that the problem could be in its deconstructor, so I hope that helps.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:36:35+00:00Added an answer on May 30, 2026 at 3:36 pm

    The reason why this is failing is that you are using delete on something that was allocated on the stack.

    Note the combination of ifstream in(filename);, creating an ifstream on the stack and the delete inStream; in the destructor of Scanner.

    The easiest suggestion would be to allocate in on the heap, whish ifstream *in = new ifstream(filename) or whatnot.

    A better solution would probably be to have the other classes(Scanner, Parser, etc) take the ifstream by reference, and avoid pointers unless they are needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this must be a pretty common problem, but I haven't been able
I'm pretty sure I know the answer to this, but short of a virtual
So I'm pretty sure i already know the answer to this, but does the
I am pretty much new in this. I know Remoting, HTTPService and WebService. I
I do not know how common this problem is for other users, but for
I have this scenario, which I think must be pretty common: class Parameter {
I know this is a pretty basic question, and I think I know the
I know this is a pretty basic regex, could someone explain what it is
Know this might be rather basic, but I been trying to figure out how
I know this might be a no-brainer, but please read on. I also know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.