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.
"Node" is a typedef'd struct that represents a node in the abstract syntax tree.
Code:
$ ./compile
Running YACC:
lang.y: conflicts: 4 shift/reduce
Running Lex:
Running GCC:
In file included from lang.l:2:
lang.y:12: error: expected specifier-qualifier-list before ‘Node’
GCC returned with exit code 1
I don't see anything wrong with the grammar part of the code, and it seems to be the compiler complaining, so how about showing the definition of the Node type. Is it in the code section of the yacc file, or is it in a header file? If the latter, is there a #include for it?
You seem to spend relatively little time tracking down problems on your own. It might be a helpful learning experience to dig into the problems a little more before posting for help. Just a friendly suggestion.
When it reaches the line highlighted in bold, it segfaults.
I used gdb and found that this happens on the first iteration, so it couldn't be going out the array's bounds. I also found that n->opValues.ops (which is of Node** type) is not NULL, so that isn't the problem. I wonder what's wrong?
You didn't call va_start() before calling va_arg(), so ap has no initial value; if you try examining it in gdb you should see it's either NULL (0) or has some random (garbage) value.
It's good that you used gdb but when you have an error examining the values of all of the variables in the section of code in question may be necessary to figure out what's going on.
The funny strings were because yylval was stored in the nodes, but it changes later during parsing. I solved that by making a simple function that allocates a duplicate string.
The Literal/Identifiers with the same value were because I for got the breaks in the switch statement that acted on different nodes.
The funny strings were because yylval was stored in the nodes, but it changes later during parsing. I solved that by making a simple function that allocates a duplicate string.
The Literal/Identifiers with the same value were because I for got the breaks in the switch statement that acted on different nodes.
Now the output is EXACTLY like my example.
So, now you probably have some feeling of (lack of) variables locality, encapsulation and at all relationship between language constructs recognized by parser and triggered by recognition callbacks.
stmt: This is line 44
expr '\n' { $$ = $1; }
IF expr '\n' stmts ';' '\n' { $$ = mkOpNode(IF, 2, $2, $4); }
;
Code:
$ ./compile
Running YACC:
lang.y:45.21-22: $$ for the midrule at $3 of `stmt' has no declared type
lang.y:46.60-61: $2 of `stmt' has no declared type
lang.y:46.64-65: $4 of `stmt' has no declared type
YACC returned with exit code 1
Are you missing a vertical bar between the two lines of your stmt rule? (You want a stmt to be either an expr followed by a newline, or an if statement.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.