Blame tests/string_r.l

Packit f00812
/*
Packit f00812
 * This file is part of flex.
Packit f00812
 * 
Packit f00812
 * Redistribution and use in source and binary forms, with or without
Packit f00812
 * modification, are permitted provided that the following conditions
Packit f00812
 * are met:
Packit f00812
 * 
Packit f00812
 * 1. Redistributions of source code must retain the above copyright
Packit f00812
 *    notice, this list of conditions and the following disclaimer.
Packit f00812
 * 2. Redistributions in binary form must reproduce the above copyright
Packit f00812
 *    notice, this list of conditions and the following disclaimer in the
Packit f00812
 *    documentation and/or other materials provided with the distribution.
Packit f00812
 * 
Packit f00812
 * Neither the name of the University nor the names of its contributors
Packit f00812
 * may be used to endorse or promote products derived from this software
Packit f00812
 * without specific prior written permission.
Packit f00812
 * 
Packit f00812
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
Packit f00812
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
Packit f00812
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
Packit f00812
 * PURPOSE.
Packit f00812
 */
Packit f00812
Packit f00812
%{
Packit f00812
/* A template scanner file to build "scanner.c". */
Packit f00812
#include <stdio.h>
Packit f00812
#include <stdlib.h>
Packit f00812
#include "config.h"
Packit f00812
Packit f00812
#define NUMBER 200
Packit f00812
#define WORD   201
Packit f00812
Packit f00812
%}
Packit f00812
Packit f00812
%option 8bit prefix="test"
Packit f00812
%option nounput nomain nodefault noyywrap noinput
Packit f00812
%option warn reentrant
Packit f00812
Packit f00812
Packit f00812
%%
Packit f00812
Packit f00812
[[:space:]]+   { }
Packit f00812
[[:digit:]]+   { printf("NUMBER "); fflush(stdout);}
Packit f00812
[[:alpha:]]+   { printf("WORD "); fflush(stdout);}
Packit f00812
.              {
Packit f00812
    fprintf(stderr,"*** Error: Unrecognized character '%c' while scanning.\n",
Packit f00812
         yytext[0]);
Packit f00812
    yyterminate();
Packit f00812
    }
Packit f00812
Packit f00812
<<EOF>>  { printf("<<EOF>>\n"); yyterminate();}
Packit f00812
Packit f00812
%%
Packit f00812
Packit f00812
Packit f00812
#define INPUT_STRING_1  "1234 foo bar"
Packit f00812
#define INPUT_STRING_2  "1234 foo bar *@&@&###@^$#&#*"
Packit f00812
Packit f00812
int main(void);
Packit f00812
Packit f00812
int
Packit f00812
main ()
Packit f00812
{
Packit f00812
    char * buf;
Packit f00812
    int len;
Packit f00812
    YY_BUFFER_STATE state;
Packit f00812
    yyscan_t  scanner=NULL;
Packit f00812
Packit f00812
Packit f00812
    /* Scan a good string. */
Packit f00812
    printf("Testing: yy_scan_string(%s): ",INPUT_STRING_1); fflush(stdout);
Packit f00812
    yylex_init(&scanner);
Packit f00812
    state = yy_scan_string ( INPUT_STRING_1 ,scanner);
Packit f00812
    yylex(scanner);
Packit f00812
    yy_delete_buffer(state, scanner);
Packit f00812
    yylex_destroy(scanner);
Packit f00812
Packit f00812
    /* Scan only the first 12 chars of a string. */
Packit f00812
    printf("Testing: yy_scan_bytes(%s): ",INPUT_STRING_2); fflush(stdout);
Packit f00812
    yylex_init(&scanner);
Packit f00812
    state = yy_scan_bytes  ( INPUT_STRING_2, 12 ,scanner);
Packit f00812
    yylex(scanner);
Packit f00812
    yy_delete_buffer(state,scanner);
Packit f00812
    yylex_destroy(scanner);
Packit f00812
Packit f00812
    /* Scan directly from a buffer.
Packit f00812
       We make a copy, since the buffer will be modified by flex.*/
Packit f00812
    printf("Testing: yy_scan_buffer(%s): ",INPUT_STRING_1); fflush(stdout);
Packit f00812
    len = strlen(INPUT_STRING_1) + 2;
Packit f00812
    buf = malloc(len);
Packit f00812
    strcpy( buf, INPUT_STRING_1);
Packit f00812
    buf[ len -2 ]  = 0; /* Flex requires two NUL bytes at end of buffer. */
Packit f00812
    buf[ len -1 ] =0;
Packit f00812
Packit f00812
    yylex_init(&scanner);
Packit f00812
    state = yy_scan_buffer( buf, len ,scanner);
Packit f00812
    yylex(scanner);
Packit f00812
    yy_delete_buffer(state,scanner);
Packit f00812
    yylex_destroy(scanner);
Packit f00812
    
Packit f00812
    printf("TEST RETURNING OK.\n");
Packit f00812
    return 0;
Packit f00812
}