What does this Ragel Code fragment do?

    %%{
  machine microscript;

  action ClearNumber {
    currentNumber = 0;
  }

  action RecordDigit {
    uint8_t digit = (*p) - '0';
    currentNumber = (currentNumber * 10) + digit;
  }

  number = ((digit @RecordDigit)+) >ClearNumber;
  whitespace = space+;

  main := number (whitespace number)*;
}%% 

EDIT: Make me understand the meaning of this ">" operator. I quoted his description from the ragel manual in a comment on @jcomeu

I understand that the ClearNumber action is called before RecordDigit, if so, currentNumber is initialized to zero, i.e. using multiplication by 10.

And finally, the definition of a number. What does it mean number=((digit @RecordDigit)+) >ClearNumber?

This is the source of the code: here

EDIT : * In particular, how does RecordDigit work? What is p? Pointer? if so, what does this indicate? What does it mean digit =(*p)- '0';? [SOLVED]

+3
source share
5 answers

Pointer p

p in RecordDigit - , Ragel. . ( Java Ruby , data). , (*p) - '0' (, '7' s 55 ASCII), '0' (48 ASCII), , : 55 - 48 = 7.

> @

> . @ .

, :

((digit @RecordDigit)+) >ClearNumber

ClearNumber RecordDigit , digit . , :

  action ClearNumber {
    printf("ClearNumber\n");
    currentNumber = 0;
  }

  action RecordDigit {
    printf("RecordDigit\n");
    uint8_t digit = (*p) - '0';
    currentNumber = (currentNumber * 10) + digit;
  }

:

ClearNumber
RecordDigit
RecordDigit
RecordDigit

3- .

currentNumber 0. uint8_t (*p) - '0' currentNumber. , currentNumber, 10 .. , Ragel , , , .

Ragel . Ragel .

+3

ragel, RecordDigit C, . , p - ; * p (). "0" "9" 9. , 10 , , , "321" 321, 10 RecordDigit, .

"".

+1

ragel. , , , . ASCII 0- 9 - 48 57. , '123', 48, , 1. 10 123.

+1
number = ((digit @RecordDigit)+) >ClearNumber;

'digit' - : [0-9]

It collects the numbers one by one (using the "+" operator, which means "1..N") and introduces the ClearNumber action at the beginning of the new number.

The @RecordDigit action is used to calculate the number during parsing.

Sorry for my English, not my native language. Hope this helps.

+1
source

To really understand ragel, you have to generate a chart. Install graphviz and run ragel as follows:

ragel -V -p microscript.rl | dot -Tpng -o microscript.png

I usually just use a makefile

%.png: %.rl
        ragel -V -p $*.rl | dot -Tpng -o $@

So, I could just run make microscript.png

enter image description here

+1
source

Source: https://habr.com/ru/post/1792411/


All Articles