Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!howland.reston.ans.net!ix.netcom.com!netcom.com!ludemann
From: ludemann@netcom.com (Peter Ludemann)
Subject: Re: Why hasn't Prolog Taken over the World?
Message-ID: <ludemannCzw2Hy.B8y@netcom.com>
Summary: yacc vs DCGs ... yacc is "OK"
Keywords: Prolog DCG yacc bison lex flex LALR(1) parser
Organization: NETCOM On-line Communication Services (408 261-4700 guest)
References: <3a8c3n$qk0@hobbes.cc.uga.edu> <3adsk9$m6@sleepy.cs.keele.ac.uk>
Date: Sat, 26 Nov 1994 19:08:21 GMT
Lines: 66

In article <3adsk9$m6@sleepy.cs.keele.ac.uk>,
Paul Singleton <paul@cs.keele.ac.uk> wrote:
>Is industry resistant to the use of 'lex' or 'yacc'?  Does the fact that
>these are translated into 'C' help their acceptability?  Surely no-one
>ever reads or (heaven forbid) hacks the 'C' source which they generate? *

Having just written some code using yacc/lex (actually, bison/flex),
and being a big fan of DCGs, here are a few thoughts:

- yacc is blindingly fast (I remember when compiler-compilers took several
  minutes for a simple grammar; bison took 1.6 seconds on my 486/66 for a
  grammar with 353 rules (yacc was 2.6 seconds).  Flex was fast, too
  (under a second).  Of course, the compilation steps were slower
  ... about 12-16 seconds, depending on optimization level for the
  parser and 5.5-10 seconds for the lexical analyzer.  So, I could edit
  my grammar and do a complete rebuild in about 30 seconds.  Not as fast
  as Prolog, but still, not bad.

- The resulting compiler was fast enough (> 4000 lines/sec.).  I suspect
  that a DCG-written compiler would have been faster, but I'm not sure.

- The compiler executable was 78K.

- yacc could handle left recursion in grammar rules.  This was easy
  for me to handle it would have been difficult for somebody who hasn't
  spent his youth doing grammar transformations.  On the other hand,
  understanding the "shift/reduce conflict" messages isn't easy for
  beginners.

- Once yacc stopped complaining about the grammar, I knew it was
  unambiguous.  If I had written a DCG parser, I wouldn't have had
  such an assurance.

- I got cc (actually gcc and g++) and yacc/lex (also bison/flex) free
  on my machine.

Up to this point (the parser simply produces a modified syntax tree),
I would estimate that yacc/lex programming was about 50% more time
than using DCGs, mainly because of the extra typing I had to do in C
(I also wrote a trivial memory allocator and pretty-printer in C).

For the next step of optimization and code generation, Prolog would be
at least 2-3 times more productive, probably more.  But, if I were a
conventional programmer, I would already be "hooked" on the yacc/lex/C
paradigm and wouldn't even think of using Prolog.  [Ironic aside: in
today's newspaper there was an article on how VLIW design could
dominate in a few years ... the major book on this (the "Bulldog"
compiler) has about 2 pages explaining why Lisp should be used for
developing such as complex compiler.]

So, to summarize, the apparent advantages of using yacc/lex/C were:
- readily available tool that generated C (free, in this case).
- good enough productivity at the beginning (getting me "hooked").
- lots of people have already done it this way, and there's lots
  of documentation (even if it's not all correct).
- I can easily distribute the resulting code (if I had used Prolog,
  I would have had to package it up with an existing "free" Prolog
  distribution, such as SWI-Prolog).

Would Prolog be more acceptable if there were an LALR(1) parser
generator packaged with it?  I don't know.  But it wouldn't hurt.

BTW, if anybody wants to rewrite my parser in Prolog, I can provide
the source.  Just send me a note (ludemann@netcom.com).
-- 
Peter Ludemann                      ludemann@netcom.com
