Book "Compiler Construction using Flex and Bison" - problems, discussions, steps, ...
Hello. I started reading the book "Compiler Construction using Flex and Bison", freely available at http://research.microsoft.com/en-us/...l/compiler.pdf. Same file mirrored here: http://balacobaco.insomnia247.nl/dedec0/compiler.pdf .
There are a few errors but was able to fix them until chapter 3. Now, in chapter 4, I have reached a point where I cannot make some modifications the text directs to, without reaching a compilation error from bison. A working bison file (it compiles with no output), with the lines that causes problem when added/changed is: Code:
%start program Code:
$ bison -vd ch4.y I need this .y file working before I proceed to next session, where the corresponding scanner (a flex file) is changed. May you help me understanding what is wrong and fixing it? |
Forgot to post it. The "st.h" file is:
Code:
typedef struct symrec |
Are you sure you have copied over all the changes as required? I noticed a reference to "exp : INT" which I do not see in your file. There could be others. I would suggest going back over all entries.
|
The "exp: INT" you mention is in the end of PDF page 21? I have changed it to INTEGER.
I had a few doubts and went through a few errors in chapters 1-3. I had to make changes to what is written in the book to be able to compile. I have files made for each chapter, with their given code. Until chapter 3 they work. Is "INT" something that makes sense? Or would it be a short "typo" for INTEGER, like I thought? And there is also the NUMBER token. Note that, in the file above, I have added declarations for all of them (or bison gives error for undeclared token). In the file below I have changed all INT and NUMBER tokens to INTEGER. Then I removed declarations for both, the error is the same. Without the union declaration and the IDENTIFIER with <id> (exchanged) bison compiles it silently (assumed to be good). With the union declaration and <id> in IDENTIFIER, as added in chapter 4, the error appears. In the code below it is easy to make the changes I mentioned. It is just to cut/paste a few lines from/to multiline comments above each part: Code:
%start program Code:
$ bison -vd ch4.y |
INT appears in your tokens. Been a while since I have played with this stuff, but I am sure one of the others will be able to help you further :)
|
Do not miss it: in this last post INT is only inside a multiline comment (that we should use it to easily change from a working to an erroneous file, or vice versa). There is no other occurrences of it.
I hope to have given enough details of my problem so anyone could easily and quickly reproduce it. Thank you for your goodwill, grail. Do you have a book to recommend? This is the second one that I got with these not so small problems. |
Not seeing any definition for ASSGNOP.
Also I don't think this is right: Code:
%token <id> IDENTIFIER Code:
%type <id> IDENTIFIER |
Quote:
Quote:
1. Without union and IDENTIFIER is a simple token: it compiles (a previous result). 2. With union declared, "%token IDENTIFIER" removed, line "%type <id> IDENTIFIER" added: Code:
$bison -vd ch4.y Code:
$bison -vd ch4.y For now I am keeping the "%type <id> IDENTIFIER" line, but the error continues. |
Code:
ch4.y:91.54-55: $2 from `command' has no declared type |
Line 91 is
Code:
command : SKIP |
NO! It should be $1, right?? So context_check will check if the variable is declared or not. Right?? :D :D :D
|
How much clearer can I get? I already told you in comment 7 there is no definition for ASSGNOP and you didn't believe me.
|
I will admit first, that I have not fully followed your example code nor read the PDF (that URL is blocked by my local firewall rules).
But I do see that you are confused about the use of %token and %type, so perhaps I can offer some helpful comment on those. Quote:
%token is used to declare terminals, and may include or require a <type> assignment when a %union has been declared, but not otherwise. So for terminal symbols (which IDENTIFIER seems to be), either of these would be correct depending on usage... Code:
%token IDENTIFIER Code:
%type <id> IDENTIFIER So something like this might be appropriate, again depending on usage... Code:
%type <id> commands command exp ... When %union is declared you will need to declare terminals (tokens) with a type as... Code:
%token <id> IDENTIFIER Code:
%type <id> command commands exp ... Quote:
It may be that the '$2 from command' error you are seeing is complaining about exp, not IDENTIFIER (but, also not clear to me). |
Quote:
"since there is no definition for ASSGNOP, I should look at line 91 and see that I need to change the argument because it is incorrect, it is pointing to ASSGNOP instead of IDENTIFIER" ? No way. Your words there are not clear at all, not for me. I bet that not for others too. The title of the thread contains the word "learning" because I know very little of Bison, if I can say I know something about it at all. Anyway, thank you. Our conversation helped me to solve another "easy" problem in this book. |
astrogeek, thank you for your explanations. I had not yet seen %type, so I had no clue what was it. I just used it as something more to try, almost blindly (as my trial and error report shows).
As I undertand now, IDENTIFIER is the token that corresponds to the variable name, not its value or symbol. In C we could have an integer atribution: boxOfFruits = 26; For this line, there would be 4 tokens: IDENTIFIER (with a string "boxOfFruits"); EQUAL_SIGN; INTEGER (with value 26); END_OF_LINE. So, IDENTIFIER is a terminal symbol or not (I think it is not). The language of the book it not C, but it is something similar for that expression. |
All times are GMT -5. The time now is 11:31 AM. |