Re: verify legal sentences
You are going to build a parser. It is a lot of work; as an example, here is a piece of it, provided as a staring point.
Warning: I build this in a hurry, it is not completed, it performs no major checking, it can be improved with subs and functions, and so on...
private Sub Command1_Click()
Dim strTempString 'to perform operation on text1.text without touching original string
Dim tmpStr 'a very local piece of string
Dim retArr
Dim intFoundPos as Integer
Dim startPos as Integer
Dim intCountOpenBracket as Integer
Dim intCountCloseBracket as Integer
Dim openBPos() as Integer
Dim closeBPos() as Integer
Dim blnFirst as Boolean
Dim intX as Integer, intY as Integer
strTempString = Text1.Text
'first, check for bracket
startPos = 1
blnFirst = true
Do
intFoundPos = InStr(startPos, strTempString, "(", vbTextCompare)
If intFoundPos <> 0 then
startPos = intFoundPos + 1
intCountOpenBracket = intCountOpenBracket + 1
'remember position of bracket
If blnFirst = true then
ReDim openBPos(0)
blnFirst = false
else
ReDim Preserve openBPos(UBound(openBPos) + 1)
End If
openBPos(UBound(openBPos)) = intFoundPos
End If
Loop While intFoundPos <> 0
startPos = 1
blnFirst = true
Do
intFoundPos = InStr(startPos, strTempString, ")", vbTextCompare)
If intFoundPos <> 0 then
startPos = intFoundPos + 1
intCountCloseBracket = intCountCloseBracket + 1
'remember position of bracket
If blnFirst = true then
ReDim closeBPos(0)
blnFirst = false
else
ReDim Preserve closeBPos(UBound(closeBPos) + 1)
End If
closeBPos(UBound(closeBPos)) = intFoundPos
End If
Loop While intFoundPos <> 0
Select Case (intCountCloseBracket - intCountOpenBracket)
Case is > 0
MsgBox intCountCloseBracket - intCountOpenBracket & " opened bracket missed!"
Exit Sub
Case is < 0
MsgBox intCountOpenBracket - intCountCloseBracket & " closed bracket missed!"
Exit Sub
Case else
'ok, go on
End Select
'second, check inside brackets
'to find portion inside: start from nearest closedB (from right to left)
'search in openB for a value lesser starting from the left
If blnFirst = false then '(you got at least one bracket)
ReDim retArr(UBound(closeBPos))
for intX = 0 to UBound(closeBPos)
for intY = UBound(openBPos) to 0 step -1
If openBPos(intY) < closeBPos(intX) then
'get text inside brackets; work on original string as we are going change the copy
tmpStr = mid(strTempString, openBPos(intY) + 1, (closeBPos(intX) - 1) - (openBPos(intY) + 1))
'verify inside brackets rules are granted
retArr = Split(tmpStr, " ")
'what the rules?
'---------------------
'make array openbracket not good for next trial
openBPos(intY) = len(Text1.Text) + 1
blnFirst = true
Exit for
End If
next intY
If blnFirst = false then 'error: misplaced bracket
MsgBox "Misplaced bracket!"
Exit Sub
End If
next intX
End If
'retArr = Split(strTempString.Text, " ")
End Sub
private Sub Form_Load()
Text1.Text = "((P ^ Q) => R) = -|(P ^ 0) v R"
End Sub
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.