Ask coding questions

← Back to all posts
Ask me something about creating a programming language!
h
MocaCDeveloper (651)

Hi. I am willing to answer anything you might have a question about relating to creating/building a programming language!

Just drop the question below and I will reply to it and explain it step by step(if necessary)!

P.S: Don't mind the pinned repl

Comments
hotnewtop
Coder100 (18215)

Alright, I have made a language so I am slightly biased but I want to see how other's did it.

Lexer

I made it go index by index and create tokens (a struct) along the way. How did you do it? regex?

Parser

I used recursive descent. Apparently node.js uses it as well. What did you use? A parser combinator?

Interpreter

I first resolved everything (checked types and stuff) before interpreting it, which doubled execution time but also saved a lot of work as well. What did you do?

Alright, 3 questions, hope to get new insights after your answer!

fuzzyastrocat (1843)

@targetfanttthat I'd also like to ask about the Lexer part, since in my languages I've always done it with regex.

MocaCDeveloper (651)

@Coder100

Lexer

For the lexer, I made it to where it went character by character. I had the lexer pick up and 'tokenize' each keyword that it found, as well as 'tokenize' simple things like '+' and '=' etc

Parser

The parser was just the syntax setter for my programming language. I strongly tied the lexer and parser together, every time a new token was found(from the lexer), the parser would automatically work on the syntax depending on what that current token was.
The parser was the 'trial and error' for figuring out the syntax of the language, as well as picking up extra information(like variable names/variable assignment values) for the runtime.

Runtime(or, Interpreter)

I made it to where the runtime has a struct that holds the current token type, variable name, variable value and other ideal information that could possible be useful.
Depending on the token type(the token type can be something like: int, or can be something like a function declaration or a 'built-in' method like a printing statement) the runtime would then 'check over' the information.
I made the runtime to where it would check variable definition information as well as values and make sure the variable definition information matches up.
If it did, then the runtime would then move onto the next token.
The way I made my language, the runtime was working at the same time as the parser(only when the parser was parsing something like a variable definition or a function declaration etc).
If the parser had just gotten to the end of a function that, lets say, was setting up the syntax for the function, we would then pass information onto the runtime struct and call the "checking" function for the runtime.
My version of a "runtime" is basically just double-checking everything. The only thing my runtime does in terms of actually having an effect on the programming language is making built-in methods do things within the language.

The runtime step is a very confusing step. Very confusing to explain because the way I make my runtime is so so much different in terms of how and why I do it

BUT, I hope this kinda gives you an insight on how exactly I approach each step of building a programming language!

MocaCDeveloper (651)

@fuzzyastrocat What would you like to ask about the lexer? How I do it or..?

fuzzyastrocat (1843)

@targetfanttthat Yes, basically: did you use regex or do it like Coder100

MocaCDeveloper (651)

@fuzzyastrocat I build it from scratch. Figuring out how to skip over white-space, figuring out how to encounter each character as well as pick up keywords and 'tokenize' everything the language needs to be a language.
Also, what is regex? I have never heard of it

fuzzyastrocat (1843)

@targetfanttthat Good to know.
Regex is short for regular expressions — there's some info here: regexbuddy.com/regex.html

MocaCDeveloper (651)

@fuzzyastrocat Oh alrighty! If you need any help on building any part of a programming language from scratch just let me know and I will help you(but it seems like you know very much what you're doing already so..)(I kinda feel dumb compared to you)

fuzzyastrocat (1843)

@targetfanttthat Heh don't feel that way! There's always knowledge to be gained from others, even if you're really good at something. (I wouldn't have asked if I knew it already :D)

Coder100 (18215)

@targetfanttthat wow! Immediate parsing, never done that before, is it more performant?

MocaCDeveloper (651)

@Coder100 Yes, actually. I found it easier to just link the parser to the lexer right off the bat giving a little head start on the runtime so that in the end not only did we just go through a file and tokenize it(keyword by keyword, punctuation by punctuation), but we also shot that sun of a gun through 2 other steps right away in a matter of bang bop boom lol

Coder100 (18215)

why do you keep saying hi @python88

MocaCDeveloper (651)

@python88 Please stop..if you have a question then ask it, this isn't a discussion board

python88 (297)

@targetfanttthat i have a question; you know on python you can do inputs? hw do i do that on java?

Coder100 (18215)

you use the scanner class @python88

python88 (297)

@Coder100 how to write that in code? like scanner.calss or something else?

Bookie0 (6299)

hi!

this board is for asking questions, but you are in a way 'asking' indirectly if anyone needs help, so I guess thats fine lol

MocaCDeveloper (651)

@Bookie0 Ohhh..I thought if the post had a question or was related to answering questions it was to dwell in the 'ask' section. It can be moved if need be!

DynamicSquid (4937)

@Bookie0 i think this section is fine

JacobMcPherson1 (239)

Hi there! I know this post is like 9 months old but I need some help with my programming language (https://replit.com/@JacobMcPherson1/Blade#run/parser.py), I've made most of the basic stuff like output, variables, and basic math, but I dont know how the heck im gonna do all the advanced stuff like functions, loops, and conditionals. Can you please give me some tips and help? Thanks!

Leroy01010 (409)

in python how do you make a print variable?

python88 (297)

in java, how do you add inputs?

MocaCDeveloper (651)

@python88 Are you talking about something related to I/O?

python88 (297)

@targetfanttthat you jbiw how there are inputs in python? well how do you do that in java?

MocaCDeveloper (651)

@python88 Here is a documentation I found:
https://www.javatpoint.com/how-to-get-input-from-user-in-java

I don't write Java, I used to. This is what I always referenced when I wrote Java.
Hope this helps!

PattanAhmed (1406)

Well!
What is a Shell in Python
@JosephSanthosh LoL!
Thanks!

Sorry for disturbing you, if you don't like me to ping you!

MocaCDeveloper (651)

@PattanAhmed I am kinda confused on this. So you're asking him what a Shell is in Python? Sorry for the confusion.

PattanAhmed (1406)

@targetfanttthat Nope, I am asking you about the answer!
Because he once asked me this question!
So, I am pinging him too, so that he can see the answer if you have answered it.
Thanks!

MocaCDeveloper (651)

@JosephSanthosh You want to know about the Python Shell?
A Shell in Python: Also known as "Python Interactive Shell", can execute just one line of python code at a time and is ran inside a terminal.
Basically, just a terminal version of the language that executes line by line.

I think that answers the question..if not please let me know

PattanAhmed (1406)

@targetfanttthat Yes that's cool
But instead of my name in starting type @JosephSanthosh
Thanks!

MocaCDeveloper (651)

@PattanAhmed Oh you wanted me to ping your friend instead of you?? I am so sorry!!

PattanAhmed (1406)

@targetfanttthat Don't be confused...
Sometimes, somethings will be confusing everywhere...

Me myself is confused too

LoL!

k9chelsea2 (734)

how do i create stuff like if, while, boolean commands in py

MocaCDeveloper (651)

@k9chelsea2 So you want to create keywords for your language in python?

k9chelsea2 (734)

in a sense yes I just want to know how to define them not how to add the words to the lang

MocaCDeveloper (651)

@k9chelsea2 Well, I have never really done anything related to building a language in Python. I believe that there is a library where you can append keyword names to a list though, however I am not sure what that library is called.

If I find the post where the 2 people were talking about it I will let you know, but other than that I do not know much about defining keywords in Python..

MocaCDeveloper (651)

@k9chelsea2 I know I said I would ping you to a post, but forget that I found some documentation!

There is a Python Library named PLY that is heavily used for defining language keywords for a programming language that is going to be written in Python.

I think that if anyone was smart enough, they would use PLY if they attempted to create a language in Python.

Here is the documentation: https://www.dabeaz.com/ply/ply.html

If you need any help, please let me know

MocaCDeveloper (651)

@k9chelsea2 Yup. Let me know how it goes!!

[deleted]

How does a makefile work and how do I make one in C? I have tried to make one but it didn't work...

MocaCDeveloper (651)

@ImmaEatYouMan Well, I personally only use a Makefile when using another compiler for my C projects!

Also, you can't make a Makefile in C..I don't think anyway

Coder100 (18215)

a makefile is basically a bash shell but with slightly more features, it's good for compiling C code but otherwise can be used for anything. @ImmaEatYouMan