ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
It is one line that's off too. I was trying to change that one line in this file, and fix that case. I don't "think" I can do it from within the bison syntax, because it is not smart enough to do conditional actions, such as execute this action if the code is in this form, execute the other action if the code is in that form, which would generate two different code generation cases. I need the whole if, so I cannot test from within the action itself. Therefore this helper function was written to transform the code.
void generate_if_line_numbers()
{
/*
1. Test if the if is just an if statement.
2. If it is just an if, change end_1 to iffalse_or_end_1.
3. Test if the if is an if else ladder with one if-else.
4. If step 3 is true, change end_2 to end_1.
*/
bool iffound = false, ifendfound = false;
String rstring = "\"iffalse_or_end_1\"\n";
string modstring = "";
String modstring2 = "";
Array<String> modstrings;
bool changed = false;
ASTNode node;
unsigned long len = tree.nodenumber();
unsigned long changedindex = 0;
for (unsigned long i = 0; i < out.length(); ++i)
{
String line = out[i];
if (line[0] == ';')
{
int index = line.getString().find("Begin if :1");
if (index != string::npos)
iffound = true;
}
}
for (unsigned long i = 0; i < out.length(); ++i)
{
String line = out[i];
if (line[0] == ';')
{
int index = line.getString().find("End if :1");
if (index != string::npos)
ifendfound = true;
}
}
if (iffound == true && ifendfound == false)
{
for (unsigned long i = 0; i < len; ++i)
{
node.setNode(tree.getNode(i));
String nodename = node.getName();
if (nodename == "if_statementp3")
{
for (unsigned long j = 0; j < node.getValue().length(); ++j)
{
int index = node.getValue()[j].getString().find("\"end_1\"");
if (index != string::npos)
{
modstring = node.getValue()[j].getString();
modstring.erase(index);
modstring.insert(index, rstring.getString().c_str());
modstring2 = modstring;
changed = true;
changedindex = j;
break;
}
}
}
}
}
if (changed == true)
{
for (unsigned long i = 0; i < node.getValue().length(); ++i)
{
if (i != 0)
modstrings.insert();
if (i != changedindex)
modstrings[i] = node.getValue()[i];
else
modstrings[i] = modstring2;
}
node.setValue(modstrings);
tree.setNode("if_statementp3", node);
for (unsigned long i = out.length() - 1; i > 0; --i)
{
out.remove();
--outcount;
}
generate_pcode(false);
}
}
So the problem was that I was previously setting a copy of the string, and not the string which will be used. Now I have to check my other examples.
There is another example that doesn't work, and there are two lines in it. I am hot on the track of fixing that now. Then I will test again to see if I missed any examples...
So now the code is working for the first case, but for the second it's not working. The first problem is that the code is adding extra code and is changing labels names. Here is the generated output:
Code:
VERSION TVOID 0V
; Begin if :1
ALOAD TBOOLEAN false
CGOTOL TSTRING "iftrue_1"
GOTOL TSTRING "iffalse_or_end_1"
LBL TSTRING "iftrue_1"
VOID TVOID 0V
; End if code :1
GOTOL TSTRING "end_1"
LBL TSTRING "iffalse_or_end_1"
; Begin if :2
ALOAD TBOOLEAN false
CGOTOL TSTRING "iftrue_2"
GOTOL TSTRING "iffalse_or_end_2"
LBL TSTRING "iftrue_2"
VOID TVOID 0V
GOTOL TSTRING "end_1"
LBL TSTRING "end_1"
; End if :1
GOTOL TSTRING "end_1"
; End if or else code :1
GOTOL TSTRING "end_1"
LBL TSTRING "end_1"
; End if :1
END TVOID 0V
The code that generates this output is:
Code:
void generate_if_line_numbers()
{
/*
1. Test if the if is just an if statement.
2. If it is just an if, change end_1 to iffalse_or_end_1.
3. Test if the if is an if else ladder with one if-else.
4. If step 3 is true, change end_2 to end_1.
5. If step 3 is true, change iffalse_or_end_2 to end_2.
*/
bool iffound = false, ifendfound = false, if2found = false, if2endfound = false, if3found = false;
String rstring = "";
string modstring = "";
String modstring2 = "";
Array<String> modstrings;
bool changed = false;
ASTNode node;
unsigned long len = tree.nodenumber();
unsigned long changedindex = 0;
// Step 1
for (unsigned long i = 0; i < out.length(); ++i)
{
String line = out[i];
if (line[0] == ';')
{
int index = line.getString().find("Begin if :1");
if (index != string::npos)
iffound = true;
}
}
for (unsigned long i = 0; i < out.length(); ++i)
{
String line = out[i];
if (line[0] == ';')
{
int index = line.getString().find("End if :1");
if (index != string::npos)
ifendfound = true;
}
}
// Step 2
rstring = "\"iffalse_or_end_1\"\n";
if (iffound == true && ifendfound == false && if2found == false && if2endfound == false && if3found == false)
{
for (unsigned long i = 0; i < len; ++i)
{
node.setNode(tree.getNode(i));
String nodename = node.getName();
if (nodename == (String)("if_statementp3_1"))
{
for (unsigned long j = 0; j < node.getValue().length(); ++j)
{
int index = node.getValue()[j].getString().find("\"end_1\"");
if (index != string::npos)
{
modstring = node.getValue()[j].getString();
modstring.erase(index);
modstring.insert(index, rstring.getString().c_str());
modstring2 = modstring;
changed = true;
changedindex = index;
break;
}
}
}
}
}
if (changed == true)
{
for (unsigned long i = 0; i < node.getValue().length(); ++i)
{
if (i != 0)
modstrings.insert();
if (i != changedindex)
modstrings[i] = node.getValue()[i];
else
modstrings[i] = modstring2;
}
node.setValue(modstrings);
tree.setNode((String)("if_statementp3_1"), node);
for (unsigned long i = out.length() - 1; i > 0; --i)
{
out.remove();
--outcount;
}
}
// Step 3
for (unsigned long i = 0; i < out.length(); ++i)
{
String line = out[i];
if (line[0] == ';')
{
int index = line.getString().find("Begin if :2");
if (index != string::npos)
if2found = true;
}
}
for (unsigned long i = 0; i < out.length(); ++i)
{
String line = out[i];
if (line[0] == ';')
{
int index = line.getString().find("End if :2");
if (index != string::npos)
if2endfound = true;
}
}
for (unsigned long i = 0; i < out.length(); ++i)
{
String line = out[i];
if (line[0] == ';')
{
int index = line.getString().find("Begin if :3");
if (index != string::npos)
if3found = true;
}
}
// Step 4
rstring = "\"end_1\"\n";
if (iffound == true && ifendfound == true && if2found == true && if2endfound == false && if3found == false)
{
for (unsigned long i = 0; i < len; ++i)
{
node.setNode(tree.getNode(i));
String nodename = node.getName();
if (nodename == (String)("if_statementp3_2"))
{
for (unsigned long j = 0; j < node.getValue().length(); ++j)
{
int index = node.getValue()[j].getString().find("\"end_2\"");
if (index != string::npos)
{
modstring = node.getValue()[j].getString();
modstring.erase(index);
modstring.insert(index, rstring.getString().c_str());
modstring2 = modstring;
changed = true;
changedindex = index;
cout << modstring2 << endl;
break;
}
}
}
}
}
if (changed == true)
{
for (unsigned long i = 0; i < node.getValue().length(); ++i)
{
if (i != 0)
modstrings.insert();
if (i != changedindex)
modstrings[i] = node.getValue()[i];
else
modstrings[i] = modstring2;
}
node.setValue(modstrings);
tree.setNode((String)("if_statementp3_2"), node);
for (unsigned long i = out.length() - 1; i > 0; --i)
{
out.remove();
--outcount;
}
}
// Step 5
/*
rstring = "\"iffalse_or_end_2\"\n";
if (iffound == true && ifendfound == true && if2found == true && if2endfound == false && if3found == false)
{
for (unsigned long i = 0; i < len; ++i)
{
node.setNode(tree.getNode(i));
String nodename = node.getName();
if (nodename == (String)("if_statementp3_" + ifcounter))
{
for (unsigned long j = 0; j < node.getValue().length(); ++j)
{
int index = node.getValue()[j].getString().find("\"end_2\"");
if (index != string::npos)
{
modstring = node.getValue()[j].getString();
modstring.erase(index);
modstring.insert(index, rstring.getString().c_str());
modstring2 = modstring;
changed = true;
changedindex = j;
break;
}
}
}
}
}
if (changed == true)
{
for (unsigned long i = 0; i < node.getValue().length(); ++i)
{
if (i != 0)
modstrings.insert();
if (i != changedindex)
modstrings[i] = node.getValue()[i];
else
modstrings[i] = modstring2;
}
node.setValue(modstrings);
tree.setNode((String)("if_statementp3_" + ifcounter), node);
for (unsigned long i = out.length() - 1; i > 0; --i)
{
out.remove();
--outcount;
}
}*/
if (changed == true)
generate_pcode(false);
}
I worked on this again, and with some output statements, I think I got it worked out. Here is my algorithm for modifying the code:
Code:
void generate_if_line_numbers()
{
/*
1. Test if the if is just an if statement.
2. If it is just an if, change end_1 to iffalse_or_end_1.
3. Test if the if is an if else ladder with one if-else.
4. If step 3 is true, change end_2 to end_1.
*/
bool iffound = false, ifendfound = false, if2found = false, if2endfound = false, if3found = false;
String rstring = "";
string modstring = "";
String modstring2 = "";
Array<String> modstrings;
bool changed = false;
ASTNode node;
unsigned long len = tree.nodenumber();
unsigned long changedindex = 0;
// Step 1
for (unsigned long i = 0; i < out.length(); ++i)
{
String line = out[i];
if (line[0] == ';')
{
int index = line.getString().find("Begin if :1");
if (index != string::npos)
iffound = true;
}
}
for (unsigned long i = 0; i < out.length(); ++i)
{
String line = out[i];
if (line[0] == ';')
{
int index = line.getString().find("End if :1");
if (index != string::npos)
ifendfound = true;
}
}
// Step 2
rstring = "\"iffalse_or_end_1\"\n";
if (iffound == true && ifendfound == false && if2found == false && if2endfound == false && if3found == false)
{
for (unsigned long i = 0; i < len; ++i)
{
node.setNode(tree.getNode(i));
String nodename = node.getName();
if (nodename == (String)("if_statementp3_1"))
{
for (unsigned long j = 0; j < node.getValue().length(); ++j)
{
int index = node.getValue()[j].getString().find("\"end_1\"");
if (index != string::npos)
{
modstring = node.getValue()[j].getString();
modstring.erase(index);
modstring.insert(index, rstring.getString().c_str());
modstring2 = modstring;
changed = true;
changedindex = index;
break;
}
}
}
}
}
if (changed == true)
{
for (unsigned long i = 0; i < node.getValue().length(); ++i)
{
if (i != 0)
modstrings.insert();
if (i != changedindex)
modstrings[i] = node.getValue()[i];
else
modstrings[i] = modstring2;
}
node.setValue(modstrings);
tree.setNode((String)("if_statementp3_1"), node);
for (unsigned long i = out.length() - 1; i > 0; --i)
{
out.remove();
--outcount;
}
}
// Step 3
for (unsigned long i = 0; i < out.length(); ++i)
{
String line = out[i];
if (line[0] == ';')
{
int index = line.getString().find("Begin if :2");
if (index != string::npos)
if2found = true;
}
}
for (unsigned long i = 0; i < out.length(); ++i)
{
String line = out[i];
if (line[0] == ';')
{
int index = line.getString().find("End if :2");
if (index != string::npos)
if2endfound = true;
}
}
for (unsigned long i = 0; i < out.length(); ++i)
{
String line = out[i];
if (line[0] == ';')
{
int index = line.getString().find("Begin if :3");
if (index != string::npos)
if3found = true;
}
}
// Step 4
unsigned long nodenumber = 0;
rstring = "GOTOL TSTRING \"end_1\"\n";
if (iffound == true && ifendfound == true && if2found == true && if2endfound == false && if3found == false)
{
for (unsigned long i = 0; i < len; ++i)
{
node.setNode(tree.getNode(i));
String nodename = node.getName();
if (nodename == (String)("if_statementp3_2"))
{
nodenumber = i;
for (unsigned long j = 0; j < node.getValue().length(); ++j)
{
int index = node.getValue()[j].getString().find("GOTOL TSTRING \"end_2\"\n");
if (index != string::npos)
{
changedindex = j;
modstring = node.getValue()[j].getString();
modstring.erase(index);
modstring.insert(index, rstring.getString().c_str());
modstring2 = modstring;
changed = true;
break;
}
}
}
}
}
if (changed == true)
{
node.setNode(tree.getNode(nodenumber));
for (unsigned long i = 0; i < node.getValue().length(); ++i)
{
if (i != 0)
modstrings.insert();
if (i != changedindex)
modstrings[i] = node.getValue()[i];
else
modstrings[i] = modstring2;
}
node.setValue(modstrings);
tree.setNode(nodenumber, node);
for (unsigned long i = out.length() - 1; i > 0; --i)
{
out.remove();
--outcount;
}
}
if (changed == true)
generate_pcode(false);
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.