4 ms·
The author forgot default cases in various switch (tok) statements: static void factor(void) { switch (type) { case TOK_IDENT: case TOK_NU
by kazinator 5y ago
The author forgot default cases in various switch (tok) statements:
static void
factor(void)
{
switch (type) {
case TOK_IDENT:
case TOK_NUMBER:
next();
break;
case TOK_LPAREN:
expect(TOK_LPAREN);
expression();
expect(TOK_RPAREN);
}
}
The TOK_ enumeration has a good many more members than just those three. If any other one occurs in the code, or an outright corrupt type value occurs, it silently falls through. If the error is handled at all, it will result in
some strange diagnostic, in some higher level code that thinks it has seen an expression with a factor in it.
Adding a break after the last block in a switch is a good idea; someone adding a new case might forget. (Wit diagnostic support, like GCC requiring a fallthrough comment, this is less important nowadays).
If a switch deliberately falls through for unmatched cases, have
default:
break;
to express that explicitly.
This whole approach of the parser consisting of void functions is poor for 2021. It's as if the author is deliberately trolling everyone who has ever taken any level of interest in functional programming.
The author will have a hard time fitting the necessary semantic actions into this code, like constructing an abstract syntax tree, and might succumb to the temptation to do it with some hacks which build the structure procedurally using global variables.
The parser would be a more future proof if it anticipated the need for backtracking. So that is to say, a function such as factor() above could be considered to be a pattern matching construct, and return a useful value indicating whether it matched or failed to match. Furthermore, if it it failed to match, then it would backtrack, leaving the state of the input stream as it was prior to entry.
You can then do interesting things, because you're essentially LL(k) now, like speculatively parse the input using several possible constructs in priority order, until one matches.
The code inconsistently mixes the use of concrete character constants like '.' and '{' and #define symbols that expand to character constants like #define TOK_PLUS '+'.
Using the character constants in the code as in expect('+') or whatever is clearer. The Yacc approach of using constants for abstract tokens, (which are enumerated starting at some value above 256), and concrete character constants for single-character tokens that denote themselves, is worth mimicking.
You're not going to redefine TOK_PLUS to something other than '+' in the future, and doing so would be like like changing #define FOUR 4 to #define FOUR 5. TOK_PLUS doesn't inform any better than '+', and isn't any easier to find with grep.
- p4bl0 5y agoI second all this. Reading the blogpost I was wondering how the author will do with this parser to actually build an AST rather than just validating syntax. The thing is that building an AST is typically the kind of thing where "do it with some hacks which build the structure procedurally using global variables" as kazinator said is a sure way to have horrible bugs. The inherently recursive nature of an AST doesn't mix very well with procedural manipulation of a global state.
- Someone 5y agoI don’t think there will be an AST. https://briancallahan.net/blog/20210814.html https://briancallahan.net/blog/20210814.html: “We will be writing a single-pass compiler for a simple language and immediately output our final output code as soon as our compiler has enough information to do so” That “as soon as” implies code will be generated before the entire program has been parsed. Also, for me single-pass implies “no AST”, as you would need at least one pass to construct one, and iterating over an AST counts as another pass for me.
- pwdisswordfish8 5y agoSingle-pass to me also implies a code generator that writes the object file directly, rather than compiling to C or assembly (or some other language) as an intermediate format. But words don't mean anything anymore, so long as people can plausibly convince others that making them feel bad by calling out improper use means that they should be allowed to use words however they want, like a child owed the opportunity to exercise their unbridled spirit.
- kazinator 5y agoNote that loading the program into memory is also a pass. Producing an installation image is also a pass, as is installing the program into the target. When we are talking about compiler passes, we don't count these, because they are outside the compiler. When we consider a compiler that puts out assembly language, if that works without producing an AST, just by generating assembly in a syntax-directed way as it followed the phrase structure of the code, then we call that a one-pass compiler. What it makes is assembly language, and it does that in one pass. Producing executable code from the assembly requires another pass, but that pass belongs to the assembler. The compiler is not even running, and so it can't be a pass of that compiler.