Live data from Hacker News

I Wrote a Compiler

blog.singleton.io

51–60 of 78 posts

Re: I Wrote a Compiler

#51
post #14

Earlier quoted context omitted.

I know BASIC is kind of a “bad” language, but there’s something so delightful about it. If we’re plugging TinyBASIC projects that others might find interesting, I made an MMO TinyBASIC REPL the other day: http://10klob.com/

BASIC is an amazing language that computing novices (including humanities majors) could learn in an afternoon, that could be efficiently compiled or compactly interpreted, that was small enough to support dozens of interactive users on a mainframe or minicomputer, or to fit into a tiny 8-bit microcomputer – and yet was largely equivalent to FORTRAN in terms of its expressive power. I think the closest modern equivale…

Well this afternoon bit...

Advanced BASICs are too big for that, and in less advanced ones you get to POKE the hardware to do certain things. Which means you get to learn a bunch of hardware and machine code. That's not all bad though!

Re: I Wrote a Compiler

#52

> It’s possible to write the lexer and parser entirely by hand I write mine all by hand. It's the easiest part of a compiler to write, by far. It's also the least troublesome. One advantage of doing them by hand is better, more targeted error messages are easier to fold in.

I don't think I know any lang devs who don't write their own parser. Beyond just being the easiest part it's also the most fun, and it's the thing that gets you sucked in the first place. If you're using a generator you're missing out.

Re: I Wrote a Compiler

#54

> It’s possible to write the lexer and parser entirely by hand I write mine all by hand. It's the easiest part of a compiler to write, by far. It's also the least troublesome. One advantage of doing them by hand is better, more targeted error messages are easier to fold in.

In my lifetime parsers have gone from: written by hand with great effort, to: use a parser generator to be sure the grammar is correct and save effort, to: just write by hand because it's not that hard and error reporting, to: just use AI.

Out of curiosity, have you had luck using LLMs for parser generation? I should really try it out with my parser combinator library; would be cool to be able to throw in sample code and some basic instructions and get a usable parser out for prototyping. Think I might try that next time I build a new language, just for fun.

Re: I Wrote a Compiler

#55

How does one debug a grammar? This has always eluded me. Where is the special tooling to help spot errors?

If you use something like Antlr (https://www.antlr.org/) it has visualization tools and an online tester (http://lab.antlr.org/). There are also tools for visualizing generic EBNF grammars like this: https://jacquev6.github.io/DrawGrammar/

Re: I Wrote a Compiler

#56
post #6

>The original authors of yacc were Mike Lesk and Eric Schmidt - yes that Eric Schmidt. Incorrect, they were authors of lex. yacc was authored by Stephen Johnson. Surprising to me is all the authors are still around, even though the tools are over 50 years old!. Shows how young computer science field is.

Eric Schmidt had a career at Sun Microsystems, though he is better known for Google of course.

Re: I Wrote a Compiler

#57

Earlier quoted context omitted.

A compiler is a language translator. To me it makes no difference whether it generates machine code (one type of language, machine executable), asm (equivalent language but which needs additional preprocessing into machine code), or some other language (which also needs additional preprocessing into machine code, but the tool for this is another compiler). Or it might output Java bytecode or MSIL, which doesn't even…

Let's put it this way: if a program translates code written in a "smarter" language into code written in a "stupider" language, then it is okay to call it a compiler. If it translates a restricted subset of BASIC into Go, it doesn't really do anything beyond replacing one syntax with another.

"Smarter" and "stupider" are completely subjective terms.

It translates one language into another, while maintaining logical correctness and its original semantics. Some cases are easier to do than others, but it doesn't change the nature of what is being done: language translation.

It isn't as simple as you think. Ex. in basic, the goto statement allows you to jump from anywhere to anywhere. Go has restrictions, such as it may not jump over variables coming into scope (being declared), nor can it jump into another scope, only outwards. So, for starters, this probably needs to scan the BASIC code for variables and hoist everything to the top as well as rewrite any code where it is trying to jump into a deeper scope (ex. jump from the topmost scope of a function into the body of a for loop).

Yes, it is a compiler. Maintaining semantics.

Re: I Wrote a Compiler

#58
This is great! I feel like there's been a resurgence of interest in language design and compilers of late. I have no business having an interest in this kind of thing, but even I have been inspired to try and make the changes to javascript that I think would improve it: https://chicory-lang.github.io/

Re: I Wrote a Compiler

#59
post #6

>The original authors of yacc were Mike Lesk and Eric Schmidt - yes that Eric Schmidt. Incorrect, they were authors of lex. yacc was authored by Stephen Johnson. Surprising to me is all the authors are still around, even though the tools are over 50 years old!. Shows how young computer science field is.

Eric Schmidt had a career at Sun Microsystems, though he is better known for Google of course.

yeah, he was 20 when he co-authored lex. Had a good career at Sun and was brought into Google as adult supervision and that was 20 years ago! Kind of living example of the (almost) entire history of computing.

Re: I Wrote a Compiler

#60
post #6

>The original authors of yacc were Mike Lesk and Eric Schmidt - yes that Eric Schmidt. Incorrect, they were authors of lex. yacc was authored by Stephen Johnson. Surprising to me is all the authors are still around, even though the tools are over 50 years old!. Shows how young computer science field is.

Who coauthored "The C Programming Language" anyway? Oh right, prof. Kernighan. https://www.cs.princeton.edu/~bwk/

The “k” in awk, of course. I’ve never met Kernighan or Weinberger personally. I did have a great conversation once with Al Aho and Larry Wall, largely about the history and mutual influence of programming languages upon one another.
Post reply on HN