|
-
Jan 24th, 2013, 08:48 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Array out of range.
Hi I am trying to make some code to collect tokens of a string. but I keep getting an out of range error, tho it works with some expressions
here my code.
Code:
Public Class Form1
Private Enum Tokypes
None = 0
Digit = 1
Sysmbol = 2
End Enum
'Private Expression As String = "8 * 2 + (10+5)" 'Works fine on this expression.
Private Expression As String = "8 * 2 + 1" ' VB Crys on this one why
Private Pos As Integer = 0
Dim sToken As String = vbNullString
Dim sTokType As Tokypes
Private Function isDelim(ByVal c As Char) As Boolean
If (("+-/*%^=()".IndexOf(c) <> -1)) Then
Return True
End If
Return False
End Function
Private Sub GetTokens()
sToken = vbNullString
sTokType = Tokypes.None
If (Pos = Expression.Length) Then
sToken = vbNullString
sTokType = Tokypes.None
Return
End If
'Skip white
While (Pos < Expression.Length) And Char.IsWhiteSpace(Expression(Pos))
Pos += 1
End While
'
If isDelim(Expression(Pos)) Then
sToken = sToken & Expression(Pos)
Pos += 1
'Token Type.
sTokType = Tokypes.Sysmbol
ElseIf Char.IsDigit(Expression(Pos)) Then
'Thowing error here - Index was outside the bounds of the array.
While Char.IsDigit((Expression(Pos)))
sToken = sToken & Expression(Pos)
Pos += 1
End While
If Pos = Expression.Length Then Return
'Token Type.
sTokType = Tokypes.Digit
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
GetTokens()
While sTokType <> Tokypes.None
MsgBox(sToken)
GetTokens()
End While
End Sub
End Class
If you run the code with this expression eveything is fine "8 * 2 + (10+5)"
But if you run it with this you get an out of range error. 8 * 2 + 1
I don't see why cos the first expression has more length than the second.
-
Jan 24th, 2013, 09:04 PM
#2
Re: Array out of range.
your problem occurs in the code below when you enter the loop and Pos = 8. When Pos is 8,Char.IsDigit is true because Expression(8)="1". In the loop you then increment Pos, loop and look at Expression(9) which doesn't exist because the upper index of the string array is 8.
You need to change your code to test Pos and jump out of the While or rearrange your logic.
Code:
While Char.IsDigit((Expression(Pos)))
sToken = sToken & Expression(Pos)
Pos += 1
End While
kevin
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Jan 25th, 2013, 03:24 AM
#3
Thread Starter
Fanatic Member
Re: Array out of range.
Thanks
Think I fixed it I appened an extra white space to the end of the expression and it seems to work. I left the code here in case someone else may need it, updated for Alpha.
Code:
Public Class Form1
Private Enum Tokypes
None = 0
Digit = 1
Alpha = 2
Sysmbol = 3
End Enum
Private Expression As String = "PI+8 * 2 + 500 *9 * SIN(3)"
Private Pos As Integer = 0
Dim sToken As String = vbNullString
Dim sTokType As Tokypes
Private Function isDelim(ByVal c As Char) As Boolean
If ((" +-/*%^=()".IndexOf(c) <> -1)) Then
Return True
End If
Return False
End Function
Private Sub GetTokens()
sToken = vbNullString
sTokType = Tokypes.None
While Pos < Expression.Length AndAlso Char.IsWhiteSpace(Expression(Pos))
Pos += 1
End While
If (Pos = Expression.Length) Then
sToken = vbNullString
sTokType = Tokypes.None
Return
End If
If isDelim(Expression(Pos)) Then
sToken = Expression(Pos)
Pos += 1
'Token Type.
sTokType = Tokypes.Sysmbol
ElseIf Char.IsLetter(Expression(Pos)) Then
While Not isDelim(Expression(Pos))
sToken = sToken & Expression(Pos)
Pos += 1
If (Pos >= Expression.Length) Then
Return
End If
End While
'Token Type.
sTokType = Tokypes.Alpha
ElseIf Char.IsDigit(Expression(Pos)) Then
While Not isDelim(Expression(Pos))
sToken = sToken & Expression(Pos)
Pos += 1
If (Pos >= Expression.Length) Then
Return
End If
End While
'Token Type.
sTokType = Tokypes.Digit
Else
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Pos = 0
GetTokens()
While sTokType <> Tokypes.None
MsgBox(sToken)
GetTokens()
End While
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Expression = Expression & " "
End Sub
End Class
-
Jan 25th, 2013, 09:16 AM
#4
Re: [RESOLVED] Array out of range.
What is this code supposed to accomplish?
-
Jan 25th, 2013, 08:04 PM
#5
Thread Starter
Fanatic Member
Re: [RESOLVED] Array out of range.
 Originally Posted by dbasnett
What is this code supposed to accomplish?
tokenizer for a math evaluator I am going to make.
-
Jan 26th, 2013, 10:21 AM
#6
Re: [RESOLVED] Array out of range.
I thought so. This may be useful Parsing Infix
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|