1

I have written a simple assembler using flex+bison. I'd like to add a preprocessor (macros) to the assembly language. This is my first time trying to use flex+bison, I'm not sure how to go about this.

Is it appropriate to add a separate instance of flex+bison and do the preprocessing completely separately? Or do they support expressing different constructs for different phases of processing?

If curious, the assembler is here, for the DCPU-16 architecture.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
blueshift
  • 6,742
  • 2
  • 39
  • 63

2 Answers2

4

You definitely want a separate parser. That's the way gcc does it - you can stop the compilation after just the preprocessing (-E, I believe). It may be technically possible to write as a single parser, but it'll be a lot more trouble, and I don't see a particular reason why it would be useful, whereas keeping it separate will allow you to stop compilation after just preprocessing, making it easier to track down errors not just in the preprocessor/assembler itself, but any programs written with it.

Kevin
  • 53,822
  • 15
  • 101
  • 132
  • You describe a desirable *effect*. What I am asking about is method: Express both in one "instance" of flex+bison (and handle the phases in the parser callbacks) or have a complete separate flex+bison instance. – blueshift Apr 14 '12 at 06:01
  • 2
    Write two separate lexers and parsers. Use the prefix directive and they won't clash. – Kevin Apr 14 '12 at 14:07
  • I don't agree with this answer. It is simply pushing a religion, and a bad one. Textual preprocessing is the scourge of software development. The way gcc does is is that way because gcc has to be compatible with a braindamaged design. The way the question is posed, however, does lead to this answer; it asks for a "preprocessor". – Kaz May 04 '12 at 20:02
  • 1
    @Kaz feel free to share an alternative constructive suggestion rather than just bashing the one offered. – blueshift May 27 '12 at 23:08
3

You can take a look at mine. I got in the same idea you have:P You can find it here It supports expressions but I realized that you must have an ast if you want to support forward references. Maybe when Notch figures out that unfixed sized op codes are harder to make an assembler for he will change it:)

A decent book on the subject is Flex and Bison It shows how you can do "states" that is you can push the lexer state and change out the file buffers. It shows a rudimentarily parser/lex for just that. It helped alot in my learning:)

Paul Bruner
  • 435
  • 4
  • 8
  • Thanks, I'll take a look. And yes you need an AST if you want to support short literals involving symbols, too. They make life harder but surely that can be considered part of the challenge of the 0x10c world? :) – blueshift Apr 23 '12 at 04:15