我有一个HTML表格代码,需要使用Linux系统中的Flex实用程序转换为纯文本。
我在.lex文件中提出了令牌列表,如下所示:
OPENTABLE <table>
CLOSETABLE </table>
OPENROW <tr>
CLOSEROW </tr>
OPENHEADING <th>
CLOSEHEADING </th>
OPENDATA <td>
CLOSEDATA </td>
STRING [0-9a-zA-Z]*
%%
%%
HTML解析的我的CGF(包含翻译方案)如下所示:
TABLE --> OPENTABLE ROWLIST CLOSETABLE ;
ROWLIST --> ROWLIST ROW | ^ ;
ROW --> OPENROW DATALIST CLOSEROW printf("\n");
DATALIST --> DATALIST DATA | ^ ;
DATA --> OPENDATA STRIN CLOSEDATA printf(yytext+"\t");
我已经看过一些例子,但我没有得到我应该在.lex文件的规则部分写的内容。
答案 0 :(得分:0)
我在基础知识上花了一些时间,并想出来了。 Flex'信息页面非常有帮助。这是所需文件。效果很好,但仍需要改进。
%{
#include <string.h>
char *substring(char* str)
{
int i = 0;
int l = strlen(str);
char *str2;
str2 = malloc(l+1);
for (i=4; i < l-5;i++)
{
str2[i-4] = str[i];
}
return str2;
}
%}
OPENTABLE "<table>"
CLOSETABLE "</table>"
OPENROW "<tr>"
CLOSEROW "</tr>"
OPENHEADING "<th>"
CLOSEHEADING "</th>"
OPENDATA "<td>"
CLOSEDATA "</td>"
STRING [a-zA-Z0-9]*
%%
{OPENDATA}.{STRING}.{CLOSEDATA} printf("%s\t", substring(yytext));
{OPENHEADING}.{STRING}.{CLOSEHEADING} printf("%s\t", substring(yytext));
{CLOSEROW} printf("\n");
. ;
[ \n\t] ;
%%
int main(int argc, char** argv)
{
++argv, --argc;
yyin = fopen(argv[0], "r");
yylex();
}