5
I’m creating a text interpreter based on the Github do Duckduckgo in PHP.
This is one of the codes I created:
if (strpos(strtolower($qt), "rand") !== FALSE){
if (preg_match("/^rand$/", strtolower(removeAccents($qt)), $match)){
$result = rand(1,9999);
$sndline = "Random number";
}
elseif (preg_match("/^random$/", strtolower(removeAccents($qt)), $match)){
$result = rand(1,9999);
$sndline = "Random number";
}
elseif (preg_match("/^rand *\((?<min>[0-9]+),(?<max>[0-9]+)\)$/", strtolower(removeAccents($qt)), $match)){
$result = rand($match['min'],$match['max']);
$sndline = "Random number";
}
elseif (preg_match("/^random *\((?<min>[0-9]+),(?<max>[0-9]+)\)$/", strtolower(removeAccents($qt)), $match)){
$result = rand($match['min'],$match['max']);
$sndline = "Random number";
}
}
As you can see, the script performs the action if the user types rand
, random
, rand (1,99)
and random (1,99)
.
The whole interpreter was written with functions preg_match
(I mean, everything with Regex) but found that they overload the system.
How can I create something that is fast (without overloading the system) and also understands different "orders" given by the user without using Regex (remembering that the user can type anything in the search)?
Let’s assume that I have more than one of these "modules", I will have to add more conditions (elseif)?
– hsbpedro
It’s a path. But depending on the complexity of your rules, it’s worth studying a little bit about lexers and parsers and implement something more generic.
– bfavaretto
Complexity you say in what sense?
– hsbpedro
For example, if you have many of these keywords to be identified in the input data, if they can appear in different positions, if they can be grouped into expressions, if the order makes a difference, among other things. The more its rules approach a "language", the more appropriate it is to represent it in BNF and create a parser for her.
– bfavaretto
Without going in the direction of lexers and parsers, has another way to go adding new modules without having to create new elseifs?
– hsbpedro
No, it’s on the basis of
if
,elseif
andelse
same. In certain cases of string equality comparison with a list of terms you can opt for aswitch
and react accordingly, but it doesn’t make much difference.– bfavaretto
If I add too many
elseif
, the code will still be fast?– hsbpedro
It depends on what you do in it, there is no absolute answer.
– bfavaretto
A general path (which lexers consist of taking) is to take the entire string, break into a list of "words" (tokens) and scan this list by comparing the terms with what you accept, interpreting linearly the entire "phrase".
– bfavaretto
Unfortunately, I don’t have the competence or time to go into detail right now. But who knows any colleague here on the site does not post a response to that later.
– bfavaretto
Let’s go continue this discussion in chat.
– hsbpedro
@hsbpedro Sorry, but now we can not talk in chat, I’m ending the day. But keep trying small changes and testing the difference in performance.
– bfavaretto
Thank you very much for the clarification and the help!
– hsbpedro