문제

I do not know How ragel read the source from the file. All the examples I have seen read from stdin.

Please can you show me an example in C of interfacing with Ragel where the program does not read from standard in?

도움이 되었습니까?

해결책

Ragel expects a few variables to be in scope and point to various important parts of the buffer it is scanning. You can find out what it expects them to be called and how you can change them in the Ragel User Guide. Section 5, interface to host program, is your friend here.

An example from the manual that runs the scanner over the first argument:

#include <stdio.h>
#include <string.h>

%%{
    machine foo;
    write data;
}%%

int main( int argc, char **argv )
{
    int cs;
    if ( argc > 1 ) {
        char *p = argv[1];
        char *pe = p + strlen( p );
        %%{
            main := [0-9]+ ( '.' [0-9]+ )?;
            write init;
            write exec;
        }%%
    }
    printf("result = %i\n", cs >= foo_first_final );
    return 0;
}

Basically cs holds the state of the machine and p and pe point to the beginning and end of the buffer respectively.

The definitions from the manual:

• cs - Current state. This must be an integer and it should persist across invocations of the machine when the data is broken into blocks that are processed independently. This variable may be modied from outside the execution loop, but not from within.

• p - Data pointer. In C/D code this variable is expected to be a pointer to the character data to process. It should be initialized to the beginning of the data block on every run of the machine. In Go, Java and Ruby it is used as an oset to data and must be an integer. In this case it should be initialized to zero on every run of the machine.

• pe - Data end pointer. This should be initialized to p plus the data length on every run of the machine. In Go, Java and Ruby code this should be initialized to the data length.

• eof - End of file pointer. This should be set to pe when the buer block being processed is the last one, otherwise it should be set to null. In Go, Java and Ruby code -1 must be used instead of null. If the EOF event can be known only after the nal buer block has been processed, then it is possible to set p = pe = eof and run the execute block.

• data - This variable is only required in Go, Java and Ruby code. It must be an array containting the data to process.

• stack - This must be an array of integers. It is used to store integer values representing states. If the stack must resize dynamically the Pre-push and Post-Pop statements can be used to do this (Sections 5.6 and 5.7).

• top - This must be an integer value and will be used as an oset to stack, giving the next available spot on the top of the stack.

• act - This must be an integer value. It is a variable sometimes used by scanner code to keep track of the most recent successful pattern match.

• ts - This must be a pointer to character data. In Go, Java and Ruby code this must be an integer. See Section 6.3 for more information.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top