integrating ragel w/ bison

Jason jason2... at jasonjobe.com
Thu Jan 4 18:21:54 UTC 2007


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;
}



More information about the ragel-users mailing list