lex.l (1506B)
1 %option noyywrap 2 3 %{ 4 #include <err.h> 5 #include <stdio.h> 6 #include <string.h> 7 #include <stdlib.h> 8 9 #include "token.h" 10 11 int lineno = 1; 12 %} 13 14 DELIM [ \t]+ 15 KEYWORD deffacts|defrule|test|bind|read|printout 16 OPERATOR =|\+|-|\*|\/ 17 INT 0|[+-]?[1-9]+[0-9]* 18 FLOAT [+-]?[0-9]+((\.[0-9]+)([eE][+-]?[0-9]*)?|([eE][+-]?[0-9]*)?) 19 STR \"[^\"\\]*(?:\\.[^\"\\]*)*\" 20 DEFIN [A-Za-z]+[A-Za-z0-9_-]* 21 VAR \?[A-Za-z0-9]+ 22 COMMENT ;.* 23 UNKNOWN [^ {DELIM}\n]+ 24 25 %% 26 {DELIM} { /* ignore whitespace */ } 27 {KEYWORD} { return TOK_KEYWORD; } 28 {OPERATOR} { return TOK_OPERATOR; } 29 {INT} { return TOK_INT; } 30 {FLOAT} { return TOK_FLOAT; } 31 {STR} { return TOK_STR; } 32 {DEFIN} { return TOK_DEFIN; } 33 {VAR} { return TOK_VAR; } 34 {UNKNOWN} { return TOK_UNKNOWN; } 35 {COMMENT} { /* skip comments */ } 36 \n { lineno++; } 37 <<EOF>> { printf("EOF\n"); exit(0); } 38 %% 39 40 static char *tokens[] = { 41 "INT", /* integers */ 42 "FLOAT", /* floats */ 43 "STR", /* strings */ 44 "DEFIN", /* definitions */ 45 "VAR", /* variables */ 46 "KEYWORD", /* keywords */ 47 "OPERATOR", /* operators */ 48 "UNKNOWN", /* unknown token */ 49 }; 50 51 int 52 main(int argc, char *argv[]) 53 { 54 int tok; 55 56 if (argc < 2) { 57 fprintf(stderr, "usage: %s input [output]\n", *argv); 58 return (-1); 59 } 60 if ((yyin = fopen(argv[1], "r")) == NULL) 61 err(1, "fopen(%s)", argv[1]); 62 if (argc == 3 && (yyout = fopen(argv[2], "w")) == NULL) 63 err(1, "fopen(%s)", argv[2]); 64 65 while ((tok = yylex()) >= 0) { 66 fprintf(yyout, "line: %5d\ttoken=%s\tval='%s'\n", 67 lineno, tokens[tok-1], yytext); 68 } 69 70 return (0); 71 }