Parsing an integer and HEX value in Ragel -


i trying design parser using ragel , c++ host langauge. there particular case parameter can defined in 2 formats :

a. integer : eg. signalvalue = 24 b. hexadecimal : eg. signalvalue = 0x18 

i have below code parse such parameter :

int = ((digit+)$incr_count) %get_int >!(int_error); #[0-9] hex = (([0].'x'.[0-9a-f]+)$incr_count) %get_hex >!(hex_error); #[hexadecimal]  signal_val = ( int | hex ) %/getsignalvalue; 

however in above defined parser command, integer values(as defined in section a) gets recognized , parsed correctly. if hexadecimal number(eg. 0x24) provided, number gets stored ´0´ . there no error called in case of hexadecimal number. parser recognizes hexadecimal, value stored '0'.

i seem missing out minor details ragel. has faced similar situation?

the remaning part of code :

 //global  int lint = -1;   action incr_count {    igenrlcount++; }  action get_int {   int channel = 0xff;   std::stringstream str;     while(igenrlcount > 0)   {     str << *(p - igenrlcount);     igenrlcount--;   }   str >> lint; //push values   str.clear(); }   action get_hex {  std::stringstream str;   while(igenrlcount > 0)  {   str << std::hex << *(p - igenrlcount);   igenrlcount--;  }  str >> lint; //push values }  action getsignalvalue {   cout << "lint = " << lint << endl; } 

it's not problem fsm (which looks fine task have), it's more of c++ coding issue. try implementation of get_hex():

action get_hex {  std::stringstream str;  cout << "get_hex()" << endl;  while(igenrlcount > 0)  {   str << *(p - igenrlcount);   igenrlcount--;  }  str >> std::hex >> lint; //push values } 

notice uses str string buffer , applies std::hex >> std::stringstream int. in end get:

$ ./a.out 245 lint = 245 $ ./a.out 0x245 lint = 581 

which want.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -