Quote:
My compilers text is rather old, so maybe someone else can recommend a more recent one if you're inclined to do some reading first (although actually I doubt the concepts have changed at all). |
Quote:
Anyway, I was considering using a tree node structure that contains a name, value, and child nodes to represent the syntax tree. A function that would print the tree recursively might be something like this, where each node structure stores it's name, value, and a NULL-terminated array of child nodes: Code:
int level = 0; |
Quote:
You would also be able to dump AST into a file and consume it in another script. I've been successfully using this approach for years in/for serious EDA related stuff. |
I am slowly getting to understand the Calculator example, and I finally got it to compile!
Code:
$ cat euclidian-algorithm.txt |
2 Attachment(s)
I'm slowly making some progress — I already wrote most of the Lex and YACC files for an interpreter that puts together a syntax tree structure and parses it.
I'm pretty sure I omitted a lot of important stuff, could you give me any suggestions? |
Looks pretty good, considering that a short while ago you said:
Quote:
Code:
[+-]?[0-9]+ Well done. --- rod. |
Quote:
4-3 will be interpreted as: 4 -3 instead of 4 - 3 , which is invalid. I thought that it would work right if you would implement negation as part of the grammar. That would also let you negate variables, and expressions in parentheses, not just integer literals. Quote:
|
2 Attachment(s)
OK, I tried compiling my code and it has all these errors, I am totally puzzled because I hardly understand the code I wrote in the first place :(
At least lex and yacc passed without errors first time! Code:
$ gcc *.c -o lang |
Quote:
Code:
lang.l:2:19: error: y.tab.h: No such file or directory ---- rod. |
Unary minus
Section 5.4 of the Bison manual that I mentioned earlier illustrates how to handle the unary minus:
http://www.gnu.org/software/bison/ma...ual-Precedence. Give it a try. |
@theNbomr
For some reason I run yacc, then lex, and it doesn't create a "y.tab.h" file! @larryherman I implemented that in my code now. |
Quote:
(If you're actually using yacc rather than bison just ignore the above, but if you're using Linux then yacc may just be a one-line shell script exec'ing bison.) Hope this helps. |
Code:
$ cat /usr/bin/yacc |
Can't say why the -y option to bison (in that script) doesn't seem to have the advertised effect, but the -d option to bison works for me, generating the file file.tab.h (from file.y).
|
2 Attachment(s)
bison -d worked.
I corrected some other minor errors, but what is this: Code:
$ gcc *.c -o lang |
All times are GMT -5. The time now is 07:26 AM. |