[ragel-users] Re: integrating ragel w/ bison

Adrian Thurston thurs... at cs.queensu.ca
Fri Jan 5 03:41:16 UTC 2007


Hi Jason,

I finished off the rest of the pull driver and added it to the examples 
directory. Be sure to let me know if you find any bugs. I did it fast.

Cheers,
  Adrian

Jason wrote:
> 
> Adrian,
> 
> I'm still not following your full intent but I did manage to get some
> code (below) that builds and runs but produces no output.
> 
> Hopefully, you can help me fill in the blanks.
> 
> thanks,
> Jason
> 
> ----------------------------------- CUT HERE
> -------------------------------------------------
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> 
> typedef struct _Scanner {
>     int cs;
>     int act;
>     int have;
>     int curline;
>     char *tokstart;
>     char *tokend;
>     char *p;
>     char *pe;
>     int done;
> 
>     int value;
> } Scanner;
> 
> #define ERR (-1000)
> 
> void scan_init( Scanner *s )
> {
>  memset (s, '\0', sizeof(Scanner));
>  s->curline = 1;
> }
> 
> int fetch_more_data ( Scanner *s ) {
>     return 0;
> }
> 
> void check_for_preserve_condition( Scanner *s ) {
> }
> 
> int Scanner_error = -1;
> 
> #define Identifier 1
> #define Number 2
> 
> 
> int scan( Scanner *s )
> {
>        char *p = s->p;
>        char *pe = s->pe;
>     int token, no_token = '\0';
> 
>        while ( 1 ) {
>                if ( p == pe ) {
>                        check_for_preserve_condition(s);
>                        if ( ! fetch_more_data(s) )
>                                return EOF;
>                }
> 
>                token = no_token;
> 
>                %%{
>                        machine Scanner;
>                        access s->;
> 
>                        main := |*
> 
>                        # Identifiers
>                        ( [a-zA-Z_] [a-zA-Z0-9_]* ) =>
>                                { token = Identifier; fbreak; };
> 
>                        # Whitespace
>                        [ \t\n];
> 
>             # Number
>                        digit+ => { token = Number; s->value = atoi(p);
> fbreak; };
> 
>                        # Anything else
>                        any => { token = *p; fbreak; };
> 
>                        *|;
> 
>                        write exec;
>                }%%
> 
>                if ( token != no_token ) {
>                        s->p = p;
>                        s->pe = pe;
>                        return token;
>                }
> 
>                if ( s->cs == Scanner_error )
>                        return ERR;
>        }
> }
> 
> 
> int main (int argc, char** argv)
> {
>    Scanner ss;
>    int tok;
> 
>    ss.pe = "2 * 3";
>    scan_init (&ss);
> 
>    while (1) {
>     tok = scan (&ss);
>     if (tok == EOF || tok == ERR)
>       break;
>     printf ("%d\n", tok);
>    }
> 
>    return 0;
> }
> 
> 
> 
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: pullscan.rl
URL: <http://www.colm.net/pipermail/ragel-users/attachments/20070104/64beea16/attachment-0001.ksh>


More information about the ragel-users mailing list