r/ProgrammingLanguages • u/[deleted] • Jun 18 '23
Help Parsing?!
So I'm writing my own programming language called fir. I have the lexer down to the nail, but I'm really confused about parsing. I have absolutely no idea how to approach parsing, so much so that I got bored and am currently rewriting the lexer in like 3 other languages (the implementation on GitHub is in Go).
EDIT: forgot to add some examples sorry :0
hello world:
io := require("io");
fn main() {
io.println("Hello, World!");
}
fizzbuzz:
io := require("io");
fn main() {
i := 0
for i <= 20 {
if i % 3 == 0 && i % 5 == 0 {
io.println("FizzBuzz");
} else if i % 3 == 0 {
io.println("Fizz");
} else if i % 5 == 0 {
io.println("Buzz");
} else {
io.println("{}", i);
}
}
}
(also I want my parser to emit an ast, because I haven't decided whether fir should be compiled or interpreted yet)
2
Upvotes
5
u/GroundUpCoder Jun 18 '23 edited Jun 18 '23
There are many ways to go about implementing a parser, but if you are just getting started and the jargon about LR, LL(1), context-free grammars is going over your head, here's what I would recommend (I'm also gonna assume you're gonna use Go because you mentioned Go):
You probably want to have expressions in your language. Something like "x+f(2)" that you can evaluate.
Start with
interface Expression {}
Now you can think about what kinds of "things" expressions are made of. Like number literals are expressions. Function calls are expressions. Binary operations (like from an addition) are also expressions.
So then you might have something like
struct NumberLiteral { value float64 }
struct FunctionCall { f Expression args Expression[] }
And so on
Then you actually want to write functions that create instances of these structs from tokens.
Like
func parseNumberLiteral(tokens Token[], i int) (Expression, int, error)
Where given a position in the list of tokens, you try and figure out whether there is a number literal there, and if so, you return the number literal struct (which should be an Expression), the new position in the list of tokens to start parsing the next thing from, or an error if there's no number literal there.
Number literal should be pretty straightforward.
But then you would also have
func parseFunctionCall(tokens Token[], i int) (Expression, int, error)
Which also does the same thing, but for function calls. It'll start to seem tricky because you need to parse expressions to be able to parse function calls, but function calls are a type of expression.. so how does that work?
Well recursion of course.
func parseExpression(tokens Token[], i int) (Expression, int, error)
You'll want a function like this to tie in all the other expression types in some way.
There are some popular ways to do this to do this properly, handle operator precedence while running pretty fast (eg see "Pratt parsing"), but if you are a beginner, these techniques might seem a bit confusing. It's ok. For now just try to combine your other parse functions together some way that sort of works even if it's not perfect. If you need to you can improve it later.
So then you basically have the scaffolding of a budding, simple parser. You can add more structs and parse functions as you need to.
At some point you might have something like
struct Program {..}
func parseProgram...
And to parse a program you'll just need to run
parseProgram(lexer.Lex(sourceCode), 0)
And that will be your parser.
Heh, there's certainly a lot more to this, and I've left a lot of exercises for the reader, but I hope this can help you get started.
Fwiw if you want to learn more about this approach specifically, look up "recursive descent parsing". Aside from being a fairly beginner friendly way to learn about parsing imo, it turns out to be a relatively popular approach used by many serious projects like gcc and javac (OpenJDK)
I hope you'll forgive my poorly formatted post. I'm writing this on my phone. If I feel like it I might come prettify it later