Live data from Hacker News

I Wrote a Compiler

blog.singleton.io

31–40 of 78 posts

Re: I Wrote a Compiler

#31
post #3

I thought a compiler, with no adjective or caveat, should turn a HLL into machine language. Isn't what this describes—turning BASIC into Go—more accurately described as a "pseudocompiler" or "Go compiler" or somesuch? I know Emacs is always said to have a "bytecode compiler" that processes Elisp code, not a "compiler" per se. Am I mistaken?

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 target a real, physical machine at all.

They translate one language into another. The line between compiler/transpiler just doesn't make sense to me.

Re: I Wrote a Compiler

#32
post #8
post #3

I thought a compiler, with no adjective or caveat, should turn a HLL into machine language. Isn't what this describes—turning BASIC into Go—more accurately described as a "pseudocompiler" or "Go compiler" or somesuch? I know Emacs is always said to have a "bytecode compiler" that processes Elisp code, not a "compiler" per se. Am I mistaken?

Strictly speaking it's a transpiler, but honestly the delta between the target language (Go) and the source language (BASIC) is very fluffy and wooly, from what I remember from my PL theory days the distinction was always fuzzy enough that people used whatever term felt right to them. An example off the top of my head — Chicken Scheme (call-cc.org) calls itself a compiler but it's target language is C

"Transpile" is a shortening of the older term "trans-compile". [0]

It's a subset. All transpilers are compilers. Not all compilers are transpilers.

[0] Amiga BASIC called itself a transcompiler, from memory.

Re: I Wrote a Compiler

#33

> 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 suspect that this is the more common opinion, especially when the desired outcome is real world use.

Recursive descent is surprisingly ergonomic and clean if one gets the heuristics right. Personally I find it way easier than writing BNF and its derivatives as you quickly get into tricky edge cases, slow performance and opaque errors.

Re: I Wrote a Compiler

#34
post #3

I thought a compiler, with no adjective or caveat, should turn a HLL into machine language. Isn't what this describes—turning BASIC into Go—more accurately described as a "pseudocompiler" or "Go compiler" or somesuch? I know Emacs is always said to have a "bytecode compiler" that processes Elisp code, not a "compiler" per se. Am I mistaken?

A compiler is any program that translates from one language to another (source-to-source, source-to-bytecode, or source-to-machine code), so translating BASIC to Go is indeed a proper compiler, just as GCC translating C to LLVM IR before machine code is still a compiler.

Re: I Wrote a Compiler

#35

> 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 write mine all by hand. It's the easiest part of a compiler to write, by far. It's also the least troublesome.

It's also the most annoying if you're writing a new language. You want to iterate on its ideas, but can't do so until you have a parser done.

I've been designing a few language concepts over the past year, and it feels 80% of this time has been writing and debugging parsers; by the time I get to the meat of language design - the shape of its AST, the semantics of it - any small syntactic change means going back to update the lexer and parser stage. Doesn't help that I can't settle on a syntax.

BTW I first started with PEG, which are nice in theory, but I feel the separation of lexing and parsing stage to very helpful to reduce boilerplate (handling whitespace in PEG is obnoxious). Later, I hand-wrote my parsers (in C), but it's gotten so repetitive I've dedicated a weekend to just learning lex/yacc (actually flex/bison). Even if parsers are easy to write, it's good to have higher level tools to reduce the tedium of it.

Re: I Wrote a Compiler

#37
post #35

> 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 write mine all by hand. It's the easiest part of a compiler to write, by far. It's also the least troublesome. It's also the most annoying if you're writing a new language. You want to iterate on its ideas, but can't do so until you have a parser done. I've been designing a few language concepts over the past year, and it feels 80% of this time has been writing and debugging parsers; by the time I get to the meat…

There are other tools for prototyping the grammar for a new language. There are several online tools that can interactively parse input based on a given grammar. The one I developed is: https://fransfaase.github.io/MCH2022ParserWorkshop/IParseStu... The page does contain a sample grammar and sample input. For the grammar for the grammar see: https://fransfaase.github.io/MCH2022ParserWorkshop/index.htm...

Re: I Wrote a Compiler

#38
post #29
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.

[flagged]

yes, that Eric Schmidt.

Re: I Wrote a Compiler

#39
post #3

I thought a compiler, with no adjective or caveat, should turn a HLL into machine language. Isn't what this describes—turning BASIC into Go—more accurately described as a "pseudocompiler" or "Go compiler" or somesuch? I know Emacs is always said to have a "bytecode compiler" that processes Elisp code, not a "compiler" per se. Am I mistaken?

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.

Re: I Wrote a Compiler

#40
post #35

> 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 write mine all by hand. It's the easiest part of a compiler to write, by far. It's also the least troublesome. It's also the most annoying if you're writing a new language. You want to iterate on its ideas, but can't do so until you have a parser done. I've been designing a few language concepts over the past year, and it feels 80% of this time has been writing and debugging parsers; by the time I get to the meat…

  > You want to iterate on its ideas, but can't do so until you have a parser done.
Embed your language into host language. It is simple to do even in C++.

Iterate on ideas till your heart content, then add syntax.

Post reply on HN