Parsing Tokens
Posted: Sat Jul 16, 2005 8:14 pm
I am currently in the midst of writing a programming language in PureBasic it's self, and I'm having a little bit of trouble parsing commands which have nested commands within them.
I'm trying to convert:
Command1( Command2(1,2,3 )+1 , Aurg )
Into:
1 2 3 Command2() +1 ¬ Aurg Command1() ¬
Basically I'm trying to put the most nested command first, so I can work out its result before the others. The format is:
<Parameters> Command() Maths BREAK
Does anyone have any idead of how to do this successfully, it'd really help me a lot.
Here is my best working example:
I'm trying to convert:
Command1( Command2(1,2,3 )+1 , Aurg )
Into:
1 2 3 Command2() +1 ¬ Aurg Command1() ¬
Basically I'm trying to put the most nested command first, so I can work out its result before the others. The format is:
<Parameters> Command() Maths BREAK
Does anyone have any idead of how to do this successfully, it'd really help me a lot.
Here is my best working example:
Code: Select all
Procedure.s ParseToken(Recursive,Token.s)
Stack.s
Scanned.s
Temp.s
If Recursive=0 And CountString(Token,"(")>1
Stack+Mid(Token,1,FindString(Token,"(",1)-1)+"()"+Chr(190)+" "
Token=Mid(Token,FindString(Token,"(",1)+1,Len(Token))
EndIf
Debug Token
While Token
Scanned=Left(Token,1)
Token=Mid(Token,2,Len(Token))
If Scanned="("
If Temp
Stack=Temp+"()"+Chr(190)+" "+Stack
EndIf
If Recursive=0
Temp=ParseToken(1,Mid(Token,1,FindString(Token,")",1)))
Stack=Temp+Stack
ElseIf Recursive=1
Temp=ParseToken(1,Mid(Token,1,FindString(Token,")",1)))
Stack=Temp+Stack
;ProcedureReturn Stack
EndIf
Temp=""
Token=Mid(Token,FindString(Token,")",1)+1,Len(Token))
ElseIf Scanned=","
If Recursive
If Temp
Stack=Temp+" "+Stack
Temp=""
EndIf
Else
If Temp
Stack+Temp+" "
Temp=""
EndIf
EndIf
ElseIf Scanned=")"
If Token
Debug 1
EndIf
If Recursive
If Temp
Stack=Temp+" "+Stack
EndIf
Else
If Temp
Stack+Temp+" "
EndIf
EndIf
;ProcedureReturn Stack
ElseIf Scanned<>"()"
Temp+Scanned
EndIf
Wend
ProcedureReturn Stack
EndProcedure