編譯原理課程作業,實在不會,發出來求助

需要利用flex將html檔案轉為txt檔案
要求是:
(1)洗掉html的所有html標簽,并將轉換后的檔案直接輸出到螢屏上.
(2)洗掉所有的開始標記為<script...>, <style...>和<form...>
結束標記為</script>,</style>和</form>中間的所有內容
(3) 提出所有的錨中的超鏈,并將其保存在新建的文本檔案hyplink.txt中
/* like NONEED, SCPT and FORM, 請完成TAG, LINK條件模式下對應
正則運算式和對應的C語言動作 */
html2txt.l如下:
%{
#include <stdio.h>
#include <string.h>
FILE *hyplink;
int lastchar; /* if last output char is newline */
%}
%x NONEED
%x FORM
%x LINK
%x SCPT
%x TAG
%x COMMENT
%%
("<"[sS][cC][rR][iI][pP][tT][^>]*">") BEGIN(SCPT);
("<"[sS][tT][yY][lL][eE][^>]*">") BEGIN(NONEED);
"<"[iI][fF][rR][aA][mM][eE][^>]*">" BEGIN(NONEED);
("<!--"[sS][cC][rR][iI][pP][tT][^>]*">") BEGIN(SCPT);
("<"[Ff][oO][rR][Mm][^>]*">") BEGIN(FORM);
"<"[aA] BEGIN(LINK);
"<" BEGIN(TAG);
<NONEED>"</"[sS][tT][yY][lL][eE][^>]*">" BEGIN(INITIAL);
<NONEED>"</"[iI][fF][rR][aA][mM][eE][^>]*">" BEGIN(INITIAL);
<NONEED>.|\n ;
<SCPT>"/"[sS][cC][rR][iI][pP][tT][^>]*">"+(\n)* BEGIN(INITIAL);
<SCPT>.|\n {; }
<FORM>("</"[Ff][oO][rR][Mm][^>]*">"+(\n)*) BEGIN(INITIAL);
<FORM>.|\n { ;
/* like NONEED, SCPT and FORM, 請完成TAG, LINK條件模式下對應
正則運算式和對應的C語言動作 */
}
(&[qQ][oO][tT]";") {
printf("'");
}
(&[gG][tT]";") {
printf(">");
}
(&[lL][tT]";") {
printf("<");
}
(&[aA][mM][pP]";") {
printf("&");
}
(&[nN][bB][sS][pP]";") {
printf(" ");
}
[\r\t]* {; }
("</"[lL][iI]">") {; }
("</"[pP]">") {; }
("-->") {; }
%%
int main(int argc, char **argv )
{
++argv, --argc;
if ( argc > 0 ) {
if ((yyin = fopen( argv[0], "rb" )) == NULL) {
printf("the file %s could'nt open!\n", argv[0]);
exit (1);
}
}
else
yyin = stdin;
if ((hyplink = fopen("hyplink.txt", "w")) == NULL) {
printf("coud'nt create hyperlink file!\n");
exit(1);
}
yylex();
fclose(yyin);
fclose(hyplink);
return 0;
}
int yywrap()
{
return 1;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/269060.html
標籤:其它技術問題
上一篇:在線等!急!
