Wrestling with phase 1 of a C compiler

Christopher F Clark <christopher.f.clark@compiler-resources.com>
Mon, 12 Sep 2022 21:45:54 +0300

          From comp.compilers

Related articles
Wrestling with phase 1 of a C compiler luser.droog@gmail.com (luser droog) (2022-09-07)
Re: Wrestling with phase 1 of a C compiler luser.droog@gmail.com (luser droog) (2022-09-09)
Re: Wrestling with phase 1 of a C compiler luser.droog@gmail.com (luser droog) (2022-09-11)
Wrestling with phase 1 of a C compiler christopher.f.clark@compiler-resources.com (Christopher F Clark) (2022-09-12)
Re: Wrestling with phase 1 of a C compiler gah4@u.washington.edu (gah4) (2022-09-12)
Re: Wrestling with phase 1 of a C compiler christopher.f.clark@compiler-resources.com (Christopher F Clark) (2022-09-13)
Re: source languages, was Wrestling with phase 1 of a C compiler gneuner2@comcast.net (George Neuner) (2022-09-14)
Re: Wrestling with phase 1 of a C compiler luser.droog@gmail.com (luser droog) (2022-09-14)
Re: Wrestling with phase 1 of a C compiler gah4@u.washington.edu (gah4) (2022-09-14)
Re: Wrestling with phase 1 of a C compiler luser.droog@gmail.com (luser droog) (2022-09-15)
| List of all articles for this month |

From: Christopher F Clark <christopher.f.clark@compiler-resources.com>
Newsgroups: comp.compilers
Date: Mon, 12 Sep 2022 21:45:54 +0300
Organization: Compilers Central
References: 22-09-001
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="99560"; mail-complaints-to="abuse@iecc.com"
Keywords: design
Posted-Date: 12 Sep 2022 15:46:44 EDT

While I hate being extremely pessimistic, you are going in absolutely the
wrong direction.


If you want a functional language, use one. Don't try to turn C into a
functional language. Even if you succeed, you will have made a mess that
no one (probably not even you) will actually understand.


I say that from experience. Early on in writing Yacc++, we did macros that
allowed us to write object-oriented C, which had C++-like semantics (ala
1986, pre-templates, pre STL, etc.) with nice ABC (abstract base classes,
plus some inheritance and polymorphism). It worked. However, it was a
challenge to understand. Our wisest move was at 2.0 rewriting everything
in C++ and not trying to have a "C" layer with similar functionality. The
code was an order-of-magnitude simpler and even making a translation that
did C# was relatively trivial from that code.


C is a nice small imperative language. It's fine for expressing those
kinds of semantics. The C preprocessor is both simple and powerful, but it
doesn't change the nature of C. You cannot really do "compilation" in the
C preprocessor. And, if you really want a parser combinator library, you
want it to do some compilation. Otherwise, you have just masked the fact
that you are doing hand-written recursive descent. And, if you want to do
hand-written recursive descent, do it. Don't put lipstick on a pig and
take it to the opera.


There is a reason your attempts are failing. You don't have good closures
or co-routines in C. You cannot get them easily. You cannot really
interact well with the function stack in C. And, your efforts to put the
state behind pointers, while necessary only get you part of the way there.


So, if you are lucky, you will have something that looks like a parser
combinator language, but which actually has either
  - broken semantics (i.e. cases that don't work properly and don't warn you
that they don't)
or
- is very complicated.




--
******************************************************************************


Chris Clark email: christopher.f.clark@compiler-resources.com
Compiler Resources, Inc. Web Site: http://world.std.com/~compres
23 Bailey Rd voice: (508) 435-5016
Berlin, MA 01503 USA twitter: @intel_chris



Post a followup to this message

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