Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Flex/examples/manual/   (Fast Lexical Analyzer Version 2.6©)  Datei vom 6.11.2012 mit Größe 2 kB image not shown  

Quelle  string1.lex   Sprache: Lex

 
/* 
 * string1.lex: Handling strings by using input()
 */

%{
#include <ctype.h>
#include <malloc.h>

#define ALLOC_SIZE 32 /* for (re)allocating the buffer */                   

#define isodigit(x) ((x) >= '0' && (x) <= '7'
#define hextoint(x) (isdigit((x)) ? (x) - '0' : ((x) - 'A') + 10)  

void yyerror(char *message)
{
  printf("\nError: %s\n",message);
}

%}

%%

\" {
     int  inch,count,max_size;
     char *buffer;
     int  temp;

     buffer   = malloc(ALLOC_SIZE);
     max_size = ALLOC_SIZE;
     inch     = input();
     count    = 0;
     while(inch != EOF && inch != '"' && inch != '\n'){
        if(inch == '\\'){
          inch = input();
          switch(inch){
          case '\n': inch = input(); break;
          case 'b' : inch = '\b';    break;
          case 't' : inch = '\t';    break;
          case 'n' : inch = '\n';    break;
          case 'v' : inch = '\v';    break;
          case 'f' : inch = '\f';    break;
          case 'r' : inch = '\r';    break;
          case 'X' :
          case 'x' : inch = input();
                     if(isxdigit(inch)){
                       temp = hextoint(toupper(inch));
                       inch = input();
                       if(isxdigit(inch)){
                         temp = (temp << 4) + hextoint(toupper(inch));
                       } else {
                         unput(inch);
                       }
                       inch = temp; 
                     } else {
                       unput(inch);
                       inch = 'x';
                     }
             break;
          default:
             if(isodigit(inch)){
                temp = inch - '0';
                inch = input();
                if(isodigit(inch)){
                  temp = (temp << 3) + (inch - '0');
                } else {
                  unput(inch);
                  goto done;
                }
                inch = input();
                if(isodigit(inch)){
                  temp = (temp << 3) + (inch - '0');
                } else {
                  unput(inch);
                }
             done:
                inch = temp; 
             }
          } 
        }
        buffer[count++] = inch;
        if(count >= max_size){
           buffer = realloc(buffer,max_size + ALLOC_SIZE);
           max_size += ALLOC_SIZE;
        }           
        inch = input();
     }
     if(inch == EOF || inch == '\n'){
       yyerror("Unterminated string.");
     }
     buffer[count] = '\0';
     printf("String = \"%s\"\n",buffer);
     free(buffer);
   }
.
\n
%%


84%


¤ Dauer der Verarbeitung: 0.10 Sekunden  (vorverarbeitet)  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung ist noch experimentell.