LinuxQuestions.org
Review your favorite Linux distribution.
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 07-20-2014, 11:13 PM   #16
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,417

Original Poster
Blog Entries: 43

Rep: Reputation: 36

I believe I got it to work. However, for an if-else ladder, it won't work.

Here is my code:
Code:
			| IF LEFTP expression RIGHTP stmt ELSE stmt
			{
			 if (strcmp(yytext, "}") == 0)
	                  blockinelse = true;

			 if ($5.to_string().get().getString() == "}")
			  blockinif = true;


			 ifbegin = outcount;
			 if (!inblock)
			  ifbeginlineno = yylineno - 2;
			 else
                          ifbeginlineno = yylineno - 5;

			 conprint("ALOAD TBOOLEAN %s\n", $3.to_boolean().get().getString().c_str());

			 String if_body_str = "if_body_";
			 if_body_str += ifcounter;
			 String else_body_str = "else_body_";
			 else_body_str += ifcounter;
			 String after_if_str = "after_if_";
			 after_if_str += ifcounter;

			 conprint("CGOTOL TVOID %u\n", ifcounter);
			 conprint("GOTOL TVOID %u\n", ifcounter + 1);

			 labelTable.insert();
			 labelTable[labelTable.length() - 1].name(if_body_str);
			 labelTable[labelTable.length() - 1].address(labelTable.length() - 2);

			 labelTable.insert();
			 labelTable[labelTable.length() - 1].name(else_body_str);
			 labelTable[labelTable.length() - 1].address(labelTable.length() - 2);

			 labelTable.insert();
			 labelTable[labelTable.length() - 1].name(after_if_str);
			 labelTable[labelTable.length() - 1].address(labelTable.length() - 2);

			 conprint("\n\nLBL TVOID 0V\n");
			 unsigned long ifbodybegin = out.length() - 1;


			 // Calculate elsebegin...
			 unsigned long k = outcount;
			 unsigned long l = outcount;
			 unsigned long m;
			 for (unsigned long i = 0; i < statementoutcount.length(); ++i)
			 {
			  if (statementlineno[i] >= ifbeginlineno)
			  {
			   unsigned long j;
			   for (j = statementoutcount[i]; j < ifbegin; ++j, ++k)
 			    ;
			   outcount = k;
			   elsebeginlineno = j + 1;
			  }
			 }
			 elsebegin = outcount;


			 bool found = false;
			 bool multiplestmts = false;
			 unsigned long l2 = 0;
			 unsigned long n = outcount;


			 unsigned long fpos = ifbegin - 1;


			 // Calculate l2...
			 n = outcount;
			 found = false;
			 bool elsefound = false;
			 multiplestmts = false;
			 for (unsigned long i = 0; i < ifbegin; ++i)
			 {		  
			  ++l2;
			 }


			 // Mark the code...
			 for (unsigned long i = 0, m = statementlineno[i]; i < out.length(); ++i, ++m)
		         {
			  if (i < ifbegin)
			  {
                           out[i].insert(out[i].length(), ':');
			   out[i].insert();
			   String str;
			   str.from_long(m);
			   out[i] += str;
  		           out[i] += '\n';
			  }
			 }


			 // Calculate some other values...
			 n = outcount;
			 unsigned long len = out.length();
			 found = false;
			 elsefound = false;
			 multiplestmts = false;
			 for (unsigned long i = 0; i < len; ++i)
			 {
			  String str;
			  str = out[i];
			  unsigned long k = str.getString().find(':');
                          if (k == string::npos)
			  {
                           continue;
		          }
   		          found = true;
			  str = out[i].getString().substr(k + 2);
			  unsigned long l = atoi(str.getString().c_str());
			  
			  if (l2 > 1)
			   multiplestmts = true;

			  if (blockinelse == true && multiplestmts == true)
			  {
			   if (l >= (elsebeginlineno - 1))
                           {
			    elsefound = true;
			   }
			  }
			  else
			  {
			   if (l >= elsebeginlineno)
                           {
			    elsefound = true;
			   }
			  }
			 }
			 outcount = out.length() - 1;


			 // Are there multiple statements in the if?
			 bool multipleif = false;
			 unsigned long ifstmts = elsebegin - ifbegin - 1;
			 if (ifstmts > 1)
			  multipleif = true;


			 // Adjust ifsmts...
			 bool nvalues = false;
			 unsigned long ifstmtoffset = 0;
			 if (ifstmts == 19)
			 {
			  nvalues = ifbegin == 10 && elsebegin == 30 ? true : false;
			  if (!nvalues)
			  {
			   ifstmtoffset = ifbegin - 10;
			   ifstmts += ifstmtoffset;
			  }
			 }


			 // After this point, these are off, so we adjust them...
			 if (l2 == 4 && !blockinif)
			 {
			  ifbeginlineno -= 1;
			  elsebeginlineno -= 2;
			 }
			 else if (l2 == 3 && !blockinif)
			 {
			  elsebeginlineno -= 1;
			 }
			 else if (l2 >= 5 && !blockinif)
			 {
			  unsigned long l2offset = 0;
			  unsigned long i;
			  for (i = 6; i <= l2; ++i)
                           ++l2offset;

			  ifbeginlineno -= (2 + l2offset);
			  elsebeginlineno -= (3 + l2offset);
			 }
			 else if (l2 == 2 && blockinif)
			 {
			  ++elsebeginlineno;
			 }
			 else if (l2 == 4 && blockinif)
			 {
			  --ifbeginlineno;
			  --elsebeginlineno;
		         }
			 else if (l2 == 5 && blockinif)
			 {
			  ifbeginlineno -= 2;
			  elsebeginlineno -= 2;
		         }
			 else if (l2 >= 6 && blockinif)
			 {
			  unsigned long l2offset = 3;
			  unsigned long i;
			  for (i = 7; i <= l2; ++i)
                          {
			   ++l2offset;
			  }

			  ifbeginlineno -= (l2offset);
			  elsebeginlineno -= (l2offset);

			  if (ifstmts == 19)
			   elsebeginlineno += 1;
			  if (ifstmts == 20)
			   elsebeginlineno += 2;
			  if (ifstmts == 21)
			   elsebeginlineno += 3;
			  if (ifstmts >= 22)
			  {
			   unsigned long ifstmtoffset2offset = ifstmts - 22;
			   unsigned long ifstmtoffset2 = 4 + ifstmtoffset2offset;

			   elsebeginlineno += ifstmtoffset2;
			  }
			 }


			 // Count the statements before the if code...
			 unsigned long bifstmts = 0;
			 len = out.length();
			 found = false;
			 for (unsigned long i = 0; i < len; ++i)
			 {
			  String str;
			  str = out[i];
			  unsigned long k = str.getString().find(':');
                          if (k == string::npos)
			  {
                           continue;
		          }
   		          found = true;
			  str = out[i].getString().substr(k + 2);
			  unsigned long l = atoi(str.getString().c_str());
			  
			  if (l < ifbeginlineno)
			   ++bifstmts;
			 }
			 bool mbifstmts = bifstmts > 1 ? true : false;	// Precaution...
			 

			 if (ifbegin == 10 && elsebegin == 26 && mbifstmts)
			  ++elsebeginlineno;
			 else if (ifbegin == 11 && elsebegin == 27 && mbifstmts)
			 {
			  ++bifstmts;
			  elsebeginlineno += 4;
			 }
			 else if (ifbegin == 12 && elsebegin == 28 && mbifstmts)
			 {
			  bifstmts += 2;
			  elsebeginlineno += 3;
			 }
			 else if ((ifbegin - 13) >= 0 && (elsebegin - 29) >= 0 && mbifstmts)
			 {
			  bifstmts += (3 + (ifbegin - 13));
			  elsebeginlineno += (4 + (ifbegin - 13));
			 }
			

		         // Move the if code to the right spot...
			 k = ifbodybegin;
			 for (unsigned long i = bifstmts; i < statementoutcount.length(); ++i)
			 {
			  for (unsigned long j = statementoutcount[i]; j < ifbegin; ++j, ++k)
 			   out.move(j, k);
			  outcount = k;			 
			 }


			 // Conditionally adjust values in some cases...
			 unsigned long ifbeginoffset = 11, elsebeginoffset = 31;
			 if (ifbegin == 13 && elsebegin == 33)
			 {
 			  // Recalculate ifstmts...
			  ifstmts = 0;
			  for (unsigned long i = ifbodybegin; i <= elsebegin; ++i)
			   ++ifstmts;
			  if (ifstmts > 1)
			   multipleif = true;


			  // Recalculate elsebeginlineno...
			  if (l2 == 4 && !blockinif)
			  {

 			  }
			  else if (l2 == 3 && !blockinif)
			  {

			  }
			  else if (l2 >= 5 && !blockinif)
			  {
			   unsigned long l2offset = 0;
			   unsigned long i;
			   for (i = 6; i <= l2; ++i)
                            ++l2offset;
			  }
			  else if (l2 == 2 && blockinif)
			  {

			  }
			  else if (l2 == 4 && blockinif)
			  {

		          }
			  else if (l2 == 5 && blockinif)
			  {
			 
		          }
			  else if (l2 >= 6 && blockinif)
			  {
			   unsigned long l2offset = 3;
			   unsigned long i;
			   for (i = 7; i <= l2; ++i)
                           {
			    ++l2offset;
			   }

			   if (ifstmts == 17)
			    elsebeginlineno += 1;
			  }
			 }
			 else if (ifbegin == 12 && elsebegin == 32)
			 {
 			  // Recalculate ifstmts...
			  ifstmts = 0;
			  for (unsigned long i = ifbodybegin; i <= elsebegin; ++i)
			   ++ifstmts;
			  if (ifstmts > 1)
			   multipleif = true;


			  // Recalculate elsebeginlineno...
			  if (l2 == 4 && !blockinif)
			  {

 			  }
			  else if (l2 == 3 && !blockinif)
			  {

			  }
			  else if (l2 >= 5 && !blockinif)
			  {
			   unsigned long l2offset = 0;
			   unsigned long i;
			   for (i = 6; i <= l2; ++i)
                            ++l2offset;
			  }
			  else if (l2 == 2 && blockinif)
			  {

			  }
			  else if (l2 == 4 && blockinif)
			  {

		          }
			  else if (l2 == 5 && blockinif)
			  {
			 
		          }
			  else if (l2 >= 6 && blockinif)
			  {
			   unsigned long l2offset = 3;
			   unsigned long i;
			   for (i = 7; i <= l2; ++i)
                           {
			    ++l2offset;
			   }

			   if (ifstmts == 17)
			    elsebeginlineno += 2;
			  }
			 }
			 else if ((ifbeginoffset - ifbegin) >= 0 && (elsebeginoffset - elsebegin) >= 0)
			 {
			  elsebeginlineno += (3 + (11 - ifbeginoffset));
			  if ((ifbeginoffset - ifbegin) >= 1)
 			   elsebeginlineno += 2;
			 }


			 conprint("GOTOL TVOID %u\n", ifcounter + 2);
			 statementoutcount.insert();
			 statementoutcount[statementoutcount.length() - 1] = outcount - 1;
			 statementlineno.insert();
			 statementlineno[statementlineno.length() - 1] = elsebeginlineno;


			 conprint("\n\nLBL TVOID 0V\n");
			 statementoutcount.insert();
			 statementoutcount[statementoutcount.length() - 1] = outcount - 1;
			 statementlineno.insert();
			 statementlineno[statementlineno.length() - 1] = elsebeginlineno;

			 unsigned long elsebodybegin = out.length();

			 
			 // Add the else code to an array...
			 n = outcount;
			 len = out.length();
			 found = false;
			 elsefound = false;
			 multiplestmts = false;
			 l2 = 0;
			 Array<String> a;
			 for (unsigned long i = 0; i < len; ++i)
			 {
			  String str;
			  str = out[i];
			  unsigned long k = str.getString().find(':');
                          if (k == string::npos)
			  {
                           continue;
		          }
   		          found = true;
			  str = out[i].getString().substr(k + 2);
			  unsigned long l = atoi(str.getString().c_str());
			  
			  ++l2;
			  if (l2 > 1)
			   multiplestmts = true;

			  if (l >= elsebeginlineno)
			  {
			   if (l2 > 1)
			    a.insert();
			   a[a.length() - 1] = out[i];
			   out[i] = "";
                          }
			 }
			 // Sort it by line number...
			 sort_lines(a);

			
			 // ...And insert it back into the output...
	                 for (unsigned long i = 0; i < a.length(); ++i)
                         {
			  out.insert();
			  out[elsebodybegin + i] = a[i];
			 }
			 outcount = out.length();


			 conprint("GOTOL TVOID %u\n", ifcounter + 2);


			 conprint("\n\nLBL TVOID 0V\n");

			
			 // Remove the extra line number code...
 			 for (unsigned long i = 0; i < out.length(); ++i)
			 {
			  unsigned long pos = out[i].getString().find(':');
			  String str;

			  if (pos != string::npos)
			   str = out[i].getString().substr(0, pos);
			  else
			   str = out[i];

   			  out[i] = str;
			 }


			 ++ifcounter;
			}
 
Old 07-20-2014, 11:44 PM   #17
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,417

Original Poster
Blog Entries: 43

Rep: Reputation: 36
The specific code I'm now trying to generate code for is:

Code:
if (3 + 2)
 ;
else if (false)
 ;
 
Old 07-21-2014, 01:39 PM   #18
pwalden
Member
 
Registered: Jun 2003
Location: Washington
Distribution: Raspbian, Ubuntu, Chrome/Crouton
Posts: 374

Rep: Reputation: 50
Quote:
Originally Posted by des_a View Post
Do you mean the location tracking feature? Or if not, what feature are you talking about? Can you give me an overview of the feature you are talking about please?
I had to dig out my old 1984 copy of K&P and type.

Section 8.1 example:
Quote:
"an action is a sequence of C statements enclosed in braces { and}. Within an action, $n (that is, $1, $2, etc.) refers to the value returned by the n-th component of the rule, and $$ is the value to be returned as the value of the whole rule."
Now that I have refreshed by memory of yacc, correcting my previous pseudo-yacc-code below:
Code:
%union {
   int index;
   char *str;
   ..etc..
}

%type <index> begin_block
%type <index> end_block
%type <index> expression
%type <index> stmt

%%

begin_block: LEFTP { $$ = statementoutcount.length(); }

end_block: RIGHTP { $$ = statementoutcount.length(); }

if_statement: IF begin_block expression end_block stmt {
   start_position = $2; end_position = $4; ...; $$ = $1; }
You may have gotten one if-else statement to work, but I think you will find a never-ending mess trying to get every situation to work with the use of yylineno. The use of $$ and value returns is the yacc way to do it right.
 
Old 07-21-2014, 10:25 PM   #19
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
The p-code that you output should not have a design that is dependent upon source-line numbers from the original parse.

Instead, you should generate a list of forward-jump targets that will be "fixed up" with the correct location when the instructions that are being forward-jumped to are actually generated. The p-code instructions are initially generated with an invalid target-address ... one that the runtime interpreter will immediately identify as "indicative of a bug in the compiler" if it ever shows up. An internal list is built of all of those instructions that need to be patched. Then, at some future point in time, when the correct target address has been determined, the compiler goes back and inserts the correct address. (It verifies that each instruction is, in fact, a jump-type instruction, and that its target address is currently "invalid," both as they should be. If ever somehow they are not, it croaks, having detected a bug in the compiler itself.)

Also, be careful not to mix code-generation too closely together with parsing. Usually, the parser generates data-structures (ASTs = Abstract-Syntax Trees; or portions thereof) which correspond to the construct that is now being recognized. At significant points, these structures are processed and consumed. You are not doing a recursive-descent compiler. The direct output of the parser is not "code." The ordering of calls to the back-end routines is also not significant.

Sequences such as an empty-statement ";", for example, are ... "nothing at all." They don't have any corresponding node in the AST, because they are merely a syntactic artifact having no abstract meaning. So, the compiler does not generate anything for them.

Last edited by sundialsvcs; 07-22-2014 at 09:47 AM.
 
1 members found this post helpful.
Old 07-29-2014, 11:30 PM   #20
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,417

Original Poster
Blog Entries: 43

Rep: Reputation: 36
Also see: http://stackoverflow.com/questions/2...ode-generation
 
Old 07-29-2014, 11:33 PM   #21
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,417

Original Poster
Blog Entries: 43

Rep: Reputation: 36
...Actually, my compiler generates a VOID instruction for a ';' as a statement in this language...
 
Old 07-30-2014, 02:56 AM   #22
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
The core of the problem is that you're trying to use yacc without proper understanding. That's why you should first master hoc.
 
Old 07-30-2014, 08:59 AM   #23
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
There's a compelling reason why compilers in the real world are designed in the way that they are ... and why "even BASIC" does not use "line numbers" anymore.

There is copious documentation out there about how production languages ... gcc, Perl, Python, Ruby, haXe, and so on ... have been implemented. There are also "compiler generators" which implement a lot of this stuff for you. "Go and do likewise."

The general structure, which is well-tested and battle-scarred, is this:
  1. The source-code is fed through a lexer and a parser.
  2. The parser generates an Abstract-Syntax Tree (AST) data-structure in memory, and invokes the "back end" compiler routines at strategic moments.
  3. The "back end" routines take the current AST (fragment ...), traverse and manipulate it, and use it to generate another, lower-level data structure that will correspond to instruction-sequences, memory-layouts and so on.
  4. These data-structures are then used either to generate machine-code, or as a driver for a runtime interpreter system. (Runtime interpreters are often as-efficient, if not more so, than native machine-code generation in many practical instances, because you can optimize-the-hell out of the runtime's implementation, do that once, and reap the benefits forever.)
There's a lot of "noise" in source-code, for the benefit of humans. The AST representation "abstracts away" this now-unnecessary detail. (It has no further reference to "line numbers.") And, as noted, even it is never obliged to represent "the work-in-progress that we are actually generating," which is logically a separate apple/orange and so is distinctly represented within the overall compiler system.

The secret to quickly building a reliable (and serviceable ...) compiler is: to "do as the Romans do," and to use the same labor-saving tools that they use, and in the appointed way. Decades of experience (and successes) have gone into this body of knowledge.

Yes, I've also built a recursive-descent compiler ... made a lot of money from it, in fact, and I still do ... but it's not the way that I would do it again, and it's not the way that I did it in other parts of the same system.

Last edited by sundialsvcs; 07-30-2014 at 09:01 AM.
 
Old 07-30-2014, 01:03 PM   #24
ChrisDodd
LQ Newbie
 
Registered: Feb 2006
Posts: 3

Rep: Reputation: 1
How to write a parser...

The basic problem you are running into is that yacc/bison are bottom up parsers, so run the actions on your rules in a bottom up manner. That means it runs the IF..ELSE rule *after* the rules for the statements in the IF..ELSE, and if you are outputting code directly in those rules, it will come out in the wrong order. One fix is to NOT output code directly; rather the code generated should be put in a dynamically created buffer and returned up the parse tree as a generated value (store in $$ and then read from $n in the higher rule). That works but requires a lot of dynamic buffer management and cleanup.

An alternative if you just want to generate a little bit of code (such as a branch or label) before the code for some piece, and then some more code after, is to use an embedded action. The embedded action becomes a notional epsilon rule that will be reduced at that point in the parse (before stuff that comes after it). So you end up with something that looks like:

Code:
| IF LEFTP expression
    { $$ = ++UniqueLabel;
      printf("CGOTO L%d\n", $$); }
  RIGHTP stmt
    { $$ = ++UniqueLabel;
      printf("GOTO L%d\n", $$);
      printf("LBL L%d\n", $4); }
  ELSE stmt
    { printf("LBL L%d\n", $7); }
This contains two embedded actions; the first emits a CGOTO after the code that computes the condition and before the code for the first statement. The second comes between the two statements. The final action is the action for this rule, which just emits the final label.

An unrelated issue is with accessing yytext/yylineno directly in the parser. Since yacc/bsion use one token lookahead (but not in all cases), you can't reliably look at the 'current' token or linenumber in the parser -- you might be looking at either the last token in the current rule or the first token in the next rule. There's no good way to know, and it might change if you modify seemingly unrelated rules. So don't use yytext directly in the parser at all, and be aware that if you print messages that refer to yylineno, they might actually be referring to the next line...
 
1 members found this post helpful.
Old 07-30-2014, 02:36 PM   #25
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,417

Original Poster
Blog Entries: 43

Rep: Reputation: 36
sundialsvcs, please tell me more about a good general way to design compilers. I'd like more detail about those intermediate structures. It might take a redesign, but if the code works better, I'd be all for it. Please point me to references and tutorials where they might generate a real-world compiler or two and you could use the example source code with explanations in your code. I did go through the bison and flex tutorials and one point, but they just show the basics of the language. They don't show more about actual compiler design. The design of the other programs is working, so I will keep them the same, but this one might need to be redesigned.

What I had done is gone from directly outputting the code to outputting it into an array, and then generating the code from an array. This allowed me to do the manipulation to get this far. But maybe my structure needs to be different so I can generate code better. What I want to avoid if possible, however, is having more than one binary file have to be called or more than one parser to generate the code for this if possible. The user should just see input as the language, and output as the generated code. Whatever the program does to do this is okay, but the user should just have to see it's input and output.

If needed I could provide some documentation on the lower level formats, including the one I'm generating the code for...

---------- Post added 07-30-14 at 12:37 PM ----------

For now, while I'm waiting, I will try to implement things using ChrisDodd's suggestion...
 
Old 07-30-2014, 08:48 PM   #26
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,417

Original Poster
Blog Entries: 43

Rep: Reputation: 36
When I split the code up as follows:

Code:
			| IF LEFTP expression
			{
			 conprint("b\n");
			}
			  RIGHTP stmt
			{
			 conprint("a\n");
			 /*
			 ifbegin = outcount;
			 if (!inblock)
			  ifbeginlineno = yylineno - 2;
			 else
                          ifbeginlineno = yylineno - 5;

			 conprint("ALOAD TBOOLEAN %s\n", $3.to_boolean().get().getString().c_str());

			 String if_body_str = "if_body_";
			 if_body_str += ifcounter;
			 String after_if_str = "after_if_";
			 after_if_str += ifcounter;

			 conprint("CGOTOL TVOID %u\n", ifcounter);
			 conprint("GOTOL TVOID %u\n", ifcounter + 1);

			 labelTable.insert();
			 labelTable[labelTable.length() - 1].name(if_body_str);
			 labelTable[labelTable.length() - 1].address(labelTable.length() - 2);

			 labelTable.insert();
			 labelTable[labelTable.length() - 1].name(after_if_str);
			 labelTable[labelTable.length() - 1].address(labelTable.length() - 2);

			 conprint("\n\nLBL TVOID 0V\n");

			 unsigned long k = outcount;
			 for (unsigned long i = 0; i < statementoutcount.length(); ++i)
			 {
			  if (statementlineno[i] >= ifbeginlineno)
			  {
			   for (unsigned long j = statementoutcount[i]; j < ifbegin; ++j, ++k)
 			    out.move(j, k);
			   outcount = k;
			  }
			 }
			 
			 conprint("\n\nLBL TVOID 0V\n");
			 */
			}	  
			  ELSE stmt
			{
			 conprint("b\n");
			 /*
			 if (strcmp(yytext, "}") == 0)
	                  blockinelse = true;

			 if ($5.to_string().get().getString() == "}")
			  blockinif = true;


			 ifbegin = outcount;
			 if (!inblock)
			  ifbeginlineno = yylineno - 2;
			 else
                          ifbeginlineno = yylineno - 5;

			 conprint("ALOAD TBOOLEAN %s\n", $3.to_boolean().get().getString().c_str());

			 String if_body_str = "if_body_";
			 if_body_str += ifcounter;
			 String else_body_str = "else_body_";
			 else_body_str += ifcounter;
			 String after_if_str = "after_if_";
			 after_if_str += ifcounter;

			 conprint("CGOTOL TVOID %u\n", ifcounter);
			 conprint("GOTOL TVOID %u\n", ifcounter + 1);

			 labelTable.insert();
			 labelTable[labelTable.length() - 1].name(if_body_str);
			 labelTable[labelTable.length() - 1].address(labelTable.length() - 2);

			 labelTable.insert();
			 labelTable[labelTable.length() - 1].name(else_body_str);
			 labelTable[labelTable.length() - 1].address(labelTable.length() - 2);

			 labelTable.insert();
			 labelTable[labelTable.length() - 1].name(after_if_str);
			 labelTable[labelTable.length() - 1].address(labelTable.length() - 2);

			 conprint("\n\nLBL TVOID 0V\n");
			 unsigned long ifbodybegin = out.length() - 1;


			 // Calculate elsebegin...
			 unsigned long k = outcount;
			 unsigned long l = outcount;
			 unsigned long m;
			 for (unsigned long i = 0; i < statementoutcount.length(); ++i)
			 {
			  if (statementlineno[i] >= ifbeginlineno)
			  {
			   unsigned long j;
			   for (j = statementoutcount[i]; j < ifbegin; ++j, ++k)
 			    ;
			   outcount = k;
			   elsebeginlineno = j + 1;
			  }
			 }
			 elsebegin = outcount;


			 bool found = false;
			 bool multiplestmts = false;
			 unsigned long l2 = 0;
			 unsigned long n = outcount;


			 unsigned long fpos = ifbegin - 1;


			 // Calculate l2...
			 n = outcount;
			 found = false;
			 bool elsefound = false;
			 multiplestmts = false;
			 for (unsigned long i = 0; i < ifbegin; ++i)
			 {		  
			  ++l2;
			 }


			 // Mark the code...
			 for (unsigned long i = 0, m = statementlineno[i]; i < out.length(); ++i, ++m)
		         {
			  if (i < ifbegin)
			  {
                           out[i].insert(out[i].length(), ':');
			   out[i].insert();
			   String str;
			   str.from_long(m);
			   out[i] += str;
  		           out[i] += '\n';
			  }
			 }


			 // Calculate some other values...
			 n = outcount;
			 unsigned long len = out.length();
			 found = false;
			 elsefound = false;
			 multiplestmts = false;
			 for (unsigned long i = 0; i < len; ++i)
			 {
			  String str;
			  str = out[i];
			  unsigned long k = str.getString().find(':');
                          if (k == string::npos)
			  {
                           continue;
		          }
   		          found = true;
			  str = out[i].getString().substr(k + 2);
			  unsigned long l = atoi(str.getString().c_str());
			  
			  if (l2 > 1)
			   multiplestmts = true;

			  if (blockinelse == true && multiplestmts == true)
			  {
			   if (l >= (elsebeginlineno - 1))
                           {
			    elsefound = true;
			   }
			  }
			  else
			  {
			   if (l >= elsebeginlineno)
                           {
			    elsefound = true;
			   }
			  }
			 }
			 outcount = out.length() - 1;


			 // Are there multiple statements in the if?
			 bool multipleif = false;
			 unsigned long ifstmts = elsebegin - ifbegin - 1;
			 if (ifstmts > 1)
			  multipleif = true;


			 // Adjust ifsmts...
			 bool nvalues = false;
			 unsigned long ifstmtoffset = 0;
			 if (ifstmts == 19)
			 {
			  nvalues = ifbegin == 10 && elsebegin == 30 ? true : false;
			  if (!nvalues)
			  {
			   ifstmtoffset = ifbegin - 10;
			   ifstmts += ifstmtoffset;
			  }
			 }


			 // After this point, these are off, so we adjust them...
			 if (l2 == 4 && !blockinif)
			 {
			  ifbeginlineno -= 1;
			  elsebeginlineno -= 2;
			 }
			 else if (l2 == 3 && !blockinif)
			 {
			  elsebeginlineno -= 1;
			 }
			 else if (l2 >= 5 && !blockinif)
			 {
			  unsigned long l2offset = 0;
			  unsigned long i;
			  for (i = 6; i <= l2; ++i)
                           ++l2offset;

			  ifbeginlineno -= (2 + l2offset);
			  elsebeginlineno -= (3 + l2offset);
			 }
			 else if (l2 == 2 && blockinif)
			 {
			  ++elsebeginlineno;
			 }
			 else if (l2 == 4 && blockinif)
			 {
			  --ifbeginlineno;
			  --elsebeginlineno;
		         }
			 else if (l2 == 5 && blockinif)
			 {
			  ifbeginlineno -= 2;
			  elsebeginlineno -= 2;
		         }
			 else if (l2 >= 6 && blockinif)
			 {
			  unsigned long l2offset = 3;
			  unsigned long i;
			  for (i = 7; i <= l2; ++i)
                          {
			   ++l2offset;
			  }

			  ifbeginlineno -= (l2offset);
			  elsebeginlineno -= (l2offset);

			  if (ifstmts == 19)
			   elsebeginlineno += 1;
			  if (ifstmts == 20)
			   elsebeginlineno += 2;
			  if (ifstmts == 21)
			   elsebeginlineno += 3;
			  if (ifstmts >= 22)
			  {
			   unsigned long ifstmtoffset2offset = ifstmts - 22;
			   unsigned long ifstmtoffset2 = 4 + ifstmtoffset2offset;

			   elsebeginlineno += ifstmtoffset2;
			  }
			 }


			 // Count the statements before the if code...
			 unsigned long bifstmts = 0;
			 len = out.length();
			 found = false;
			 for (unsigned long i = 0; i < len; ++i)
			 {
			  String str;
			  str = out[i];
			  unsigned long k = str.getString().find(':');
                          if (k == string::npos)
			  {
                           continue;
		          }
   		          found = true;
			  str = out[i].getString().substr(k + 2);
			  unsigned long l = atoi(str.getString().c_str());
			  
			  if (l < ifbeginlineno)
			   ++bifstmts;
			 }
			 bool mbifstmts = bifstmts > 1 ? true : false;	// Precaution...
			 

			 if (ifbegin == 10 && elsebegin == 26 && mbifstmts)
			  ++elsebeginlineno;
			 else if (ifbegin == 11 && elsebegin == 27 && mbifstmts)
			 {
			  ++bifstmts;
			  elsebeginlineno += 4;
			 }
			 else if (ifbegin == 12 && elsebegin == 28 && mbifstmts)
			 {
			  bifstmts += 2;
			  elsebeginlineno += 3;
			 }
			 else if ((ifbegin - 13) >= 0 && (elsebegin - 29) >= 0 && mbifstmts)
			 {
			  bifstmts += (3 + (ifbegin - 13));
			  elsebeginlineno += (4 + (ifbegin - 13));
			 }
			

		         // Move the if code to the right spot...
			 k = ifbodybegin;
			 for (unsigned long i = bifstmts; i < statementoutcount.length(); ++i)
			 {
			  for (unsigned long j = statementoutcount[i]; j < ifbegin; ++j, ++k)
 			   out.move(j, k);
			  outcount = k;			 
			 }


			 // Conditionally adjust values in some cases...
			 unsigned long ifbeginoffset = 11, elsebeginoffset = 31;
			 if (ifbegin == 13 && elsebegin == 33)
			 {
 			  // Recalculate ifstmts...
			  ifstmts = 0;
			  for (unsigned long i = ifbodybegin; i <= elsebegin; ++i)
			   ++ifstmts;
			  if (ifstmts > 1)
			   multipleif = true;


			  // Recalculate elsebeginlineno...
			  if (l2 == 4 && !blockinif)
			  {

 			  }
			  else if (l2 == 3 && !blockinif)
			  {

			  }
			  else if (l2 >= 5 && !blockinif)
			  {
			   unsigned long l2offset = 0;
			   unsigned long i;
			   for (i = 6; i <= l2; ++i)
                            ++l2offset;
			  }
			  else if (l2 == 2 && blockinif)
			  {

			  }
			  else if (l2 == 4 && blockinif)
			  {

		          }
			  else if (l2 == 5 && blockinif)
			  {
			 
		          }
			  else if (l2 >= 6 && blockinif)
			  {
			   unsigned long l2offset = 3;
			   unsigned long i;
			   for (i = 7; i <= l2; ++i)
                           {
			    ++l2offset;
			   }

			   if (ifstmts == 17)
			    elsebeginlineno += 1;
			  }
			 }
			 else if (ifbegin == 12 && elsebegin == 32)
			 {
 			  // Recalculate ifstmts...
			  ifstmts = 0;
			  for (unsigned long i = ifbodybegin; i <= elsebegin; ++i)
			   ++ifstmts;
			  if (ifstmts > 1)
			   multipleif = true;


			  // Recalculate elsebeginlineno...
			  if (l2 == 4 && !blockinif)
			  {

 			  }
			  else if (l2 == 3 && !blockinif)
			  {

			  }
			  else if (l2 >= 5 && !blockinif)
			  {
			   unsigned long l2offset = 0;
			   unsigned long i;
			   for (i = 6; i <= l2; ++i)
                            ++l2offset;
			  }
			  else if (l2 == 2 && blockinif)
			  {

			  }
			  else if (l2 == 4 && blockinif)
			  {

		          }
			  else if (l2 == 5 && blockinif)
			  {
			 
		          }
			  else if (l2 >= 6 && blockinif)
			  {
			   unsigned long l2offset = 3;
			   unsigned long i;
			   for (i = 7; i <= l2; ++i)
                           {
			    ++l2offset;
			   }

			   if (ifstmts == 17)
			    elsebeginlineno += 2;
			  }
			 }
			 else if ((ifbeginoffset - ifbegin) >= 0 && (elsebeginoffset - elsebegin) >= 0)
			 {
			  elsebeginlineno += (3 + (11 - ifbeginoffset));
			  if ((ifbeginoffset - ifbegin) >= 1)
 			   elsebeginlineno += 2;
			 }


			 conprint("GOTOL TVOID %u\n", ifcounter + 2);
			 statementoutcount.insert();
			 statementoutcount[statementoutcount.length() - 1] = outcount - 1;
			 statementlineno.insert();
			 statementlineno[statementlineno.length() - 1] = elsebeginlineno;


			 conprint("\n\nLBL TVOID 0V\n");
			 statementoutcount.insert();
			 statementoutcount[statementoutcount.length() - 1] = outcount - 1;
			 statementlineno.insert();
			 statementlineno[statementlineno.length() - 1] = elsebeginlineno;

			 unsigned long elsebodybegin = out.length();

			 
			 // Add the else code to an array...
			 n = outcount;
			 len = out.length();
			 found = false;
			 elsefound = false;
			 multiplestmts = false;
			 l2 = 0;
			 Array<String> a;
			 for (unsigned long i = 0; i < len; ++i)
			 {
			  String str;
			  str = out[i];
			  unsigned long k = str.getString().find(':');
                          if (k == string::npos)
			  {
                           continue;
		          }
   		          found = true;
			  str = out[i].getString().substr(k + 2);
			  unsigned long l = atoi(str.getString().c_str());
			  
			  ++l2;
			  if (l2 > 1)
			   multiplestmts = true;

			  if (l >= elsebeginlineno)
			  {
			   if (l2 > 1)
			    a.insert();
			   a[a.length() - 1] = out[i];
			   out[i] = "";
                          }
			 }
			 // Sort it by line number...
			 sort_lines(a);

			
			 // ...And insert it back into the output...
	                 for (unsigned long i = 0; i < a.length(); ++i)
                         {
			  out.insert();
			  out[elsebodybegin + i] = a[i];
			 }
			 outcount = out.length();


			 conprint("GOTOL TVOID %u\n", ifcounter + 2);


			 conprint("\n\nLBL TVOID 0V\n");

			
			 // Remove the extra line number code...
 			 for (unsigned long i = 0; i < out.length(); ++i)
			 {
			  unsigned long pos = out[i].getString().find(':');
			  String str;

			  if (pos != string::npos)
			   str = out[i].getString().substr(0, pos);
			  else
			   str = out[i];

   			  out[i] = str;
			 }


			 ++ifcounter;
			 */
			}
I get this error when compiling the bison file:

Code:
pnfha.ypp:540.25-542.25: warning: rule useless in parser due to conflicts: $@1:
/* empty */
And the code for ifelsetest.pha will not work because it complains of a syntax error. It says "unexpected ELSE".
 
Old 07-30-2014, 08:50 PM   #27
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,417

Original Poster
Blog Entries: 43

Rep: Reputation: 36
So it appears that the syntax is broken by splitting it up into chunks, and this probably has something to do with the warning...
 
Old 07-30-2014, 11:58 PM   #28
ChrisDodd
LQ Newbie
 
Registered: Feb 2006
Posts: 3

Rep: Reputation: 1
The conflict means that the grammar is not LALR(1), so bison can't generate a parser for it -- it resolves the conflicts by generating a parser for a subset of the language, which may result in rules becoming useless as they can never be recognized. You need to give bison the `-v` flags and look at the pnfha.output file to see where the conflicts are and figure out why they are occurring so you can fix them.
 
Old 07-31-2014, 12:25 AM   #29
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,417

Original Poster
Blog Entries: 43

Rep: Reputation: 36
So then I tried to break the code up as follows:

Code:
			| ifelsep1 ifelsep2 ifelsep3
			{
			 conprint("d\n");
			}	  
			;


ifelsep1:		IF LEFTP expression
			{
			 conprint("a\n");
			}
			;


ifelsep2:		RIGHTP stmt
			{
			 conprint("b\n");
			 /*
			 ifbegin = outcount;
			 if (!inblock)
			  ifbeginlineno = yylineno - 2;
			 else
                          ifbeginlineno = yylineno - 5;

			 conprint("ALOAD TBOOLEAN %s\n", $3.to_boolean().get().getString().c_str());

			 String if_body_str = "if_body_";
			 if_body_str += ifcounter;
			 String after_if_str = "after_if_";
			 after_if_str += ifcounter;

			 conprint("CGOTOL TVOID %u\n", ifcounter);
			 conprint("GOTOL TVOID %u\n", ifcounter + 1);

			 labelTable.insert();
			 labelTable[labelTable.length() - 1].name(if_body_str);
			 labelTable[labelTable.length() - 1].address(labelTable.length() - 2);

			 labelTable.insert();
			 labelTable[labelTable.length() - 1].name(after_if_str);
			 labelTable[labelTable.length() - 1].address(labelTable.length() - 2);

			 conprint("\n\nLBL TVOID 0V\n");

			 unsigned long k = outcount;
			 for (unsigned long i = 0; i < statementoutcount.length(); ++i)
			 {
			  if (statementlineno[i] >= ifbeginlineno)
			  {
			   for (unsigned long j = statementoutcount[i]; j < ifbegin; ++j, ++k)
 			    out.move(j, k);
			   outcount = k;
			  }
			 }
			 
			 conprint("\n\nLBL TVOID 0V\n");
			 */
			}
			;

ifelsep3:		ELSE stmt
			{
			 conprint("c\n");
			 /*
			 if (strcmp(yytext, "}") == 0)
	                  blockinelse = true;

			 if ($5.to_string().get().getString() == "}")
			  blockinif = true;


			 ifbegin = outcount;
			 if (!inblock)
			  ifbeginlineno = yylineno - 2;
			 else
                          ifbeginlineno = yylineno - 5;

			 conprint("ALOAD TBOOLEAN %s\n", $3.to_boolean().get().getString().c_str());

			 String if_body_str = "if_body_";
			 if_body_str += ifcounter;
			 String else_body_str = "else_body_";
			 else_body_str += ifcounter;
			 String after_if_str = "after_if_";
			 after_if_str += ifcounter;

			 conprint("CGOTOL TVOID %u\n", ifcounter);
			 conprint("GOTOL TVOID %u\n", ifcounter + 1);

			 labelTable.insert();
			 labelTable[labelTable.length() - 1].name(if_body_str);
			 labelTable[labelTable.length() - 1].address(labelTable.length() - 2);

			 labelTable.insert();
			 labelTable[labelTable.length() - 1].name(else_body_str);
			 labelTable[labelTable.length() - 1].address(labelTable.length() - 2);

			 labelTable.insert();
			 labelTable[labelTable.length() - 1].name(after_if_str);
			 labelTable[labelTable.length() - 1].address(labelTable.length() - 2);

			 conprint("\n\nLBL TVOID 0V\n");
			 unsigned long ifbodybegin = out.length() - 1;


			 // Calculate elsebegin...
			 unsigned long k = outcount;
			 unsigned long l = outcount;
			 unsigned long m;
			 for (unsigned long i = 0; i < statementoutcount.length(); ++i)
			 {
			  if (statementlineno[i] >= ifbeginlineno)
			  {
			   unsigned long j;
			   for (j = statementoutcount[i]; j < ifbegin; ++j, ++k)
 			    ;
			   outcount = k;
			   elsebeginlineno = j + 1;
			  }
			 }
			 elsebegin = outcount;


			 bool found = false;
			 bool multiplestmts = false;
			 unsigned long l2 = 0;
			 unsigned long n = outcount;


			 unsigned long fpos = ifbegin - 1;


			 // Calculate l2...
			 n = outcount;
			 found = false;
			 bool elsefound = false;
			 multiplestmts = false;
			 for (unsigned long i = 0; i < ifbegin; ++i)
			 {		  
			  ++l2;
			 }


			 // Mark the code...
			 for (unsigned long i = 0, m = statementlineno[i]; i < out.length(); ++i, ++m)
		         {
			  if (i < ifbegin)
			  {
                           out[i].insert(out[i].length(), ':');
			   out[i].insert();
			   String str;
			   str.from_long(m);
			   out[i] += str;
  		           out[i] += '\n';
			  }
			 }


			 // Calculate some other values...
			 n = outcount;
			 unsigned long len = out.length();
			 found = false;
			 elsefound = false;
			 multiplestmts = false;
			 for (unsigned long i = 0; i < len; ++i)
			 {
			  String str;
			  str = out[i];
			  unsigned long k = str.getString().find(':');
                          if (k == string::npos)
			  {
                           continue;
		          }
   		          found = true;
			  str = out[i].getString().substr(k + 2);
			  unsigned long l = atoi(str.getString().c_str());
			  
			  if (l2 > 1)
			   multiplestmts = true;

			  if (blockinelse == true && multiplestmts == true)
			  {
			   if (l >= (elsebeginlineno - 1))
                           {
			    elsefound = true;
			   }
			  }
			  else
			  {
			   if (l >= elsebeginlineno)
                           {
			    elsefound = true;
			   }
			  }
			 }
			 outcount = out.length() - 1;


			 // Are there multiple statements in the if?
			 bool multipleif = false;
			 unsigned long ifstmts = elsebegin - ifbegin - 1;
			 if (ifstmts > 1)
			  multipleif = true;


			 // Adjust ifsmts...
			 bool nvalues = false;
			 unsigned long ifstmtoffset = 0;
			 if (ifstmts == 19)
			 {
			  nvalues = ifbegin == 10 && elsebegin == 30 ? true : false;
			  if (!nvalues)
			  {
			   ifstmtoffset = ifbegin - 10;
			   ifstmts += ifstmtoffset;
			  }
			 }


			 // After this point, these are off, so we adjust them...
			 if (l2 == 4 && !blockinif)
			 {
			  ifbeginlineno -= 1;
			  elsebeginlineno -= 2;
			 }
			 else if (l2 == 3 && !blockinif)
			 {
			  elsebeginlineno -= 1;
			 }
			 else if (l2 >= 5 && !blockinif)
			 {
			  unsigned long l2offset = 0;
			  unsigned long i;
			  for (i = 6; i <= l2; ++i)
                           ++l2offset;

			  ifbeginlineno -= (2 + l2offset);
			  elsebeginlineno -= (3 + l2offset);
			 }
			 else if (l2 == 2 && blockinif)
			 {
			  ++elsebeginlineno;
			 }
			 else if (l2 == 4 && blockinif)
			 {
			  --ifbeginlineno;
			  --elsebeginlineno;
		         }
			 else if (l2 == 5 && blockinif)
			 {
			  ifbeginlineno -= 2;
			  elsebeginlineno -= 2;
		         }
			 else if (l2 >= 6 && blockinif)
			 {
			  unsigned long l2offset = 3;
			  unsigned long i;
			  for (i = 7; i <= l2; ++i)
                          {
			   ++l2offset;
			  }

			  ifbeginlineno -= (l2offset);
			  elsebeginlineno -= (l2offset);

			  if (ifstmts == 19)
			   elsebeginlineno += 1;
			  if (ifstmts == 20)
			   elsebeginlineno += 2;
			  if (ifstmts == 21)
			   elsebeginlineno += 3;
			  if (ifstmts >= 22)
			  {
			   unsigned long ifstmtoffset2offset = ifstmts - 22;
			   unsigned long ifstmtoffset2 = 4 + ifstmtoffset2offset;

			   elsebeginlineno += ifstmtoffset2;
			  }
			 }


			 // Count the statements before the if code...
			 unsigned long bifstmts = 0;
			 len = out.length();
			 found = false;
			 for (unsigned long i = 0; i < len; ++i)
			 {
			  String str;
			  str = out[i];
			  unsigned long k = str.getString().find(':');
                          if (k == string::npos)
			  {
                           continue;
		          }
   		          found = true;
			  str = out[i].getString().substr(k + 2);
			  unsigned long l = atoi(str.getString().c_str());
			  
			  if (l < ifbeginlineno)
			   ++bifstmts;
			 }
			 bool mbifstmts = bifstmts > 1 ? true : false;	// Precaution...
			 

			 if (ifbegin == 10 && elsebegin == 26 && mbifstmts)
			  ++elsebeginlineno;
			 else if (ifbegin == 11 && elsebegin == 27 && mbifstmts)
			 {
			  ++bifstmts;
			  elsebeginlineno += 4;
			 }
			 else if (ifbegin == 12 && elsebegin == 28 && mbifstmts)
			 {
			  bifstmts += 2;
			  elsebeginlineno += 3;
			 }
			 else if ((ifbegin - 13) >= 0 && (elsebegin - 29) >= 0 && mbifstmts)
			 {
			  bifstmts += (3 + (ifbegin - 13));
			  elsebeginlineno += (4 + (ifbegin - 13));
			 }
			

		         // Move the if code to the right spot...
			 k = ifbodybegin;
			 for (unsigned long i = bifstmts; i < statementoutcount.length(); ++i)
			 {
			  for (unsigned long j = statementoutcount[i]; j < ifbegin; ++j, ++k)
 			   out.move(j, k);
			  outcount = k;			 
			 }


			 // Conditionally adjust values in some cases...
			 unsigned long ifbeginoffset = 11, elsebeginoffset = 31;
			 if (ifbegin == 13 && elsebegin == 33)
			 {
 			  // Recalculate ifstmts...
			  ifstmts = 0;
			  for (unsigned long i = ifbodybegin; i <= elsebegin; ++i)
			   ++ifstmts;
			  if (ifstmts > 1)
			   multipleif = true;


			  // Recalculate elsebeginlineno...
			  if (l2 == 4 && !blockinif)
			  {

 			  }
			  else if (l2 == 3 && !blockinif)
			  {

			  }
			  else if (l2 >= 5 && !blockinif)
			  {
			   unsigned long l2offset = 0;
			   unsigned long i;
			   for (i = 6; i <= l2; ++i)
                            ++l2offset;
			  }
			  else if (l2 == 2 && blockinif)
			  {

			  }
			  else if (l2 == 4 && blockinif)
			  {

		          }
			  else if (l2 == 5 && blockinif)
			  {
			 
		          }
			  else if (l2 >= 6 && blockinif)
			  {
			   unsigned long l2offset = 3;
			   unsigned long i;
			   for (i = 7; i <= l2; ++i)
                           {
			    ++l2offset;
			   }

			   if (ifstmts == 17)
			    elsebeginlineno += 1;
			  }
			 }
			 else if (ifbegin == 12 && elsebegin == 32)
			 {
 			  // Recalculate ifstmts...
			  ifstmts = 0;
			  for (unsigned long i = ifbodybegin; i <= elsebegin; ++i)
			   ++ifstmts;
			  if (ifstmts > 1)
			   multipleif = true;


			  // Recalculate elsebeginlineno...
			  if (l2 == 4 && !blockinif)
			  {

 			  }
			  else if (l2 == 3 && !blockinif)
			  {

			  }
			  else if (l2 >= 5 && !blockinif)
			  {
			   unsigned long l2offset = 0;
			   unsigned long i;
			   for (i = 6; i <= l2; ++i)
                            ++l2offset;
			  }
			  else if (l2 == 2 && blockinif)
			  {

			  }
			  else if (l2 == 4 && blockinif)
			  {

		          }
			  else if (l2 == 5 && blockinif)
			  {
			 
		          }
			  else if (l2 >= 6 && blockinif)
			  {
			   unsigned long l2offset = 3;
			   unsigned long i;
			   for (i = 7; i <= l2; ++i)
                           {
			    ++l2offset;
			   }

			   if (ifstmts == 17)
			    elsebeginlineno += 2;
			  }
			 }
			 else if ((ifbeginoffset - ifbegin) >= 0 && (elsebeginoffset - elsebegin) >= 0)
			 {
			  elsebeginlineno += (3 + (11 - ifbeginoffset));
			  if ((ifbeginoffset - ifbegin) >= 1)
 			   elsebeginlineno += 2;
			 }


			 conprint("GOTOL TVOID %u\n", ifcounter + 2);
			 statementoutcount.insert();
			 statementoutcount[statementoutcount.length() - 1] = outcount - 1;
			 statementlineno.insert();
			 statementlineno[statementlineno.length() - 1] = elsebeginlineno;


			 conprint("\n\nLBL TVOID 0V\n");
			 statementoutcount.insert();
			 statementoutcount[statementoutcount.length() - 1] = outcount - 1;
			 statementlineno.insert();
			 statementlineno[statementlineno.length() - 1] = elsebeginlineno;

			 unsigned long elsebodybegin = out.length();

			 
			 // Add the else code to an array...
			 n = outcount;
			 len = out.length();
			 found = false;
			 elsefound = false;
			 multiplestmts = false;
			 l2 = 0;
			 Array<String> a;
			 for (unsigned long i = 0; i < len; ++i)
			 {
			  String str;
			  str = out[i];
			  unsigned long k = str.getString().find(':');
                          if (k == string::npos)
			  {
                           continue;
		          }
   		          found = true;
			  str = out[i].getString().substr(k + 2);
			  unsigned long l = atoi(str.getString().c_str());
			  
			  ++l2;
			  if (l2 > 1)
			   multiplestmts = true;

			  if (l >= elsebeginlineno)
			  {
			   if (l2 > 1)
			    a.insert();
			   a[a.length() - 1] = out[i];
			   out[i] = "";
                          }
			 }
			 // Sort it by line number...
			 sort_lines(a);

			
			 // ...And insert it back into the output...
	                 for (unsigned long i = 0; i < a.length(); ++i)
                         {
			  out.insert();
			  out[elsebodybegin + i] = a[i];
			 }
			 outcount = out.length();


			 conprint("GOTOL TVOID %u\n", ifcounter + 2);


			 conprint("\n\nLBL TVOID 0V\n");

			
			 // Remove the extra line number code...
 			 for (unsigned long i = 0; i < out.length(); ++i)
			 {
			  unsigned long pos = out[i].getString().find(':');
			  String str;

			  if (pos != string::npos)
			   str = out[i].getString().substr(0, pos);
			  else
			   str = out[i];

   			  out[i] = str;
			 }


			 ++ifcounter;
			 */
			}
			;
Now it complains:

Code:
pnfha.ypp:546.25-549.25: warning: rule useless in parser due to conflicts: ifels
ep1: IF LEFTP expression
And the code does the same thing as before.
 
Old 07-31-2014, 12:33 AM   #30
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,417

Original Poster
Blog Entries: 43

Rep: Reputation: 36
The .output file is attatched.
Attached Files
File Type: txt pnfha.zip.txt (21.8 KB, 8 views)

Last edited by des_a; 07-31-2014 at 12:36 AM.
 
  


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 07:38 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