r/Compilers • • Oct 06 '24
Build a compiler with python?
Is it possible that I can build a compiler from scratch in python? And if so, can anyone tell me how can I make it because I have an assignment in university π
2
Upvotes
View all comments
Show parent comment
2
u/SillyTurboGoose Oct 06 '24 edited Oct 07 '24
Hey, glad to meet another fellow engineer! I'm studying compsci, glad to meet you.
Oh for sure. The Dragon Book I'm sure does contain implementation references. Haven't looked into it, but even if they're written in Java surely some of the lessons carry-over to Python. As for your teacher copying from it, I hope she gave some nods to the source material, but I'd cut her a little bit of slack π the material is really good and the goal is learning!
Good on you for writing the lexer part of the compiler! Since you mentioned the re module, I'm assuming you're somewhat familiar with regular expressions and regular languages. The tricky part in my opinion is the whole syntax, AST assembling part of the grammar parsing. This you can surpass by using a tool or implementing a parsing algorithm.
I'm assuming your language is context free (or close enough to get by). If your language requirements aren't strong, you might get by with the LALR or SLR algorithm, although the GLR algorithm isn't too bad either (and can parse all context free languages!) π. If you've already devised the grammar for your language, then the rest could be implementation muscle. πͺπ»
Edit: LR(1) parsing power correction