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 am trying to generate code for an if-else statement for the language I am developing. I am using bison and flex to write the language in, because it is the easiest language to write it in I have access to. However, it is still difficult for even a simple if-else statement. The problem is that the output is not always placed where I want it to be.
So what I have to do is store the output in an array, and then modify the array so that it's correct, by moving the output to where I want it. I have done this by "marking" the code with line numbers specifying where it belongs, and then moving the code to where it really belongs. The problem is that I'm depending on line numbers given by yylineno, and it is sometimes off.
I am going to a lot of work to correct the line numbers, so they will not be off. While correcting the line numbers, I have needed to count the number of if statements, so that everything will be still placed okay if there is a statement block in the if. Then I can use that value, to correct the other values. The problem now, is that the number of if statements is always reading the same.
I have no idea what this line-renumbering or line-moving is, but it does sound wrong. If you haven't read Pike and Kernighan's book The Unix Programming Environment then you should now. If you have, you should read it again.
The line numbering is where I use yylineno, and "mark", the code I'm outputting, so that it has line numbers where needed. What the moving is, is then I use my move function in my array class, to "move" the elements to the correct line number they belong on. After this, I remove the line numbers. Then I can output the array and everything is in the right spot. I'll read the book to see if it helps.
I am creating this on a windows XP enviroment right now, but it should port to Linux pretty easy, and I later intend to. I just started it on Windows because that is where I normally do most of my initial programming work for applications.
This language I am creating, is supposed to be compiled to another language I created, called PNFASM, which is Portable Numbers Format Assembly Language. That can be compiled into PNF, which is Portable Numbers format, and then run by the interpreter, or translated to C++ to be compiled into a native application.
I just read the PDF version of it. I knew the gist of it. But it doesn't talk about anything of the nature I'm doing. What I'm doing is using bison and flex. Bison comes from yacc, but has more features, and flex comes from lex, but has more features. It is a limitation of bison that the code is not output where I want it to be, because the code generation takes place in the actions, and the actions are not run in the order I would like them to be for the if-else statement. So that is why I'm moving the code to the right spot. I am storing the code in an array, so that I can do that manipulation easy, before it gets written to the file.
Perhaps there is a better way to do the code generation?
As NevemTeve says, whatever you are doing is not very clear. Perhaps an example of your language and Bison input would help.
Bison and yacc, use stacks and token steam look-aheads to parse syntax, so he yylineno value and the index in the term stack could have have a very complicated relationship.
It sounds like you should not use Bison at all. Just use flex to tokenize and write your own simple if-then-else parser.
So I fixed one problem. The loop I was using to calculate the if statements was somehow off. But I figured out that what I wanted it to do was the same as this:
I see now that hoc was a link. Hoc is an interpreted programming language, and my language is a compiled language. I am trying to write a compiler for my language, not an interpreter. So the action doesn't execute the code, the action takes part in generating code that will be executed, or in this case assembled later to be executed by the interpreter. The action part of the code is where my error is. The syntax part of the code works just fine.
You should not be using yylineno to identify jump label points. yylineno is typically used to generate the line number in warning or error messages about syntax parsing errors, not code labels.
It has been awhile since I used bison/yacc, but you be using the values returned that the parser can return to identify the label positions.
Below is probably incorrect as I have not used yacc in awhile.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.