Re: Syntax Highlighting

John Lilley <>
16 Jan 1997 20:12:24 -0500

          From comp.compilers

Related articles
Syntax Highlighting (Tim Roberts) (1997-01-16)
Re: Syntax Highlighting (John Lilley) (1997-01-16)
Re: Syntax Highlighting (Scott Stanchfield) (1997-01-19)
| List of all articles for this month |

From: John Lilley <>
Newsgroups: comp.compilers
Date: 16 Jan 1997 20:12:24 -0500
Organization: Nerds for Hire, Inc.
References: 97-01-124
Keywords: syntax, tools

Tim Roberts wrote:
> anyone help me with syntax highlighting. Ideally, I'd like to write an
> editor to run under Windows

Typically, syntax highlighting involves writing a lexer for the
language you are editing. The lexer turns the input source text into
"tokens", which are the fundamental chunks of input. I'll not get
into that here -- check out any good compiler theory book like the
Dragon book, or read about lex or flex or dlg.

Each token type is assigned some display styles by the user of your

Once you have a lexer, then you feed the source text from your editor
into the lexer, get the stream of tokens back, and for each token
match up the token with the corresponding source text segment and draw
the text in the right style. The easiest way to do that is to embed
line/column information into each token.

Then the final trick is to optimize the redraw when you edit the text.
This is a bit tricky, especially since typing "/*" in C/C++ will turn
a ton of tokens from what they were previously into a giant comment.
I suggest that when a change is made, you back up to the token
preceeding the text change, start lexing from there, and continue
lexing the text including and following the change, until the token
stream once again matches with the previous token stream. Obviously
there is room for clever optimziation :)

Good luck!

john lilley

Post a followup to this message

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