yacclexcompiler-theorydcpu-16

Keeping track of original line numbers through macro expansion


I'm working on a assembler for fun, written in C,flex,bison. I'd like to add macros, includes and repeating blocks and was thinking of doing this with a separate preprocessing stage parser.

My question is, how might I keep track of original source line numbers (and filenames)? This is for producing useful error messages, pretty printing, and generating debug information.

yylineno in the second parser after preprocessing is complete will presumably be offset after macro expansion and so on.


Solution

  • you can add

    ;#file filename.asm
    ;#line 5
    

    to the preprocessed assembler so

    foo:
    PUSHREG(A,B,C)
    ;--10 lines of code
    POPREG(A,B,C)
    set PC,POP
    

    turns into

    foo:
    ;#file functionmacros.asm
    ;#line 10
    set push,A
    set push,B
    set push,C
    ;#file yourfile.asm
    ;#line 5
    ;--10 lines of code
    ;#file functionmacros.asm
    ;#line 30
    set C,pop
    set B,POP
    set C,POP
    ;#file yourfile.asm
    ;#line 16
    set PC,POP