getting core dumped, cant figure out how to fix it.

closed account (Dy7SLyTq)
so im getting core dumped from an out of range error, and i cant figure out why

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <regex.h>

using std::ostream;
using std::cout;
using std::cerr;
using std::endl;
using std::cin;
using std::ifstream;
using std::istringstream;
using std::string;
using std::vector;

class Token
{
     string Type, Name;
     int LineNo, LineCol;

     public:
          Token(string type, string name, int lineno, int linecol)
               : Type(type), Name(name), LineNo(lineno), LineCol(linecol) {}
          Token() {}

          void SetType   (string type) { Type    = type;    }
          void SetName   (string name) { Name    = name;    }
          void SetLineNo (int lineno)  { LineNo  = lineno;  }
          void SetLineCol(int linecol) { LineCol = linecol; }

          string GetType   () { return Type;    }
          string GetName   () { return Name;    }
          int    GetLineNo () { return LineNo;  }
          int    GetLineCol() { return LineCol; }
};

ostream& operator<<(ostream &out, Token token)
{
     out<<"("<< token.GetType() <<", '"<< token.GetName() <<"', "<< token.GetLineNo() <<", "<< token.GetLineCol() <<")";
     return out;
}

void  ReadInFile (ifstream&, vector<string>&);
void  Lex        (vector<string>&, vector<Token>&);
Token Lex        (string, int&, int&);

int main(int argc, char *argv[])
{
     ifstream File(argv[1]);
     vector<string> FileContents(1);
     vector<Token> TokenList;

     ReadInFile(File, FileContents);

     Lex(FileContents, TokenList);

     for(auto &Counter : TokenList)
          cout<< Counter << endl;
}

void ReadInFile(ifstream &File, vector<string> &FileContents)
{
     int Counter = 0;

     while(getline(File, FileContents[Counter++]))
          FileContents.resize(FileContents.size() + 1);
}

void Lex(vector<string> &FileContents, vector<Token> &TokenList)
{
     int LineNo = 1, MBegin, MEnd;

     for(auto &Counter : FileContents)
     {
          do
          {
               TokenList.push_back(Lex(Counter, MBegin, MEnd));
               Counter = Counter.substr(MEnd, Counter.size()-MEnd);
               TokenList[TokenList.size() - 1].SetLineNo(LineNo);
          }while(MBegin != Counter.size() - 1);

          LineNo++;
     }
}

Token Lex(string Line, int &MBegin, int &MEnd)
{
     regex_t Regex;
     regmatch_t Match;

     regcomp(&Regex, "\"[^\"]+\"", REG_EXTENDED);
     if(regexec(&Regex, Line.c_str(), 1, &Match, 0) == 0)
     {
               (MBegin = Match.rm_so) && (MEnd = Match.rm_eo);
               return Token("STRING", Line.substr(MBegin + 1, MEnd - MBegin - 2), -1, MBegin + 1);
     }
     regfree(&Regex);
}


edit: the error is with a substring apparently
Last edited on
MBegin and MEnd are uninitialised iif the string doesn't match the regular expression.
closed account (Dy7SLyTq)
ok i fixed that but it didnt work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Lex(vector<string> &FileContents, vector<Token> &TokenList)
{
     int LineNo = 1, MBegin, MEnd;

     for(auto &Counter : FileContents)
     {
          do
          {
               (MBegin = 0) && (MEnd = 0);
               TokenList.push_back(Lex(Counter, MBegin, MEnd));
               Counter = Counter.substr(MEnd, Counter.size() - MEnd - 1);
               TokenList[TokenList.size() - 1].SetLineNo(LineNo);
          }while(MEnd != Counter.size() - 1);

          LineNo++;
     }
}
Have you printed Counter and MEnd before calling substr()?

Don't you think it's a little odd that ReadInFile returns a vector of strings that's one larger than the correct number? Because you (rightly) don't bother to compensate for the extra blank string in Lex.
closed account (Dy7SLyTq)
ah ok so its because FileContents[FileContents.size()-1] = ""?

edit: in ReadInFile i did pop_back() at the end and nothing happened
Last edited on
closed account (Dy7SLyTq)
Thanks for the help. I realized I was modifying memory that doesnt exist so I just need to do a little rewrite
closed account (Dy7SLyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <regex.h>

using std::ostream;
using std::cout;
using std::cerr;
using std::endl;
using std::cin;
using std::ifstream;
using std::istringstream;
using std::string;
using std::vector;

class Token
{
     string Type, Name;
     int LineNo, LineCol;

     public:
          Token(string type, string name, int lineno, int linecol)
               : Type(type), Name(name), LineNo(lineno), LineCol(linecol) {}
          Token() {}

          void SetType   (string type) { Type    = type;    }
          void SetName   (string name) { Name    = name;    }
          void SetLineNo (int lineno)  { LineNo  = lineno;  }
          void SetLineCol(int linecol) { LineCol = linecol; }

          string GetType   () { return Type;    }
          string GetName   () { return Name;    }
          int    GetLineNo () { return LineNo;  }
          int    GetLineCol() { return LineCol; }
};

ostream& operator<<(ostream &out, Token token)
{
     out<<"("<< token.GetType() <<", '"<< token.GetName() <<"', "<< token.GetLineNo() <<", "<< token.GetLineCol() <<")";
     return out;
}

void  ReadInFile (ifstream&, vector<string>&);
void  Lex        (vector<string>&, vector<Token>&);
Token Lex        (string, int&, int&);

int main(int argc, char *argv[])
{
     ifstream File(argv[1]);
     vector<string> FileContents;
     vector<Token> TokenList;

     ReadInFile(File, FileContents);

     Lex(FileContents, TokenList);

     for(auto &Counter : TokenList)
          cout<< Counter << endl;
}

void ReadInFile(ifstream &File, vector<string> &FileContents)
{
     int Counter = 0;
     string Line;

     while(getline(File, Line))
          FileContents.push_back(Line);
}

void Lex(vector<string> &FileContents, vector<Token> &TokenList)
{
     int LineNo = 1, MBegin, MEnd;

     for(auto &Counter : FileContents)
     {
          (MBegin = 0) && (MEnd = Counter.size() - 1);

          while(true)
          {
               TokenList.push_back(Lex(Counter, MBegin, MEnd));

               if(Counter.at(MEnd) == '\n')
               {
                    if(TokenList[TokenList.size() - 1].GetType() == "UNINITIALIZED")
                         TokenList.pop_back();

                    break;
               }

               Counter = Counter.substr(MEnd, Counter.size() - MEnd - 1);
               TokenList[TokenList.size() - 1].SetLineNo(LineNo);
          }

          LineNo++;
     }
}

Token Lex(string Line, int &MBegin, int &MEnd)
{
     regex_t Regex;
     regmatch_t Match;

     regcomp(&Regex, "\"[^\"]+\"", REG_EXTENDED);
     if(regexec(&Regex, Line.c_str(), 1, &Match, 0) == 0)
     {
               (MBegin = Match.rm_so) && (MEnd = Match.rm_eo);
               return Token("STRING", Line.substr(MBegin + 1, MEnd - MBegin - 2), -1, MBegin + 1);
     }
     regfree(&Regex);

     (MBegin = 0) && (MEnd = Line.size() - 1);
     return Token("UNINITIALIZED", "", -1, -1);
}


im still getting core dumped and i cant figure out why. its a problem with one of the substrs in one of the Lex()'s. i think im doing my math wrong somewhere and its making a substr out of string thats not there
Topic archived. No new replies allowed.