I guess a difficulty will be that most lexer generators can't deal with
significant whitespace, but I thought that might be something I could
fairly easily modify. (But, I've never used a lexer generator, so I
may have a big learning curve in front of me.)
Randy Kramer
Thanks for the suggestion! I downloaded Luddite and UDL (well actually,
I downloaded all of Komodo--wish I had looked first--something over 400
MB). Anyway, started looking, may try looking again--tentatively
headed in the direction of writing the lexer "manually"--you'll
probably see more questions on the list.
Randy Kramer
On Friday 11 September 2009 07:37:07 pm Lex Trotman wrote:
> I doubt that flex or other lexical analyser generators are suitable
> for lexers in scintilla. They generate lexical analysers that are
> optimised for reading the whole file from the beginning and returning
> a string of tokens to the parser.
Ahh, good point--I think I was starting to understand that, but you're
comment made it clear to me. I'm headed in the direction of writing
the lexer "manually" in C++, and you'll probably see more questions on
the list.
One thing that sort of bothers me--I know you can use labels and gotos
in C/C++ (at least I'm pretty sure of that), but it's pretty much
considered bad form. But, it looks to me like that is the best
approach for parts of my state machine--like the main loop and parts of
other sub-loops (I guess I should say main state machine and parts of
other sub- state machines).
One of the alternatives is a deeply nested structure that, for some of
the things to be recognized, may be deeply nested indeed--just the
indentation to handle the nesting would be ridiculous. I could create
sub-functions just to minimize nesting, but gotos seem so much easier.
Oh, I have come across the quex lexical analyser generator which has a
facility to (automatically, iiuc) create a state diagram based on the
input. I'm tempted to use it just to create a good state diagram for
documentation.
Thanks!
Randy Kramer
Hmm, and I just re-read (re-skimmed?) Neil's coding style document
(Scintilla and SciTE: Code Style) and see this:
"The goto statement is not used because of bad memories from my first
job maintaining FORTRAN programs. The union feature is not used as it
can lead to non-type-safe value access."
If I'm still thinking the way I am now, I might write the first
version(s) with gotos, and try to revise it later.
Randy Kramer
> Hmm, and I just re-read (re-skimmed?) Neil's coding style document
> (Scintilla and SciTE: Code Style) and see this:
>
> "The goto statement is not used because of bad memories from my first
> job maintaining FORTRAN programs. The union feature is not used as it
> can lead to non-type-safe value access."
>
> If I'm still thinking the way I am now, I might write the first
> version(s) with gotos, and try to revise it later.
Lexers don't have to follow the style rules when they are written
by others so are their responsibility. Some currently use goto.
Neil
Well, look (again?) at some mainstream lexers like LexCPP, LexLua or similar: most of them
if not all have a state machine made by using a test on the state (sc.state) -- could have
been a switch, I suppose, and some tests on the current char (and those around) which
result on a state change (sc.ChangeState, sc.ForwardSetState)... or not (sc.Forward).
The resulting code is still readable, not too deeply nested.
Actually, some lexers use a switch (like LexInno). But not all use StyleContext (even
though most of them include the header!). I think a new lexer should use StyleContext as
it really eases writing lexer code.
--
Philippe Lhoste
-- (near) Paris -- France
-- http://Phi.Lho.free.fr
-- -- -- -- -- -- -- -- -- -- -- -- -- --
I second that, I think it is convenient to follow the LexCPP
structure and use StyleContext. The COBOL lexer, for example, is
structured a bit differently, and in some cases, a character can
be left sorta 'unlexed' or skipped over (in normal source code,
such glitches are often not noticeable, though.)
As for gotos, once I zapped a huge chunk of duplicated code in
LexPerl with gotos, and Neil accepted the patch. :-) But recently,
I have rewritten it to follow the LexCPP style, and no gotos are
needed there.
--
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia
Thanks! (Sorry for the late reply!)
I might still start with my goto approach (especially while working in
pseudocode), but then think about rewriting it to use switch
statements.
I have noticed that a fair number of the "lexer generators" generate
code using gotos, but I guess they (as opposed to a human) are diligent
enough to handle it without making mistakes. ;-)
Randy Kramer
Yes, and they don't care about readability and maintenability (at least by themselves!). :-D