Comments in a pre-compiler

"Satyam" <>
7 Sep 2005 13:15:08 -0400

          From comp.compilers

Related articles
Comments in a pre-compiler (Satyam) (2005-09-07)
does MORTRAN2 still exist? (glen herrmannsfeldt) (2005-09-10)
Re: Comments in a pre-compiler (Chris F Clark) (2005-09-10)
| List of all articles for this month |

From: "Satyam" <>
Newsgroups: comp.compilers
Date: 7 Sep 2005 13:15:08 -0400
Organization: Compilers Central
Keywords: lex
Posted-Date: 07 Sep 2005 13:15:08 EDT

I made a pre-compiler to add a couple of instructions I had in mind to
an existing language, basically to test whether they were usable or

In trying to improve it I wanted to make the output nicecly human
readable. Up to then, the output of the pre-compiler worked nice for
the interpreter to understand ok, but if I wanted to read it, I had to
run a beautifier on it.

The problem came with comments. Comments are read and discarded by
the lexer, they never get to the parser. The parser and the lexer
often get out of synch with one another, the parser asking the lexer
to go ahead and then backtracking (I am probably not using the right
terms to refer to these actions, just take them in the more general
meaning). So, if I let the lexer let the comments through, by the
time the parser realises which code to output, there might be some
comments out ahead of the code. It is mostly noticeable with comments
interspersed within a statement.

So, I wanted to ask, is there a trivial solution which I am unawere
of, is it a non-trivial issue or it simply cannot be done? Any


[It's a difficult problem to do well. A simple approach is to hang
the text of each comment on an adjacent token, and then output the
comment when you output the token. It's not foolproof, e.g., if the
token is duplicated or deleted, but it's easy. -John]

Post a followup to this message

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