redfloyd

redfloyd

Greetings fellow alchemists !

I have started to write an open-source interpreter in Elixir (GitHub - nicolasdilley/dwarf-interpreter: The compiler for the dwarf's language · GitHub) one week ago and I decided to write some tutorials on each stage of the development of my new language. I have finished the first one on the lexer. If you are interested in interpreters or even compilers these tutorials would give you a good practical approach to write your own !

You can find these tutorials on my website : http://nicolasdilley.com/?p=1 I would love to have your opinion !

The language is called “Dwarf” and has already lots of functionalities. The next steps are implementing loops, a type system and a good stdlib. If you want to contribute on it feel free to pm me

Showing Posts 1 to 10

OvermindDL1

OvermindDL1

Yay another person making interpreters on the beam! We are getting quite a collection, from a couple of lisps to a multitude of typed languages to a beam native Lua to others. I love seeing blogs about the actually design though, looking forward to updates! :grinning_face_with_smiling_eyes:

OvermindDL1

OvermindDL1

The simplest strategy to decode the source program would be to look at each character. However, this would result in a huge amount of “if then else” statements and the algorithm would need to backtrack and look forward a lot. For example, for the keyword “print” we would have had to check for a “p” then for a “r”, if not print an error, then for a “i”, if not print an error, etc. This would result in an inefficient and a code which is hard to read. This is why we use regular expressions (regexps). Regular expression allows us to specify how words are layed out. For example, hereis a regular expression that checks if the input language contains the keyword print in Elixir:

Not really, PEG parsers are often quite faster than lexing/parsing steps without any slow regex or nasty cases. Like look at NimbleParsec for a simple context-less PEG library or my own ExSpirit PEG library if you need context information for parsing (slower but significantly more powerful, I’m hoping NimbleParsec gets this ability so I can deprecate ExSpirit, but the way it’s designed currently it’s very difficult to so no one has yet).

redfloyd

redfloyd OP

Thanks for the tips. Your library is very interesting ! I gave you a star :wink: However, I was talking about lexical analyzer in that article not parsers :slight_smile:

OvermindDL1

OvermindDL1

I know, but PEGs remove the need for the lexical analyzer entirely while making everything far more readable and faster as well. :slightly_smiling_face:

redfloyd

redfloyd OP

I did not know about them. I am doing it the old way then :slight_smile: I am using a recursive descent parser for the parsing.

OvermindDL1

OvermindDL1

Yep, recursive decent split lexical parsing steps is very classical, PEGs became more popular about 15 years ago, but they are not taught in schools or anything yet so few know about them, even though they are significantly better designs in my opinion, they can act as a lexer, a parser, both, an AST builder, an AST runner, any mix thereof, all with near the same code, while if implemented properly will run circles around the classical methods in speed while being far easier to read. :slightly_smiling_face:

redfloyd

redfloyd OP

Thanks I will definitely look more into it ! :slight_smile: Do you know of any good reference ?

OvermindDL1

OvermindDL1

What kind of reference are you wanting? The Wikipedia article (I’m on phone or is be giving substantially more direct links) talks about the basics well enough, but actual implementations are a bit different. C++'s Spirit PEG library is easily the fastest one in the world and will not be best due to its unique code generation, but it is easily the most powerful form out too with the most abilities except streaming parsing (but you can implement that yourself via a custom iterator or a thread anyway, it can even reverse a custom format back to strings too, or parse binary, or whatever, its PEG engine can parse back and forth between streams of anything to custom data structures of anything). There is one standalone generator it somewhere that can generate PEGs via a custom PEG language that compile to a whole host of languages, forgot it’s name but it’s been around for a long long time and it’s decently fast. There are many blogs about it as well and a few papers. What do you prefer? Or can Google for Parsing Expression Grammers. :slightly_smiling_face:

redfloyd

redfloyd OP

You are right lots of resources actually. Parsing Expression Grammars:
A Recognition-Based Syntactic Foundation should be a good start I think :slight_smile:

OvermindDL1

OvermindDL1

If you find good references you should put them in a blog post too so others can see as well! :grinning_face_with_smiling_eyes:

Where Next? Top

Trending in Guides/Tuts Top

krasenyp
You probably already know that <span>{nil}</span> in a HEEX template produces <span> </span> when rendered. I fin...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews