Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!ima!johnl From: johnl@ima.UUCP Newsgroups: comp.compilers,comp.unix.questions Subject: lex/yacc question Message-ID: <558@ima.UUCP> Date: Fri, 24-Apr-87 16:47:00 EDT Article-I.D.: ima.558 Posted: Fri Apr 24 16:47:00 1987 Date-Received: Wed, 29-Apr-87 06:28:36 EDT Sender: johnl@ima.UUCP Reply-To: harvard!seismo!harvard!ksr!guy () Organization: Kendall Square Research, Cambridge MA Lines: 41 Approved: compilers@ima.UUCP Xref: utgpu comp.compilers:7 comp.unix.questions:1860 Original-sender: harvard!seismo!harvard!ksr!nobody 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] -- Send compilers articles to ima!compilers or, in a pinch, to Levine@YALE.ARPA Plausible paths are { ihnp4 | decvax | cbosgd | harvard | yale | cca}!ima Please send responses to the originator of the message -- I cannot forward mail accidentally sent back to compilers. Meta-mail to ima!compilers-request