←back to thread

35 points bluetomcat | 1 comments | | HN request time: 0.001s | source

A small tool that parses C declarations and outputs a simple visual representation at each stage, as it encounters arrays, pointers or functions.

The program uses a table-driven lexer and a hand-written, shift-reduce parser. No external dependencies apart from the standard library.

Show context
pcfwik ◴[] No.46187443[source]
Since this is about C declarations: for anyone who (like me) had the misfortune of learning the so-called "spiral rule" in college rather than being taught how declarations in C work, below are some links that explain the "declaration follows use" idea that (AFAIK) is the true philosophy behind C declaration syntax (and significantly easier to remember/read/write).

TL;DR: you declare a variable in C _in exactly the same way you would use it:_ if you know how to use a variable, then you know how to read and write a declaration for it.

https://eigenstate.org/notes/c-decl https://news.ycombinator.com/item?id=12775966

replies(2): >>46188202 #>>46189393 #
1. userbinator ◴[] No.46189393[source]
if you know how to use a variable, then you know how to read and write a declaration for it.

In other words, the precedence of operators in a declaration have exactly the same precedence as in its use.