EEVblog Electronics Community Forum

Products => Computers => Programming => Topic started by: Omega Glory on December 12, 2021, 03:38:24 pm

Title: Wonderful Tutorial For Writing Interpreters/Compilers
Post by: Omega Glory on December 12, 2021, 03:38:24 pm
I've been trying to learn how to write a compiler for a custom CPU I made, when I came across this incredible series of blog posts on creating a Pascal interpreter from scratch. It covers lexing, parsing, semantic analysis, scope resolution, and much more. The code is written in Python, and everything is much more readable than similar tutorials I've seen online. This was too good not to share: https://ruslanspivak.com/lsbasi-part1/ (https://ruslanspivak.com/lsbasi-part1/)
Title: Re: Wonderful Tutorial For Writing Interpreters/Compilers
Post by: bobcat2000 on December 22, 2021, 07:51:21 am
Oh This is good.  So much memory taking this class in school.  I think I got an A on this class.
My first calculator program written in C using a Stack... Good Time!!!

https://www.amazon.com/Compilers-Principles-Techniques-Alfred-Aho/dp/0201100886/ref=sr_1_1?crid=3MH2S7KFSSXMN&keywords=%E2%80%8E+978-0201100884&qid=1640159400&s=books&sprefix=978-0201100884%2Cstripbooks%2C265&sr=1-1 (https://www.amazon.com/Compilers-Principles-Techniques-Alfred-Aho/dp/0201100886/ref=sr_1_1?crid=3MH2S7KFSSXMN&keywords=%E2%80%8E+978-0201100884&qid=1640159400&s=books&sprefix=978-0201100884%2Cstripbooks%2C265&sr=1-1)
Title: Re: Wonderful Tutorial For Writing Interpreters/Compilers
Post by: DiTBho on December 22, 2021, 12:36:31 pm
I've been trying to learn how to write a compiler for a custom CPU I made

Assembly compiler?
For a "C--" or "C" compiler, the best reference (in my opinion) is LCC.
It has a good book, and good sources.
Title: Re: Wonderful Tutorial For Writing Interpreters/Compilers
Post by: SiliconWizard on December 22, 2021, 06:54:53 pm
It's hard not to mention Wirth's "Compiler construction": https://people.inf.ethz.ch/wirth/CompilerConstruction/index.html
Title: Re: Wonderful Tutorial For Writing Interpreters/Compilers
Post by: Omega Glory on December 23, 2021, 03:20:35 pm
Assembly compiler?
For a "C--" or "C" compiler, the best reference (in my opinion) is LCC.
It has a good book, and good sources.

Well I've written an assembler for my CPU, so I'm planning on having my compiler generate assembly. That way it might be a little easier to debug instead of going straight to hex. Hmm, I've heard of the Tiny C Compiler, but not the Little C Compiler. I'll check that out. My idea is to come up with a very simple high level language, but it will probably be similar to C.

Oh This is good.  So much memory taking this class in school.  I think I got an A on this class.
My first calculator program written in C using a Stack... Good Time!!!

https://www.amazon.com/Compilers-Principles-Techniques-Alfred-Aho/dp/0201100886/ref=sr_1_1?crid=3MH2S7KFSSXMN&keywords=%E2%80%8E+978-0201100884&qid=1640159400&s=books&sprefix=978-0201100884%2Cstripbooks%2C265&sr=1-1 (https://www.amazon.com/Compilers-Principles-Techniques-Alfred-Aho/dp/0201100886/ref=sr_1_1?crid=3MH2S7KFSSXMN&keywords=%E2%80%8E+978-0201100884&qid=1640159400&s=books&sprefix=978-0201100884%2Cstripbooks%2C265&sr=1-1)

Ah yes, the Dragon book. I have heard a lot of good things about that. I don't own a copy now because of the price, but maybe I should bite the bullet. (edit: I should have looked harder. There are a few used ones for a good price).

It's hard not to mention Wirth's "Compiler construction": https://people.inf.ethz.ch/wirth/CompilerConstruction/index.html (https://people.inf.ethz.ch/wirth/CompilerConstruction/index.html)

Thank you very much! That looks very good!
Title: Re: Wonderful Tutorial For Writing Interpreters/Compilers
Post by: rstofer on December 29, 2021, 07:16:19 pm
It's hard not to mention Wirth's "Compiler construction": https://people.inf.ethz.ch/wirth/CompilerConstruction/index.html

And we should mention the syntax diagrams used to create the Pascal compilers.  It is a straightforward translation from syntax to code.  BNF is fine but I like the diagrams.
Title: Re: Wonderful Tutorial For Writing Interpreters/Compilers
Post by: cfbsoftware on January 15, 2022, 09:54:34 pm
And we should mention the syntax diagrams used to create the Pascal compilers.  It is a straightforward translation from syntax to code.  BNF is fine but I like the diagrams.
I agree! When I first saw these in the 1970's I immediately understood the Pascal syntax. Consequently I have produced a set for the Oberon language:

https://www.astrobe.com/OberonSyntax/html/OberonSyntax.htm (https://www.astrobe.com/OberonSyntax/html/OberonSyntax.htm)

The diagrams were generated using 'EBNF Visualiser' which you can download from:

http://dotnet.jku.at/applications/Visualizer/index.html (http://dotnet.jku.at/applications/Visualizer/index.html)

You just feed it the EBNF text definitions and it creates the diagrams for you.

Title: Re: Wonderful Tutorial For Writing Interpreters/Compilers
Post by: Cerebus on January 15, 2022, 11:44:28 pm
It's hard not to mention Wirth's "Compiler construction": https://people.inf.ethz.ch/wirth/CompilerConstruction/index.html

And we should mention the syntax diagrams used to create the Pascal compilers.  It is a straightforward translation from syntax to code.  BNF is fine but I like the diagrams.

"Railroad diagrams" as I was introduced to them. Their existence predates their use in the Pascal User Manual. I first saw them in Burroughs' manuals from well before when the Pascal User Manual was published. Quite where and when they were first used is unclear to me. They definitely have their place and I'd say they are probably the best form for a programmer's reference manual. For a language implementor nothing can replace a formal grammar in BNF (or whatever other formalism for grammars one chooses/is best fitted to one's implementation method).