LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-23-2014, 08:39 AM   #121
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,426

Original Poster
Blog Entries: 43

Rep: Reputation: 36

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.
 
Old 09-25-2014, 07:13 PM   #122
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,426

Original Poster
Blog Entries: 43

Rep: Reputation: 36
I just got this code debugged. Here is 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;
 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.
 
Old 09-25-2014, 07:51 PM   #123
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,426

Original Poster
Blog Entries: 43

Rep: Reputation: 36
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...
 
Old 09-26-2014, 09:01 PM   #124
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,426

Original Poster
Blog Entries: 43

Rep: Reputation: 36
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);
}
The problem is in step #4.
 
Old 09-26-2014, 09:02 PM   #125
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,426

Original Poster
Blog Entries: 43

Rep: Reputation: 36
I am puzzled, because this should be the same code I used to change the line in the first case. The first case works, but not the second.

---------- Post added 09-26-14 at 07:03 PM ----------

What am I overlooking?

Last edited by des_a; 09-26-2014 at 09:03 PM. Reason: Posted too soon.
 
Old 10-19-2014, 09:42 PM   #126
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,426

Original Poster
Blog Entries: 43

Rep: Reputation: 36
Still working on looking it over...
 
Old 11-13-2014, 09:23 AM   #127
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,426

Original Poster
Blog Entries: 43

Rep: Reputation: 36
I had some other projects to work on before continuing this project.
 
Old 03-14-2015, 10:08 PM   #128
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,426

Original Poster
Blog Entries: 43

Rep: Reputation: 36
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);
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Portable Numbers Format: PNFHA des_a Programming 14 07-18-2014 10:51 PM
[SOLVED] Prime Generation Code Not Working Properly ashok.g Programming 29 12-14-2009 03:59 AM
A GCC code generation problem? dogbird Programming 4 12-09-2005 11:52 AM
tool for code generation? bcalmac Linux - Software 0 08-22-2005 10:41 AM
UML and C++ code generation GŠutama Linux - Software 2 11-13-2003 06:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration