我使用Lex,Yacc和C++編寫自定義shell。它正在Unix環境中運行。只要標記之間有空格,它目前工作正常。例如:Lex解析無空格
ls | grep test > out
將通過:
WORD PIPE WORD WORD GREAT WORD
到Yacc的,然後行動從那裏取。但是,如果沒有空格,我需要它來工作。例如:
ls|grep test>out
應該和前面的命令一樣工作。然而,它目前只通過:
WORD WORD
有沒有一種方法來解析輸入之前,Lex標記它呢?
編輯:
這裏是我的萊克斯文件:
%{
#include <string.h>
#include "y.tab.h"
%}
%%
\n {
return NEWLINE;
}
[ \t] {
/* Discard spaces and tabs */
}
">" { return GREAT; }
">&" { return GREATAMPERSAND; }
">>" { return GREATGREAT; }
">>&" { return GREATGREATAMPERSAND; }
"<" { return LESS; }
"|" { return PIPE; }
"&" { return AMPERSAND; }
[^ \t\n][^ \t\n]* {
/* Assume that file names have only alpha chars */
yylval.string_val = strdup(yytext);
return WORD;
}
. {
/* Invalid character in input */
return NOTOKEN;
}
%%
你需要證明你的lex文件,以便我們能夠幫助您。您必須在某處錯誤定義了令牌才能獲得此行爲。 – Dervall 2012-02-23 14:41:45