编写 .l文件
%{
c 头文件 和变量定义
%}
%%
{pattern} {c action code;}
%%
c function definition
compile && link
flex -o my.c my.l
gcc -o my my.c -lfl
当无main函数时, 需要 -lfl && %option noreject noyywrap
中文支持:
.l 文件与需要检测的数据文件是同一编码, 否则识别不出来
vim securecrt centos 编辑显示中文:
centos 编辑i18n文件, 设置utf8
securecrt 设置成utf8
~/.vimrc: set encoding=utf8 && set termencoding=utf8
====================================================================
example:
#!/bin/sh cat keyword | gawk 'BEGIN{print "%%";} { #check if need "" if(index($1, "/"") == 0) print "/"" $0 "/" {"; else print $0 " {"; print "/tprintf(/"found: %s, lineno: %d, length: %d//n/"" ", yytext, yylineno, yyleng);"; print "/tstrcpy(pat, yytext);"; print "/tyyret = 1;"; print "/tYY_FLUSH_BUFFER;/*do not resume scan remain buffer. */"; print "/treturn 1;/*1 will be returned by yylex *//n}/n/n"; } END{ print ".|//n {/* eat up any unmatched. do nothing */}/n/n"; print "<<EOF>> {"; print "/treturn 0;/n}/n/n" }' > kk && cat head kk tail > k2.l make
makefile:
# Basic Makefile -- provides explicit rules
# Creates "myprogram" from "scan.l" and "myprogram.c"
#
CC = g++
CFLAGS = -g
LDFLAGS = #-lfl, can use this if && only if no main provided,
LEX=flex
check: k2.o check.o
$(CC) -o $@ $(LDFLAGS) $^
check.o: check.cpp
$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $^
k2.o: k2.cpp
$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $^
k2.cpp: k2.l
$(LEX) $(LFLAGS) -o $@ $^
clean:
$(RM) *.o k2.cpp
check.cpp: test code
#include <stdio.h> #include <time.h> extern int check2(FILE *f, char *fn, char *pattern, FILE *out, int *ret, int output); int main(int argc, char *argv[]) { int ret; int yret = 0; clock_t start, end; unsigned int all; unsigned int found = 0, notfound = 0; FILE *out = fopen("/dev/null", "a"); char pattern[1024]; int i = 0, j = 0; char *prefix = "../../../sample_mail/spam_test/spam."; char path[200]; for(i = 1; i < 10001; i++) { sprintf(path, "%s%d", prefix, i); FILE * f = fopen(path, "r"); if(f != 0) { start = clock(); for(j = 0; j < 10; j++) { fseek(f, 0, SEEK_SET); pattern[0] = 0; ret = check2(f, path, pattern, out, &yret, (int)(j==0)); } end = clock(); all += end - start; if(ret > 0) found++; else notfound++; } else { printf("can not open file: %s/n", path); } fclose(f); } printf("total time: %d, found: %d, notfound; %d/n", all, found, notfound); return 0; }