Click to See Complete Forum and Search --> : verify legal sentences


TAN
July 17th, 2001, 04:19 AM
I have to know how to determine whether an expression is a legal sentence.

Obtain an expression from the users; validate the expression by checking each constituent according to the sequence of Propositionai Rules. Display the constituents if the expression is a legal sentence. Otherwise, explain the reasons the expression is not a legal sentence.

A few examples are illustrated below: (in my program, : -| as'not', v 'or, 'and'; => as'=>'.)

Example 1
Expression : ((P ^ Q) => R) = -|(P ^ 0) v R
Answer: Yes. Legal Sentence.
Trace: P, Q, R are Propositional Symbols => Sentence
P /\ Q, is the AND (disjunction) of 2 sentences => Sentence
(P ^ Q) => R, is the Implication of 1 sentence for another sentence => Sentence
(P Q), is the Negation of 1 sentence => Sentence
(P Q) v R, is the OR (conjunction) of 2 sentences => Sentence ((P ^ Q) => R) = -| (P ^ Q) v R, is the Equivalence of 2 sentences => Sentence Legal Sentence.

Example 2
Expression : Upper ^ lower
Answer: Invalid expression
Reason : lower is invalid.Propositional symbol.

Example 3
Expression: -| \Q
Answer: Invalid expression
Reason Q is invalid Negation of a sentence => NOT Sentence

Example 4
Expression: /\ Q
Answer: Invalid expression
Reason Q is invalid AND (disjunction) of 1 sentence => NOT Sentence

Example 5
Expression: => R
Answer: Invalid expression
Reason R, is the invalid Implication for another sentence => NOT Sentence


Example 6
Expression : P ^ => Q
Answer: Invalid expression
Reason : P ^ => Q, is invalid AND (disjunction) of 1 sentence => NOT Sentence

Cimperiali
July 18th, 2001, 11:16 AM
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.