re: How to build an AST using Bison:
For simple examples of building an AST from Bison, see this site: EPaperPress.
Also see Mitsuhisa Sato's example (This is code for a simple interpreter using Bison).
re: Performance difference of interpreter using AST vs. straight interpreting.
There are many ways you could write an interpreter - the list below is from (in general) slowest to fastest performance (note for trivial programs, there may be little difference. But for programs that repeat the same code many times, there can be a great difference):
- straight interpretation
- do some pre-processing, e.g., tokenizing and stripping comments
- build an AST and interpret that
- convert to intermediate code - byte code - via an AST or not - and interpret that
For a speed comparison, you could do the following:
Go to: Tom Gibson's Tiny C site.
And download the appropriate interpreter - either Tom Gibson's Tiny C for DOS, maybe Windows or Tom Gibson's Tiny C for Linux.
This is a pure interpreter - #1 above. Write a little program that computes primes, or something similar, and then time it.
The 'Tiny C' from Sato's site includes an interpreter that interprets from an AST - #3 above. Write a similar program for it, and time it.
Finally, get another Tiny C from: Marc Feeley's Tiny C.
This one creates an AST, converts it to byte code, and then interprets that - #4 above. Rewrite your little program for this one, and time that.
I hope this helps!