2016-01-25 56 views
1

文件:myscanner.h如何區分ENTER引起的新行和 n使用lex寫入文件?

#define TYPE 1 
#define NAME 2 
#define TABLE_PREFIX 3 
#define PORT 4 
#define COLON 5 
#define IDENTIFIER 6 
#define INTEGER 7 

輸入文件到掃描器:

db_type : mysql 
\n 
db_name : textdata 
db_table_prefix : test_ 
db_port : a1099 

如果myscanner.l是:

%{ 
#include "myscanner.h" 
int nl=0; 
%} 

%% 
:      return COLON; 
"db_type"    return TYPE; 
"db_name"    return NAME; 
"db_table_prefix"  return TABLE_PREFIX; 
"db_port"    return PORT; 
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;} 
[1-9][0-9]*    return INTEGER; 
[ \t]     ; 
\n      yylineno++; 
.      printf("unexpected character\n"); 

%% 

int yywrap(void) 
{ 
     return 1; 
} 

然後在輸入文件的第2行示出了作爲意外的錯誤字符()。 如果myscanner.l是:

%{ 
#include "myscanner.h" 
int nl=0; 
%} 

%% 
:      return COLON; 
"db_type"    return TYPE; 
"db_name"    return NAME; 
"db_table_prefix"  return TABLE_PREFIX; 
"db_port"    return PORT; 
"\n"     nl++; 
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;} 
[1-9][0-9]*    return INTEGER; 
[ \t]     ; 
\n      yylineno++; 
.      printf("unexpected character\n"); 

%% 

int yywrap(void) 
{ 
     return 1; 
} 

然後通過myscanner.l萊克斯說自己的錯誤「的規則不能在

\n      yylineno++; 

我的問題是相匹配: 做什麼,如果我有寫\ n我在文件中的兩個字符「\」,然後「N」

回答

0

組合你需要轉義反斜線:

"\\n" { /* Will match \n */ } 
相關問題