lex/yacc question

harvard!seismo!harvard!ksr!sloth!guy (Hillyer)
24 Apr 87 21:47:00 GMT

          From comp.compilers

Related articles
lex/yacc question harvard!seismo!harvard!ksr!sloth!guy (1987-04-24)
| List of all articles for this month |

From: harvard!seismo!harvard!ksr!sloth!guy (Hillyer)
Newsgroups: comp.compilers,comp.unix.questions
Date: 24 Apr 87 21:47:00 GMT
Original-sender: harvard!seismo!harvard!ksr!nobody
Organization: Kendall Square Research, Cambridge MA

I have an assembler that uses lex & yacc, and I would like
to have it generate traditional assembler listings of the form:


        address: data label: mnemonic instruction and operands


Now, my current technique is to gather the text in the lexical analyzer
and the address and data in the parser. Production of the address &
data part of a listing line is driven by the parser recognizing an
instruction, while production of the text part is driven by the lexer
seeing a new-line. The problem is that the execution of lex- and
yacc-generated code is not synchronized, so sometimes I see the text
first, then the data, while other times I see the data first and then
the text. The problem is exacerbated when an instruction produces more
than one line of data, or multiple lines of text correspond to just one
word of data. I have been able to deal with the problems, for the most
part, by keeping flags and counters around for special cases, but I'm
not very pleased with the result.


So here's the question. Is there some arcanery in lex & yacc (of which
I am ignorant) that would make this easier? I would appreciate advice
from anyone who has been in a similar boat.


Thanks.
Guy Hillyer


guy%ksr.uucp@harvard.harvard.edu
[The few times I had to write an assembler, I finessed the problem by not
making source listings, just symbol tables. But I thought about it some and
didn't come up with anything very clever. I'd buffer up a source line and
set a flag when I saw a line feed. Then, as I emit object code, I'd append
the source line to the next listing line I put out and clear the flag so
that subsequent listing lines didn't repeat the same source line. If I
started to read another source line before dumping the current one, most
likely because it's a comment, I'd put out the saved source line then. Not
too elegant, but probably effective. Neither yacc nor lex are liable to give
you much more help. Readers are encouraged to prove me wrong. -John]
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.