Recent

Author Topic: Tutorial on creating a compiler  (Read 27121 times)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Tutorial on creating a compiler
« Reply #90 on: January 09, 2020, 05:24:21 pm »
For completeness sake, a rather nice book (thanks to this post by Ñuño): Robert Nystrom's Crafting Interpreters.

This caught my atention at once:
Quote from: Crafting Interpreters
[...] this text is lighter on theory than others. As we build each piece of the system, I will introduce the history and concepts behind it. I’ll try to get you familiar with the lingo so that if you ever find yourself in a cocktail party full of PL (programming language) researchers, you’ll fit in.

But we’re mostly going to spend our brain juice getting the language up and running. This is not to say theory isn’t important. Being able to reason precisely and formally about syntax and semantics is a vital skill when working on a language. But, personally, I learn best by doing. It’s hard for me to wade through paragraphs full of abstract concepts and really absorb them. But if I’ve coded something, run it, and debugged it, then I get it.

That’s my goal for you. I want you to come away with a solid intuition of how a real language lives and breathes. My hope is that when you read other, more theoretical books later, the concepts there will firmly stick in your mind, adhered to this tangible substrate.
(emphasis mine)
« Last Edit: January 09, 2020, 05:27:36 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8527
Re: Tutorial on creating a compiler
« Reply #91 on: January 09, 2020, 05:46:38 pm »
Thanks for the cross-posting, which gets everything usefully in one place.

MarkMLl


MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

JdeHaan

  • Full Member
  • ***
  • Posts: 171
Re: Tutorial on creating a compiler
« Reply #92 on: January 09, 2020, 06:02:12 pm »
Bob Nystrom explains very clearly the step by step creation of an interpreter (in Java) and compiler (in C). I used his text to create an interpreter in Free Pascal (https://github.com/jdehaan2014/GearLanguage ), and now busy to translate his C-code into FP for the compiler part.
He also wrote an article on Pratt parsers :
http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1116
Re: Tutorial on creating a compiler
« Reply #93 on: January 10, 2020, 04:42:04 am »
Thanks for the cross-posting, which gets everything usefully in one place.
Adding the links below, we'll have an overview of recent useful extra information:

Best Tutorial
https://forum.lazarus.freepascal.org/index.php/topic,45078.0.html

Wiki stub: Make your own compiler, interpreter, parser, or expression analyzer
https://forum.lazarus.freepascal.org/index.php/topic,42556.0.html

Book recommendation on programming language concepts and design ?
https://forum.lazarus.freepascal.org/index.php/topic,42156.0.html

Books on building an object oriented compiler
https://forum.lazarus.freepascal.org/index.php/topic,47844.0.html

Books and Articles on building a debugger
https://forum.lazarus.freepascal.org/index.php/topic,48075.0.html

Standard Pascal ISO 7185:1990 and Extended Pascal ISO/IEC 10206:1991
https://forum.lazarus.freepascal.org/index.php/topic,47587.0.html

Modula, Oberon or Ada
https://forum.lazarus.freepascal.org/index.php/topic,47800.0.html

A suggestion for a new FPC feature
https://forum.lazarus.freepascal.org/index.php/topic,45832.0.html

P65Pas a new 6502 Pascal compiler
https://forum.lazarus.freepascal.org/index.php/topic,42408.0.html

Midnight trolling: Don't take everything serious!
https://forum.lazarus.freepascal.org/index.php/topic,48033.0.html

MarkMLl

  • Hero Member
  • *****
  • Posts: 8527
Re: Tutorial on creating a compiler
« Reply #94 on: January 03, 2026, 01:57:44 pm »
https://stackoverflow.com/questions/29908207/how-to-compile-vtprolog-from-source-code

That's the one, although I can't remember if I seriously tackled it with FPC (it was discussed on the ML, and there's an embedded garbage collector which was considered problematic).

PDFs temporarily at https://www.kdginstruments.co.uk/public/vt_prolog1.pdf https://www.kdginstruments.co.uk/public/vt_prolog2.pdf in case anybody wants them, they were done for the original authors but were never republished. Still a fairly good read.

I've just uploaded all the files I have to https://github.com/MarkMLl/vtprolog . Good for TP3 and TP5.5 on DOS, still haven't tried with FPC on any platform.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Weiss

  • Full Member
  • ***
  • Posts: 231
Re: Tutorial on creating a compiler
« Reply #95 on: January 04, 2026, 10:07:46 am »
JdeHaan posted Gear interpreter - thank you. Gear includes a good tutorial. Awesome work. Actually, tutorial is a book on interpreter writing on its own. I wish I had it when I started.

I am still working on my version of interpreter, which is a time-consuming catastrophe, and it will never be as good Gear.

By the way, pretty good tutorial on EBNF here:  chrome-extension://efaidnbmnnnibpcajpcglclefindmkaj/https://ics.uci.edu/~pattis/ICS-33/lectures/ebnf.pdf

440bx

  • Hero Member
  • *****
  • Posts: 6065
Re: Tutorial on creating a compiler
« Reply #96 on: January 04, 2026, 11:16:42 am »
JdeHaan posted Gear interpreter - thank you. Gear includes a good tutorial. Awesome work. Actually, tutorial is a book on interpreter writing on its own. I wish I had it when I started.
I second that.  I had a good look at the Gear interpreter, it's quite good.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

microxa

  • New member
  • *
  • Posts: 9
Re: Tutorial on creating a compiler
« Reply #97 on: January 16, 2026, 02:19:38 pm »
There is another project called zxlikepascal. Although its target platform is the Z80 processor, its internal engine uses p-code (a minimalistic analogue of LLVM).

Thanks to this approach, a rather serious problem was solved—such as optimization.

Due to relatively simple compiler implementation and use of P-code, it was possible to adapt the compiler for the PDP11 architecture.

440bx

  • Hero Member
  • *****
  • Posts: 6065
Re: Tutorial on creating a compiler
« Reply #98 on: January 16, 2026, 02:58:56 pm »
I was looking for zxlikepascal's source and didn't find it but, in the process I found the source for another ZX Pascal compiler, PASTA80.

In case someone is interested, here is the link to it:
https://github.com/pleumann/pasta80

The compiler is written in FPC :)

FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

microxa

  • New member
  • *
  • Posts: 9
Re: Tutorial on creating a compiler
« Reply #99 on: January 16, 2026, 05:17:33 pm »
https://zx-pk.ru/threads/24967-zx-like-pascal.html

It seems like the author has abandoned it.

I'm familiar with the Pasta80 project (I modified it for d6), but I can't say that it's as simple or flexible in terms of other architectures.
p.s
Regarding the PDP-11 platform, it is possible to assemble a GNU Pascal compiler for it, which seemed to produce excellent code. However, using the ZXLikePascal engine, we managed to obtain a code generator that is faster than GCC and much simpler in implementation. And at the same time improve our knowledge in such hello-worlds...

p.p.s

http://www.z80.eu/pas-compiler.html

Amazing curious case...
My interest in zxlike was further fueled by discovering an assembler compiler called ASMZ80... Somehow it managed to be ported to CP/M for TP3... which didn't prevent it from being re-portable back to a 32-bit platform...
« Last Edit: January 16, 2026, 07:17:22 pm by microxa »

440bx

  • Hero Member
  • *****
  • Posts: 6065
Re: Tutorial on creating a compiler
« Reply #100 on: January 16, 2026, 06:17:05 pm »
https://zx-pk.ru/threads/24967-zx-like-pascal.html
Thanks for the link.  I'm always interested in checking out how different compiler writers choose to implement the compiler.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Roland57

  • Hero Member
  • *****
  • Posts: 552
    • msegui.net
Re: Tutorial on creating a compiler
« Reply #101 on: January 16, 2026, 08:29:55 pm »
While we are on that topic, it's sad that MSElang be abandoned.
My projects are on Codeberg.

 

TinyPortal © 2005-2018