LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Book "Compiler Construction using Flex and Bison" - problems, discussions, steps, ... (https://www.linuxquestions.org/questions/programming-9/book-compiler-construction-using-flex-and-bison-problems-discussions-steps-4175587087/)

sundialsvcs 09-19-2016 01:06 PM

When specifying your grammar, I think that it is very important that you explicitly state the precedence of all operators, and left/right bindings and so on. "Don't rely on any defaults: say it."

I also strongly suggest that you :study: study :study: the code that is produced. Study the source-code of the actual parsing engine as it works through the data structures that are the output of the compiled grammar. It needs to "stop being a mystery, to stop being 'a black box' in your mind," as soon as possible.

dedec0 09-19-2016 01:54 PM

Quote:

Originally Posted by sundialsvcs (Post 5607352)
When specifying your grammar, I think that it is very important that you explicitly state the precedence of all operators, and left/right bindings and so on. "Don't rely on any defaults: say it."

I also strongly suggest that you :study: study :study: the code that is produced. Study the source-code of the actual parsing engine as it works through the data structures that are the output of the compiled grammar. It needs to "stop being a mystery, to stop being 'a black box' in your mind," as soon as possible.

Writing them all would be my choice, sundialsvcs. :)

Studying the generated code... seems good. I will try it sometime. For now, my aim is to get a full working flex+bison+cc compiler with some optimization for any not complicated language. I imagine that using it for more complex languages/tasks/optimizations can be smooth.

jpollard 09-19-2016 02:44 PM

The book itself is VERY incomplete - basically unusable.

Even the stack machine included is incomplete (it doesn't detect stack overflow/underflow for one, missing PC out of range, memory range limits too).

The book LOOKS like a draft copy, not a final copy.

1. The problems you have seen are due to poor choice of grammar. It is not necessary (though sometimes simpler) to use %left/%right anywhere - but you do have to be careful with the grammar rules.

2. The incorrect use of some terminology... It is almost like this was being translated from something done earlier, but hasn't yet been polished.

3. Missing examples - Optimization is an important section - yet it is nearly empty other than a little introductory text. Completely left out is algebraic optimization, a little constant folding (barely mentioned)...

4. code generation is mostly skipped.

5. Unspecified references for "further reading"...

You get better examples from the "Lex and Yacc" book, though they won't go into optimization (which is a rather complex subject on its own).

dedec0 09-19-2016 02:52 PM

Quote:

Originally Posted by jpollard (Post 5607382)
The book itself is VERY incomplete.

Even the stack machine included is incomplete (it doesn't detect stack overflow or underflow for one).

The book LOOKS like a draft copy, not a final copy.

I agree with you, but I probably cannot see it as clearly as you (because I am trying to learn from it).

I have done some work to produce the files I showed here, with things until chapter 4. If they are close to being a full compiler, I would like the help of people that are reading this thread. If they are not so close, please point that detail, so I do not expend more time with this idea.

jpollard 09-19-2016 03:08 PM

The basic problem is that you need a decent book. This is not one of them.

You might try http://gnuu.org/2009/09/18/writing-y...-toy-compiler/

I haven't gone through this - but the organization behind it do know what they are doing.

dedec0 09-19-2016 03:14 PM

Quote:

Originally Posted by jpollard (Post 5607390)
The basic problem is that you need a decent book. This is not one of them.

You might try http://gnuu.org/2009/09/18/writing-y...-toy-compiler/

I haven't gone through this - but the organization behind it do know what they are doing.

Thank you! I have read its first page, the only thing that I would not choose is the use of llvm - but I will try that, anyway. llvm is something that have been close to me for sometime...

"gnuu" is a domain I have not seen before. Funny...

sundialsvcs 09-19-2016 07:20 PM

These are among the various (widely-held ...) sentiments that prompt me to encourage you to "use the Source, Luke!"

I have read a great many books on compilers, and, with the possible(??) exception of Aho's book, I have yet to read a single decent one.

Therefore, I say, Get Jiggy Wit It, as soon as possible. Surf GitHub for existing projects which actually(!) use(!!) Bison. Find their grammar files, and the associated code which uses the Bison-generated parser. "This is the Real Deal, baby!"

Take the output of the Bison step and actually look at it. Study the actual source-code of the runtime engine. Rip the hood and both fenders off of the damned thing, and look at it, until you can see for yourself how it actually w-o-r-k-s.

You've spent more than enough time "reading books about swimming." Go find some actual examples. Then, after tightly securing your life-vest and your foam noodles, get into the water!


All times are GMT -5. The time now is 09:56 AM.