Recent

Author Topic: Create small language  (Read 1729 times)

Packs

  • Hero Member
  • *****
  • Posts: 508
Re: Create small language
« Reply #15 on: August 02, 2026, 02:53:22 pm »
Thank you 👍

ADMGNS

  • Jr. Member
  • **
  • Posts: 83
  • keep it simple and smart
Re: Create small language
« Reply #16 on: August 06, 2026, 02:40:26 am »
and also

TURBO PASCAL COMPILER INTERNALS

https://turbopascal.org/

Warfley

  • Hero Member
  • *****
  • Posts: 2084
Re: Create small language
« Reply #17 on: August 06, 2026, 03:05:02 pm »
I have tried to understand your code but it is not complete code . from where I will get full code for understanding .
Those are examples, but you probably are already missing the very foundations. There are lots of books and lectures on compiler construction, one of the most famous ones is the script for the Compiler Construction course from Niklaus Wirth he tought on the ETH Zürich: https://people.inf.ethz.ch/wirth/CompilerConstruction/CompilerConstruction1.pdf

It's quite theoretical, if you want something more hands on, there have been other suggestions here. At it's core a compiler consists of three quite distinct parts, the lexer/tokenizer, which is based on regular expressions, then the parse which is using a stack based state machine (Stack Automaton) for creating the abstract syntax tree, and then the backend, which is highly dependent on the target for your language.

In practice you usually do not need to construct the formal automatons, this is what I was trying to show with these examples. Instead of building a powerset automaton, you usually just construct some state machines that you iterate through to simulate such an automaton. Instead of building a stack automaton for the parsing you can make use of the stack of recursive calls to build a recursive descent parser for LL(1) languages. This does not mean you don't need the theory, because you need to know what you can and cannot express in your language. For example for tokenization, you are bound to what regex (and by this I mean true regex, not this backtracking stuff that modern regex engines do) is capable off (keyword: pumping lemma). For the parser, it really helps to write down the language formally and to check if it is left recursive, right recursive and if it can be resolved with a lookahead of 1.

What you can also look at is the Gold Parsing system. I've written a few gold engines (in Pascal, Typescript and Haskell) and wrote down (including some of the theory) how it works and how to build your own: https://github.com/Warfley/GoldEngines/blob/master/docs/index.md
Also I've built some tooling for VSCode to not have to use the awful editor that is shipped with GOLD: https://github.com/Warfley/gold-parser-tools

But either way I recommend to first start looking at least a bit at the theory, it will make things much easier. Once you know the basics, writing a small parser for a simple language from scratch only takes like a few days.
« Last Edit: August 06, 2026, 03:07:46 pm by Warfley »

Packs

  • Hero Member
  • *****
  • Posts: 508
Re: Create small language
« Reply #18 on: August 07, 2026, 04:04:53 am »
Thank you .

Packs

  • Hero Member
  • *****
  • Posts: 508
Re: Create small language
« Reply #19 on: August 07, 2026, 04:14:27 am »
Hi @Warfley,

Thank you, sir, for your help.

I tried the code you shared, but unfortunately it isn't working on my end. If possible, could you please share a complete working example? It would help me understand the entire process—from creating the lexer, parser, and tokens to compiling, building, and implementing a small custom syntax.

A minimal end-to-end example would make it much easier for me to learn how all the pieces fit together.

I would greatly appreciate your guidance. Thank you again for your time and support.

DavidL

  • New Member
  • *
  • Posts: 24
Re: Create small language
« Reply #20 on: August 07, 2026, 07:57:45 pm »
Hi @Warfley,

Thank you, sir, for your help.

I tried the code you shared, but unfortunately it isn't working on my end. If possible, could you please share a complete working example? It would help me understand the entire process—from creating the lexer, parser, and tokens to compiling, building, and implementing a small custom syntax.

A minimal end-to-end example would make it much easier for me to learn how all the pieces fit together.

I would greatly appreciate your guidance. Thank you again for your time and support.
Possibly you'd be better off starting with Chapter 5 in Wirth's Algorithms + Data Structures = Programs.  Just a thought...

Packs

  • Hero Member
  • *****
  • Posts: 508
Re: Create small language
« Reply #21 on: August 08, 2026, 08:16:34 am »
Sir,

chapter 5 of which book.

creaothceann

  • Sr. Member
  • ****
  • Posts: 430
Re: Create small language
« Reply #22 on: August 08, 2026, 09:42:48 am »
Chapter 5 in Wirth's Algorithms + Data Structures = Programs

Packs

  • Hero Member
  • *****
  • Posts: 508
Re: Create small language
« Reply #23 on: August 08, 2026, 10:21:22 am »
Please share pdf link

DavidL

  • New Member
  • *
  • Posts: 24
Re: Create small language
« Reply #24 on: August 08, 2026, 02:53:14 pm »
Please share pdf link
Please buy an actual book.

440bx

  • Hero Member
  • *****
  • Posts: 6578
Re: Create small language
« Reply #25 on: August 08, 2026, 03:13:31 pm »
Please share pdf link
There is a thing called Google, you may have heard of it.  It can find a pdf for you.

Everything you need to know about the _basics_ of scanning and parsing are found in "Brinch Hansen on Pascal Compilers" and, I provided a link to the book.

At this point, multiple people have provided you with more than enough information for you to get started. IOW, the language ball is in your court.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Packs

  • Hero Member
  • *****
  • Posts: 508
Re: Create small language
« Reply #26 on: August 08, 2026, 03:23:07 pm »
Share book name with author name

creaothceann

  • Sr. Member
  • ****
  • Posts: 430
Re: Create small language
« Reply #27 on: August 08, 2026, 05:11:32 pm »

ADMGNS

  • Jr. Member
  • **
  • Posts: 83
  • keep it simple and smart
Re: Create small language
« Reply #28 on: August 08, 2026, 09:52:19 pm »
from ground..

https://github.com/Jtkozzy/PL0

it is very easy to understand if you are not a newbie in pascal :)
« Last Edit: August 08, 2026, 10:08:12 pm by ADMGNS »

 

TinyPortal © 2005-2018