[ragel-users] Re: Error actions and error recovery
Christer Sandberg
chr... at gmail.com
Thu Sep 6 06:35:42 UTC 2007
On 9/5/07, Adrian Thurston <thurs... at cs.queensu.ca> wrote:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> %%{
> machine foo;
>
> action ehandle1 {
> printf( "error 1 at char: %c\n", *p );
> fgoto err1;
> }
> action ehandle2 {
> printf( "error 2 at char: %c\n", *p );
> fgoto err2;
> }
>
> err1 := [^}]* '}' @{
> printf( "resuming from error 1\n" );
> fgoto recover1;
> };
> err2 := [^;}]* (
> ';' @{
> printf( "resuming from error 2\n" );
> fgoto recover2;
> } |
> '}' @{
> printf( "resuming from error 2\n" );
> fgoto recover1;
> }
> );
>
> something = ' '*;
> pr = alnum+ ';';
> se =
> something $!ehandle1
> '{'
> recover2: pr* $!ehandle2
> '}';
>
> main := recover1: se+ '\n';
> }%%
>
> %% write data nofinal;
>
> #define BUFSIZE 128
>
> void scanner()
> {
> static char buf[BUFSIZE];
> int cs, done = 0;
>
> %% write init;
>
> while ( !done ) {
> char *p = buf, *pe;
> int len, space = BUFSIZE;
>
> len = fread( p, 1, space, stdin );
>
> /* If this is the last buffer, tack on an EOF. */
> if ( len < space )
> done = 1;
>
> pe = p + len;
> %% write exec;
>
> if ( cs == foo_error ) {
> fprintf(stderr, "PARSE ERROR\n" );
> break;
> }
> }
> }
>
> int main()
> {
> scanner();
> return 0;
> }
>
>
>
>
Hi Adrian!
Thank you very much for the example. I've totally missed the
possibility of using labels inside machine definitions. I just had to
check the users guide as soon as I woke up this morning and found it
at 6.4.3. After a search in this group I see that you've mentioned it
before here as well. I'm new to Ragel and I've always felt that it's
my lack of knowledge that has stopped me from using it more than I've
been able to. It's very refreshing to learn new things like the labels
in your example. I'm trying to construct a CSS 2.1 scanner in Ragel as
a Ruby module.
Again, thank you very much!
Christer
More information about the ragel-users
mailing list