Creating your own language
Asked Answered
O

5

9

If I were looking to create my own language are there any tools that would help me along? I have heard of yacc but I'm wondering how I would implement features that I want in the language.

Optical answered 13/12, 2008 at 19:33 Comment(1)
The canonical question for this is: https://mcmap.net/q/63539/-learning-to-write-a-compiler-closedTurbo
M
10

The first tool I would recommend is the Dragon Book. That is the reference for building compilers. Designing a language is no easy task, implementing it is even more difficult. The dragon book helps there. The book even reference to the standard unix tools lex and yacc. The gnu equivalent tools are called flex and bison. They both generate lexer and parser. There exist also more modern tools for generating lexer and parser, e.g. for java there are ANTLR (I also remember javacc and CUP, but I used myself only ANTLR). The fact that ANTLR combines parser and lexer and that eclipse plugin is availabe make it very comfortable to use. But to compare them, the type of parser you need, and know for what you need them, you should read the Dragon book. There are also other things you have to consider, like runtime environment, programming paradigm, ....

If you have already certain design ideas and need help for a certain step or detail the anwsers could be more helpful.

Mcglynn answered 13/12, 2008 at 19:49 Comment(0)
T
11

Closely related questions (all taken by searching on [compiler] on stackoverflow):

And similar topics (from the same search):


Edit: I know the stackoverflow related question search isn't what we'd like it to be, but did we really need the nth iteration of this topic? Meh!

Turbo answered 13/12, 2008 at 20:2 Comment(0)
M
10

The first tool I would recommend is the Dragon Book. That is the reference for building compilers. Designing a language is no easy task, implementing it is even more difficult. The dragon book helps there. The book even reference to the standard unix tools lex and yacc. The gnu equivalent tools are called flex and bison. They both generate lexer and parser. There exist also more modern tools for generating lexer and parser, e.g. for java there are ANTLR (I also remember javacc and CUP, but I used myself only ANTLR). The fact that ANTLR combines parser and lexer and that eclipse plugin is availabe make it very comfortable to use. But to compare them, the type of parser you need, and know for what you need them, you should read the Dragon book. There are also other things you have to consider, like runtime environment, programming paradigm, ....

If you have already certain design ideas and need help for a certain step or detail the anwsers could be more helpful.

Mcglynn answered 13/12, 2008 at 19:49 Comment(0)
G
4

ANTLR is a very nice parser generator written in Java. There's a terrific book available, too.

Gilmer answered 13/12, 2008 at 19:40 Comment(0)
M
2

I like Flex (Fast Lex) [Lexical scanner]
and Bison (A Hairy Yacc) [Yet another compiler compiler]

Both are free and available on all *NIX installations. For Windows just install cygwin.
But I old school.

By using these tools you can also find the lex rules and yacc gramers for a lot of popular languages on the internet. Thus providing you with a quick way to get up and running and then you can customize the grammers as you go.

Example: Arithmetic expression handling [order of precedence etc is a done to death problem] you can quickly get the grammer for this from the web.

An alternative to think about is to write a front-end extension to GCC.
Non Trivial but if you want a compiled language it saves a lot of work in the code generation section (you will still need to know love and understand flex/bison).

Mattland answered 13/12, 2008 at 20:2 Comment(0)
C
0

I never finished the complete language, I had used rply and llvmlite implements a simple foxbase language, in https://github.com/acekingke/foxbase_compiler

so if you want use python, rply or llvmlite is helpful. if you want use golang, goyacc maybe useful. But you should write a lexical analyzer by hard coding by hand. Or you can use https://github.com/acekingke/lexergo to simplify it.

Coadjutor answered 14/12, 2021 at 8:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.