Why and how to write your own compiler?
Why and how to write your own compiler?
Nikola Svitlica (thecelavi)
Writing a compiler is almost always perceived as a task not intended for high-level languages such as PHP. If you believe so, allow me to convince you otherwise. Through real-world examples, knowledge of how compilers work, and their benefits, will be demonstrated. Through a brief demonstration, a simple compiler written solely in PHP will be presented which will be used to power a custom, fault-tolerant query language that will be used to generate storage-agnostic search queries. After the session, you will stop using regular expressions and string operations to sanitise the user's search query simply because regular expressions are not the right tool for the job - your custom compiler is.
Prerequisite
Knowledge in PHP. Understanding of DBMS and full text search would be beneficial, but not necessary.
Outline
Session is presentation of the real life problem which we have stumble upon - creating user friendly, error tolerant, query language for site global search.
After months of attempts to solve problem sanitising user input using regular expressions (all failed), rapid growth of edge cases forced us to try another approach.
We built a fault tolerant compiler in PHP, which does lexing, parsing and compiling user search query into a valid SQL query, regardless if user had syntax errors in its input or not.
In example, we allowed users to use jokers in search, signs like: +, -, "", * and so on.
However, users are prone to syntax errors, example: foo** (we know that user meant foo*)
Finding and clearing syntax errors with regex is just impossible.
With custom compiler (lexer+parser+compiler) - it is easy to spot a syntax error and to correct it - without occurrence of any edge case.
This custom compiler is in production for several years now - no reported bugs since then. Our search works flawlessly.
Learning Objectives
- To briefly introduce theory of compilers and it's key components (lexer, parser, compiler as well as AST).
- Through example, demonstrate how easy is to actually a write one.
- To demonstrate that this knowledge is useful for every developer as it's required more often than one perceive through real-life example
- To provide pointers and references for further study of this topic
Experience level
Intermediate