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.
I have the code stable for now. The whole program isn't working, but the code for the if_statement is pretty stable now. I just had a few bugs in it before, when I changed it's form again. Of all the test cases I'd tried already, the only two so far I found that don't work was iftest, which is a simple if statement with no else, and I forgot which other one, but let's try to first get iftest working. I might have to run the examples to test them, since I forgot which other one doesn't work. Maybe also I checked wrong and there is only one case where it doesn't work. I couldn't find it now anyway.
I have a possible algorithm to do this, and it is as follows:
Code:
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.
I was trying to implement that algorithm, and I couldn't get it to edit right. I think I am using the string functions wrong, but I didn't have Internet where I was at the time, and I couldn't look up the functions because of that. I don't know these functions by heart, and know hardly any function by heart, but I look them up when I need them. I just don't seem to use them enough to know them by heart.
Last edited by des_a; 09-19-2014 at 06:21 PM.
Reason: Posted too soon by mistake.
Assuming my algorithm is right, here is my current code for implementing it:
Code:
bool iffound = false, ifendfound = false;
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)
{
unsigned long len = tree.nodenumber();
for (unsigned long i = 0; i < len; ++i)
{
ASTNode node(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");
cout << index << endl;
if (index != string::npos)
{
String rstring = "iffalse_or_end_1";
node.getValue()[j].getString().erase(index, node.getValue()[j].length());
//node.getValue()[j].getString().insert(index, rstring.getString().c_str());
for (unsigned long k = out.length() - 1; k > 0; --k)
{
out.remove();
--outcount;
}
generate_pcode(false);
}
}
}
}
}
No matter what, I know I am very close to a working solution now. Even if I am about two examples away from working, I am very close. I got closer by looking at post #82 again, and I got closer by using string labels instead of label numbers. It didn't shift the problem, it actually made it simpler for the computer to understand what to do with too.
My generated code is still not perfect, and could be cleaned up a bit, and isn't what I'd generate if I was doing it by hand, but it is equivelant to the effect of what I'd generate by hand, just with a few more useless jumps put in. But it is easiest for the computer to generate it this way, because it is a bit stupid when it comes to understanding how to generate code.
I modified my code a little more. The intent of this code is to erase the offending part of the line, but it's not erasing. What am I doing wrong?
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;
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)
{
unsigned long len = tree.nodenumber();
for (unsigned long i = 0; i < len; ++i)
{
ASTNode node(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)
{
cout << index << endl;
String rstring = "\"iffalse_or_end_1\"\n";
node.getValue()[j].getString().erase(index);
//node.getValue()[j].getString().insert(index, rstring.getString().c_str());
for (unsigned long k = out.length() - 1; k > 0; --k)
{
out.remove();
--outcount;
}
generate_pcode(false);
}
}
}
}
}
}
It is about code generation. The code doesn't generate right still in one or two cases, therefore I am trying to find an algorithm either somehow again in bison/flex, or within C++ code within the flex file (the additional C++ code part), that will transform the code to make those one or two cases work. That was the implementation of the algorithm so far, and I am having trouble with it. Unless it is for some reason a problem with the inner workings of the erase function, but I doubt that. It probably isn't the way I'm calling it anymore either, because I tried to look that up now that I was at home with Internet. But for some reason, the surrounding code has a bug or something in it that makes the erase function not work, or more likely, probably not be called, and that's why it's not working. If I solve this case, I can test again for cases that don't work, and this might be the only case that doesn't work, so I might have solved it. I won't know until I test the cases, but probably this could be the case.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.