Author Topic: Wonderful Tutorial For Writing Interpreters/Compilers  (Read 3187 times)

0 Members and 1 Guest are viewing this topic.

Offline Omega GloryTopic starter

  • Regular Contributor
  • *
  • Posts: 86
  • Country: us
    • Ezra's Robots
Wonderful Tutorial For Writing Interpreters/Compilers
« 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/
 
The following users thanked this post: kripton2035, rstofer, spostma

Offline bobcat2000

  • Regular Contributor
  • *
  • Posts: 209
  • Country: us
Re: Wonderful Tutorial For Writing Interpreters/Compilers
« Reply #1 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
 

Offline DiTBho

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: Wonderful Tutorial For Writing Interpreters/Compilers
« Reply #2 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.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14463
  • Country: fr
Re: Wonderful Tutorial For Writing Interpreters/Compilers
« Reply #3 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
 
The following users thanked this post: Omega Glory

Offline Omega GloryTopic starter

  • Regular Contributor
  • *
  • Posts: 86
  • Country: us
    • Ezra's Robots
Re: Wonderful Tutorial For Writing Interpreters/Compilers
« Reply #4 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

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

Thank you very much! That looks very good!
« Last Edit: December 23, 2021, 03:38:14 pm by Omega Glory »
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Wonderful Tutorial For Writing Interpreters/Compilers
« Reply #5 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.
 

Offline cfbsoftware

  • Regular Contributor
  • *
  • Posts: 116
  • Country: au
    • Astrobe: Oberon IDE for Cortex-M and FPGA Development
Re: Wonderful Tutorial For Writing Interpreters/Compilers
« Reply #6 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

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

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

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

Chris Burrows
CFB Software
https://www.astrobe.com
 
The following users thanked this post: rstofer

Offline Cerebus

  • Super Contributor
  • ***
  • Posts: 10576
  • Country: gb
Re: Wonderful Tutorial For Writing Interpreters/Compilers
« Reply #7 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).
Anybody got a syringe I can use to squeeze the magic smoke back into this?
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf