Re: A tiny self-hosting compiler used for teaching

ram@zedat.fu-berlin.de
Tue, 08 Sep 2026 09:14:35 +0000

          From comp.compilers

Related articles
[7 earlier articles]
Re: A tiny self-hosting compiler used for teaching gneuner2@comcast.net (George Neuner) (2026-09-05)
Re: A tiny self-hosting compiler used for teaching michael.lehn@uni-ulm.de (Michael Lehn) (2026-09-05)
Re: A tiny self-hosting compiler used for teaching michael.lehn@uni-ulm.de (Michael Lehn) (2026-09-05)
Re: A tiny self-hosting compiler used for teaching gneuner2@comcast.net (George Neuner) (2026-09-07)
Re: A tiny self-hosting compiler used for teaching christopher.f.clark@compiler-resources.com (Christopher F Clark) (2026-09-07)
Re: A tiny self-hosting compiler used for teaching michael.lehn@me.com (Michael Lehn) (2026-09-05)
Re: A tiny self-hosting compiler used for teaching ram@zedat.fu-berlin.de (2026-09-08)
Re: A tiny self-hosting compiler used for teaching martin@gkc.org.uk (Martin Ward) (2026-09-09)
| List of all articles for this month |
From: ram@zedat.fu-berlin.de
Newsgroups: comp.compilers
Date: Tue, 08 Sep 2026 09:14:35 +0000
Organization: Compilers Central
References: 26-09-010
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="14190"; mail-complaints-to="abuse@iecc.com"
Keywords: courses, design, C
Posted-Date: 08 Sep 2026 11:38:28 EDT

Christopher F Clark <christopher.f.clark@compiler-resources.com> wrote or quoted:
>While I agree with our esteemed moderator's point about aesthetics, it
>is worth noting that Wirth intentionally made Pascal easy to parse.
>Thus, the Pascal declaration notation was designed to be parsed with a
>one pass compiler.


    Wirth famously created another language that is even easier to
    parse, viz. "PL/0" for his 1976 book "Compilerbau", which then
    became "Oberon-0" for his 1996 book "Compiler Construction".


> C's notation does
>have the nice aspect that declaration syntax matches usage.


    For readers who might not know those rules:


| Parsing C Declarations
|
| To parse any C declaration by hand, follow the "Clockwise/Spiral Rule"
| (or Right-Left Rule). Always start at the identifier and move outward
| using this strict operator precedence:
|
| Precedence Rules
|
| 1. Groupings: Parentheses ( . . . ) surrounding a modifier
|
| 2. Post-fix operators (Right): Array subscripts [] and function
| parameters ()
|
| 3. Pre-fix operators (Left): Pointer stars *
|
| Step-by-Step Algorithm
|
| 1. Locate the identifier (the variable or function name). Say: "__ is
| a . . . ". When parsing an abstract declarator (e.g., inside a
| cast like "(int (*)[10])" or sizeof), find the location where the
| identifier would normally be placed.
|
| 2. Look to the right of the current position.
|
| - If [], say: "array of . . . " and move past it.
|
| - If (), say: "function returning . . . " and move past it.
|
| 3. Look to the left of the current position.
|
| - If *, say: "pointer to . . . " and move past it.
|
| - If a type qualifier (const, volatile), apply it to the element
| to the left.
|
| 4. Encountering Parentheses: If you hit a closing parenthesis ) on
| the right, you must consume all modifiers to the left until you
| hit the matching opening parenthesis (. Then, step outside the
| parentheses and repeat from Step 2.
|
| 5. Final Base Type: When the identifier and all modifiers are
| consumed, read the leftmost base type (e.g., int, char).
|
| Quick Reference Table
|
| Operator Reading Direction Meaning
| ---------- ------------------- ------------------------
| name Start here "name is a . . . "
| [N] Right " . . . array of N . . . "
| () Right " . . . function returning . . . "
| * Left " . . . pointer to . . . "
| const Left " . . . constant . . . "


    Lines marked with "| " come from my editing, where I start by writing
    prompts for the chatbot and then edit the generated texts and format
    them for Usenet. The chatbots might make mistakes and misrepresent
    or invent facts.


    For example, in "(int const *(*(*)( ))[ ])", we insert the "virtual
    identifier" "_" to get "(int const *(*(*_)())[ ])". So we now have
    "pointer to" and what remains is "(int const *(*_())[ ])"; this gives
    "function returning a pointer to", and "(int const *_[ ])" gives
    "array of pointers to constant ints". So it's, "pointer to function
    returning a pointer to an array of pointers to constant ints".


From: ram@zedat.fu-berlin.de (Stefan Ram)


Post a followup to this message

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