Re: How is a compiler written



"Walter Bright" <walter@xxxxxxxxxxxxxxxxxxxxxxx> writes:
> "Greg Comeau" <comeau@xxxxxxxxx> wrote in message
> news:dk5vr5$eb6$1@xxxxxxxxxxxxxxxxxxx
>> The Dragon Book is a good one suggestion, but suggesting the
>> best way to write a compiler is with lex and yacc is not.
>
> I agree. For one, the lexer and parser are often the simplest and most easy
> parts of a compiler to write (*). The hard stuff comes later, for which lex
> and yacc are quite useless. The annoying thing about most compiler books is
> all the attention they give to lexing and parsing, and the short shrift for
> the hard parts (semantic analysis, optimization, code generation).
>
> Me, I've usually found that lex and yacc take longer to write than building
> by hand, the error handling is poor, and the generated lexer/parser is
> inefficient.
>
> (*) The exception to this rule is C/C++, for which writing a lexer is very
> hard, and writing a parser (for C++) is even worse. Unfortunately, this
> complexity also renders lex and yacc impractical for those languages as well
> <g>.

I know that parsing C is difficult because of the treatment of typedef
names (are there other serious problems?), but what makes writing a
lexer so difficult? Are you referring to the preprocessing phase? I
suppose you really need two lexers, one that's part of the
preprocessor and another that works on the preprocessor's output, but
I don't see that either of them would be overly complex.

(To keep this topical, I'm looking for an answer in terms of something
about the language definition that makes it difficult.)

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: How is a compiler written
    ... >>Greg Comeau wrote: ... >>> The Dragon Book is a good one suggestion, ... >>> best way to write a compiler is with lex and yacc is not. ...
    (comp.lang.c)
  • Re: How is a compiler written
    ... >> best way to write a compiler is with lex and yacc is not. ... Greg Comeau / Celebrating 20 years of Comeauity! ...
    (comp.lang.c)
  • Re: How is a compiler written
    ... > The Dragon Book is a good one suggestion, ... > best way to write a compiler is with lex and yacc is not. ...
    (comp.lang.c)
  • PHP Grammar
    ... I have been asked to do a compiler from PHP5 to C code using Lex and Yacc ...
    (php.general)
  • compiler complains about lex generated code
    ... I'm converting some complicated AIX lex and yacc to IRIX. ... The compiler warns that the computed value is out of range. ...
    (comp.sys.sgi.misc)