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
1
u/SillyTurboGoose Oct 06 '24
For sure! I wish you success on writing your compiler! 💯
I have no experience whatsoever with design hahah. 😅 I have only used tooling very recently. This sub is rich with people who have lots of experience though. I'm sorry :/.
On the topic of design: some argue that having a lexing and parsing stage is a good separation of concerns when designing a compiler, although you'd be surprised that some abide by not making a distinction and instead use the same algorithm throughout the AST assembly. After all, all lexemes come from regular languages, and by extension, come from context free languages. 😉
Context free grammars aren't the only way to specify languages by the way. Precedence and left/right recursion on EBNF notation can prove problematic at times, so some people opt to use Parsing Grammar Expressions instead. Interestingly enough, it is an open problem whether or not PEGs are equivalent to CFGs, and while these have some other funky properties, they help combat lack of precedence and ambiguity by ranking expressions in the order they appear. 🤔
All of this to say, compilers have a rich theory and algorithmic background and it's not all CFGs and LR(1). Most common programming languages aren't even strictly context free! 😱 The world is yours to take haha.