Split string into logical pieces (AND, OR, parentheses)?

Just starting out? Need help? Post your questions and find answers here.
camille
User
User
Posts: 66
Joined: Tue Nov 19, 2019 12:52 pm

Split string into logical pieces (AND, OR, parentheses)?

Post by camille »

Hello,

has anybody ever written a procedure that is able to receive a string and split it by logical precedence?

E.g.

Code: Select all

string.s = "(k1 != 2 OR ((m1 = 5 AND x1 = 7) OR extension = gif))"

k1, m1, x1 and extension are real variables. 
They need to be resolved into their real value after splitting.
AND has priority over OR and parentheses must be resolved (if present).

This would result in 3 necessary comparisons (in reality more are necessary, like m1 = 5, x1 = 7, etc.)
1. m1 = 5 AND x1 = 7
2. <boolean #True/#False result from 1.> OR extension = gif
3. k1 != 2 OR <boolean #True/#False result from 2.>

Every comparison leads to a #True / #False state (after the belonging variables have been resolved)
The comparison chain would exit once a single #False is one of the results.

E.g.
m1 -> Resolved to 5: 5 = 5 -> #True
x1 -> Resolved to 6: 6 = 7 -> #False
At this point the logical chain can be ended with a break.
If x1 would have been 7, the next comparison would have been:
#True OR extension = gif
Etc.

Any idea how to accomplish something like this with a generic approach?

Merci beaucoup!
User avatar
Tenaja
Addict
Addict
Posts: 1949
Joined: Tue Nov 09, 2010 10:15 pm

Re: Split string into logical pieces (AND, OR, parentheses)?

Post by Tenaja »

What you are describing is a parser. There are a few on the forum. There really isn't a generic parser, because every need is different.
User avatar
Kiffi
Addict
Addict
Posts: 1358
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Split string into logical pieces (AND, OR, parentheses)?

Post by Kiffi »

Code: Select all

EnableExplicit

Define k1 = 2
Define m1 = 6
Define x1 = 5
Define extension.s = "gif"

Define string.s = "(k1 != 2 OR ((m1 = 5 AND x1 = 7) OR extension = 'gif'))"

string = ReplaceString(string, "k1", Str(k1))
string = ReplaceString(string, "m1", Str(m1))
string = ReplaceString(string, "x1", Str(x1))
string = ReplaceString(string, "extension", Chr(34) + extension + Chr(34))

Debug String

UseSQLiteDatabase()
OpenDatabase(0, ":memory:", "", "", #PB_Database_SQLite)
DatabaseQuery(0, "Select " + string)
Debug DatabaseError()
NextDatabaseRow(0)
Debug GetDatabaseLong(0, 0)
:mrgreen:
Hygge
camille
User
User
Posts: 66
Joined: Tue Nov 19, 2019 12:52 pm

Re: Split string into logical pieces (AND, OR, parentheses)?

Post by camille »

@Tenaja
Thanks for the hint!

@Kiffi
Thanks! This is the coolest (and shortest) boolean logic parser example ever^^ Great stuff!
mestnyi
Addict
Addict
Posts: 1000
Joined: Mon Nov 25, 2013 6:41 am

Re: Split string into logical pieces (AND, OR, parentheses)?

Post by mestnyi »

@Kiffi
Thanks! This is the coolest (and shortest) boolean logic parser example ever^^ Great stuff!
I agree too :)
but how does it work? :shock:

Code: Select all

UseSQLiteDatabase()
OpenDatabase(0, ":memory:", "", "", #PB_Database_SQLite)
DatabaseQuery(0, "Select " + string)
Debug DatabaseError()
NextDatabaseRow(0)
Debug GetDatabaseLong(0, 0)
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Split string into logical pieces (AND, OR, parentheses)?

Post by TI-994A »

mestnyi wrote:...but how does it work?
SQLite allows the use of the SELECT statement to evaluate expressions without any other database conditions such as FROM or WHERE. But not all databases conform to this.

Code: Select all

UseSQLiteDatabase()
If OpenDatabase(0, ":memory:", "", "")
  
  DatabaseQuery(0, "Select 1 + 1")
  NextDatabaseRow(0)
  Debug GetDatabaseLong(0, 0) ; = 2
  
  DatabaseQuery(0, "Select 5 - 2") 
  NextDatabaseRow(0)
  Debug GetDatabaseLong(0, 0) ; = 3
  
  DatabaseQuery(0, "Select 9 * 3") 
  NextDatabaseRow(0)
  Debug GetDatabaseLong(0, 0) ; = 27
  
  DatabaseQuery(0, "Select 100 / 4") 
  NextDatabaseRow(0)
  Debug GetDatabaseLong(0, 0) ; = 25
  
  DatabaseQuery(0, "Select 'apples' = 'apples'") 
  NextDatabaseRow(0)
  Debug GetDatabaseLong(0, 0) ; = true = 1
  
  DatabaseQuery(0, "Select 'apples' = 'oranges'") 
  NextDatabaseRow(0)
  Debug GetDatabaseLong(0, 0) ; = false = 0
  
  CloseDatabase(0)
  
EndIf
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Split string into logical pieces (AND, OR, parentheses)?

Post by Rinzwind »

You leave the actual parsing job to sqlite; bit cheating huh ;)
Denis
Enthusiast
Enthusiast
Posts: 704
Joined: Fri Apr 25, 2003 5:10 pm
Location: Doubs - France

Re: Split string into logical pieces (AND, OR, parentheses)?

Post by Denis »

Hi camille

not a solution but if you want to learn more about parser (analyseur lexicographique/syntaxique etc) :

vidéos (in french) :
Theory of Languages (THL) by Akim Demaille (6 videos of about 4 hours each, these are mathematics for grammars)
THL 1 to THL6, really excellent courses!
Théorie des langages (THL) par Akim Demaille (6 vidéos d'environ 4 heures, ce sont des mathématiques pour les grammaires)
THL 1 à THL6, vraiment d'excellents cours!


THL1 : https://www.youtube.com/watch?v=WbUpN4fHs_k
(others follow on youtube)/les autres suivent sur youtube)

French books (some hard to find/certains difficiles à trouver):
1) Eléments de théorie des Automates par Jacques Sakarovitch
2) Compilation : analyse lexicale et syntaxique, du texte à sa structure en informatique par Romain Legendre & François Schwarzentruber
3) La programmation par syntaxe, des grammaires à la compilation par B. Groc et M. Bouhier
4) Théorie des langages et compilation, brefs résumés de cours et exercices corrigés par Ali Aït el Hadj
5) Introduction à la théorie des langages de programmation par Gilles Dowek & JJ Lévy
6) Langages formels : calculabilité et complexité par Olivier carton
7) Compilateurs : Principes, techniques et outils (2e édition) par de Alfred Aho, Monica Lam & Ravi Sethi

i do prefer n°3 et 1 (soft spot for N°3, which I've had for 30 years.../un faible surtout pour N°3 que j'ai depuis 30 ans...)
A+
Denis
camille
User
User
Posts: 66
Joined: Tue Nov 19, 2019 12:52 pm

Re: Split string into logical pieces (AND, OR, parentheses)?

Post by camille »

Hi Denis,

thanks for all the information!

I have to admit, they are a bit over my head currently :D
A lot of theory...

Camille
Post Reply