It is currently Fri Feb 21, 2020 12:15 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Split string into logical pieces (AND, OR, parentheses)?
PostPosted: Fri Feb 14, 2020 3:14 pm 
Offline
User
User

Joined: Tue Nov 19, 2019 12:52 pm
Posts: 35
Hello,

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

E.g.
Code:
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: Split string into logical pieces (AND, OR, parentheses)?
PostPosted: Fri Feb 14, 2020 4:27 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Nov 09, 2010 10:15 pm
Posts: 1602
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Split string into logical pieces (AND, OR, parentheses)?
PostPosted: Fri Feb 14, 2020 4:52 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Mar 02, 2004 1:20 pm
Posts: 1052
Location: Amphibios 9
Code:
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:

_________________
Can't decide if i need a hug, an XXL coffee, 6 shots of vodka or 2 weeks of sleep.


Top
 Profile  
Reply with quote  
 Post subject: Re: Split string into logical pieces (AND, OR, parentheses)?
PostPosted: Fri Feb 14, 2020 5:01 pm 
Offline
User
User

Joined: Tue Nov 19, 2019 12:52 pm
Posts: 35
@Tenaja
Thanks for the hint!

@Kiffi
Thanks! This is the coolest (and shortest) boolean logic parser example ever^^ Great stuff!


Top
 Profile  
Reply with quote  
 Post subject: Re: Split string into logical pieces (AND, OR, parentheses)?
PostPosted: Sat Feb 15, 2020 4:43 am 
Offline
Addict
Addict

Joined: Mon Nov 25, 2013 6:41 am
Posts: 805
Quote:
@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:
UseSQLiteDatabase()
OpenDatabase(0, ":memory:", "", "", #PB_Database_SQLite)
DatabaseQuery(0, "Select " + string)
Debug DatabaseError()
NextDatabaseRow(0)
Debug GetDatabaseLong(0, 0)


Top
 Profile  
Reply with quote  
 Post subject: Re: Split string into logical pieces (AND, OR, parentheses)?
PostPosted: Sat Feb 15, 2020 6:44 am 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 19, 2011 3:47 am
Posts: 2250
Location: Singapore
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:
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: Split string into logical pieces (AND, OR, parentheses)?
PostPosted: Sat Feb 15, 2020 10:49 am 
Offline
Enthusiast
Enthusiast

Joined: Wed Mar 11, 2009 4:06 pm
Posts: 224
Location: NL
You leave the actual parsing job to sqlite; bit cheating huh ;)


Top
 Profile  
Reply with quote  
 Post subject: Re: Split string into logical pieces (AND, OR, parentheses)?
PostPosted: Sat Feb 15, 2020 4:29 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Apr 25, 2003 5:10 pm
Posts: 523
Location: Doubs - France
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Split string into logical pieces (AND, OR, parentheses)?
PostPosted: Tue Feb 18, 2020 12:16 am 
Offline
User
User

Joined: Tue Nov 19, 2019 12:52 pm
Posts: 35
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye