LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-18-2014, 02:09 PM   #106
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,421

Original Poster
Blog Entries: 43

Rep: Reputation: 36

I saw a pattern, and just changed my code. iftest now compiles to this:
Code:
VERSION TVOID 0V


VOID 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"
QUIT TVOID 0

END TVOID 0V
 
Old 09-18-2014, 02:13 PM   #107
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,421

Original Poster
Blog Entries: 43

Rep: Reputation: 36
ifelsetest, now compiles to this:
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"
VOID TVOID 0V
; End if or else code :1
GOTOL TSTRING "end_1"


LBL TSTRING "end_1"
; End if :1

END TVOID 0V
It compiles correctly.
 
Old 09-18-2014, 02:18 PM   #108
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,421

Original Poster
Blog Entries: 43

Rep: Reputation: 36
complicatediftest:
Code:
if (3 + 2)
 ;
else if (false)
 ;
compiles to:
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
; End if code :2
GOTOL TSTRING "end_2"


LBL TSTRING "iffalse_or_end_2"
; End if or else code :1
GOTOL TSTRING "end_1"


LBL TSTRING "end_1"
; End if :1

END TVOID 0V

It should compile to:
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
; End if code :2
GOTOL TSTRING "iffalse_or_end_2"


LBL TSTRING "iffalse_or_end_2"
; End if or else code :1
GOTOL TSTRING "end_1"


LBL TSTRING "end_1"
; End if :1

END TVOID 0V
 
Old 09-18-2014, 02:23 PM   #109
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,421

Original Poster
Blog Entries: 43

Rep: Reputation: 36
complicatediftest2:
Code:
if (3 + 2)
 ;
else if (false)
      ;
     else
      ;
compiles to:
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
; End if code :2
GOTOL TSTRING "end_2"


LBL TSTRING "iffalse_or_end_2"
VOID TVOID 0V
; End if or else code :2
GOTOL TSTRING "end_2"


LBL TSTRING "end_2"
; End if :2
; End if or else code :1
GOTOL TSTRING "end_1"


LBL TSTRING "end_1"
; End if :1

END TVOID 0V
It compiles fine.
 
Old 09-18-2014, 02:26 PM   #110
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,421

Original Poster
Blog Entries: 43

Rep: Reputation: 36
complicatediftest3:
Code:
if (3 + 2)
{
 ;
}
else if (false)
{
 ;
}
else
 ;
compiles to:
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
; End if code :2
GOTOL TSTRING "end_2"


LBL TSTRING "iffalse_or_end_2"
VOID TVOID 0V
; End if or else code :2
GOTOL TSTRING "end_2"


LBL TSTRING "end_2"
; End if :2
; End if or else code :1
GOTOL TSTRING "end_1"


LBL TSTRING "end_1"
; End if :1

END TVOID 0V
It compiles fine.
 
Old 09-18-2014, 02:29 PM   #111
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,421

Original Poster
Blog Entries: 43

Rep: Reputation: 36
complicatediftest4:
Code:
if (3 + 2)
{
 ;
}
else if (false)
{
 ;
}
else if (true)
{
 ;
}
else
 ;
compiles to:
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
; End if code :2
GOTOL TSTRING "end_2"


LBL TSTRING "iffalse_or_end_2"
; Begin if :3
ALOAD TBOOLEAN false
CGOTOL TSTRING "iftrue_3"
GOTOL TSTRING "iffalse_or_end_3"


LBL TSTRING "iftrue_3"
VOID TVOID 0V
; End if code :3
GOTOL TSTRING "end_3"


LBL TSTRING "iffalse_or_end_3"
VOID TVOID 0V
; End if or else code :3
GOTOL TSTRING "end_3"


LBL TSTRING "end_3"
; End if :3
; End if or else code :2
GOTOL TSTRING "end_2"


LBL TSTRING "end_2"
; End if :2
; End if or else code :1
GOTOL TSTRING "end_1"


LBL TSTRING "end_1"
; End if :1

END TVOID 0V
compiles fine.
 
Old 09-18-2014, 02:31 PM   #112
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,421

Original Poster
Blog Entries: 43

Rep: Reputation: 36
So one problem is knocked out, which is complicatediftest4, but some still don't compile fine.
 
Old 09-18-2014, 02:44 PM   #113
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,421

Original Poster
Blog Entries: 43

Rep: Reputation: 36
Excuse me. I didn't do a good job of making it clear about iftest. With my changes, it compiles to:
Code:
VERSION TVOID 0V


VOID 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"
QUIT TVOID 0

END TVOID 0V
and should compile to:
Code:
VERSION TVOID 0V


VOID 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 "iffalse_or_end_1"


LBL TSTRING "iffalse_or_end_1"
QUIT TVOID 0

END TVOID 0V
 
Old 09-19-2014, 02:55 AM   #114
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Try to stabilize the code, and when it is done, quote the relevant part of the program and one test-case (the simplest one which shows the problem).
 
Old 09-19-2014, 06:14 PM   #115
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,421

Original Poster
Blog Entries: 43

Rep: Reputation: 36
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.
 
Old 09-19-2014, 06:22 PM   #116
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,421

Original Poster
Blog Entries: 43

Rep: Reputation: 36
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);
     }
    }
   }
  }
 }
 
Old 09-19-2014, 06:26 PM   #117
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,421

Original Poster
Blog Entries: 43

Rep: Reputation: 36
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.
 
Old 09-22-2014, 12:18 PM   #118
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,421

Original Poster
Blog Entries: 43

Rep: Reputation: 36
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);
     }
    }
   }
  }
 }
}
 
Old 09-22-2014, 11:11 PM   #119
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
I don't get what it is... So far, I thought your question is about yacc/bison code-generation, but this is something completely different.
 
Old 09-23-2014, 08:35 AM   #120
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,421

Original Poster
Blog Entries: 43

Rep: Reputation: 36
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.
 
  


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 06:01 PM.

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