Parsing string -> value

Dec 25, 2012 at 8:27am
Hello everybody... I'm now making a group of code which can allow the parser to read the value inside the bracket [...]
I have no idea, but to make it properly, I thought up several important steps :
0- Check the main variable, if it is not exist then break this step.
1- Locate the position of the opening bracket symbol ("["). If it exists then :
2- Copy all characters inside the bracket until the block character ("]"). If the end bracket sign is never met, then break its parsing here.
3- Try to convert the string to value number.
4- If the value number is valid, then check the target variable's index.
5- Copy the value from target main variable's index, clean all temporary data, prepare for next element...


For example :
number[50]; //number[50] = 50;

And, here is my solution :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//i : Expression string index (str[i])
//nCharCount : string length

int nStart = i - nCharCount;
int nEnd = i;
//////Get variable...
///,,,,,,,,,,,,,,,,,,,,,,
//////////////////////////

//Find the opening bracket symbol ("[")
int nPosStart = Find(str, '[', nStart, nEnd);//Range : nStart - nEnd
if(nPosStart == nStart) {throw ERROR_7; goto Clean;} // The length of the main variable is zero
if(nPosStart == nEnd) {Parse(str, nStart, nEnd;} // The bracket expression really is not exist, use the regular parsing function instead

//Find the end bracket symbol ("]")
int nPosEnd = Find(str, ']', nPosStart + 1, nEnd);//Range : nPosStart + 1 - nEnd
if(nPosEnd == nPosStart + 1) {throw ERROR_8; goto Clean;} // The length of the bracket expression is zero
if(nPosEnd == nEnd) {throw ERROR_9; goto Clean;} // Out of range

//Accept bracket expression...
nStart = nPosStart + 1; //Set expression range
nEnd = nPosEnd;

//Get bracket expression value...
int valueIndex = Parse(bracketexpression, nStart, nEnd);//bracketexpression : str
if(IsValid(valueIndex))
SetValue(...)
//Clean,... 


And my output (It did successfully) :
number[50] : 50


Then next step I tried parsing the expression :
number[10 + number[10] + 10]
Certainly the result is 30, but unfortunately, my solution doesn't work :(
I have no idea why and how it skips "number[10] + 10" then generates the final value 10.


Thanks for any help. :)
Last edited on Dec 25, 2012 at 9:48am
Dec 25, 2012 at 1:24pm
OMG...I detected that the algorithm stops immediately for no reason when it meets any end bracket sign symbol. Thus, I'm pretty sure "number[10] + 10" especially after the first bracket "]" the string "+ 10]" is never reached then parsed. :(
Then actually the expression I've just caught is :
"10 + number[10"

The element "number[10" is missing one bracket so the parser cannot accept this number. So the result is 10 not 30 :(
Therefore, I have to make a loader which can handle multiple "bracket symbols-expressions"...

Any idea? How to fix this problem guys?
(Any help would be greatly appreciated)
Last edited on Dec 25, 2012 at 11:39pm
Dec 25, 2012 at 3:20pm
I'd use regular expressions for this kind of tasks.
Dec 25, 2012 at 10:45pm
But, how? Anybody?
Dec 26, 2012 at 11:20am
Wow my idea... Here is my solution :
The find function now is modified. (string, chr, length, startpos) (ERROR : -1)

1
2
3
4
5
6
7
8
9
10
11
12
13
int maxpos = i;
vector <int> recordstart; // Records all character positions that follow (string[position] == '[')
vector <int> recordend; // Records all character positions that follow (string[position] == ']')
int pos = maxpos - nChrCount; //Current position
while((pos = find(str, '[', maxpos, pos)) != -1){
recordstart.push_back(pos); // "["
pos = pos + 1; // Avoid infinite loop
}
pos = maxpos - nChrCount;
while((pos = Find(str, ']', maxpos, pos)) != -1){
recordend.push_back(pos); // "]"
pos++;
}

Now if we have a small-length string e.g "[[[[[]]]]]" - 10 characters which first five characters are "[" and five last characters are "]". And here is my result :
[ ]
0 9
1 8
2 7
3 6
4 5

Is this a good idea?
And any better solution guys?
Last edited on Dec 26, 2012 at 11:55am
Topic archived. No new replies allowed.