欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2355|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

折騰FLEX(詞法分析器)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:127229 發(fā)表于 2016-6-19 01:42 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
上周折騰了半天FLEX詞法分析器,N多年前玩過,現(xiàn)在全忘記了,撿起來重新折騰了一下,小計(jì)之,下次撿起來快一點(diǎn)。
FLEX是什么? 把文本按一定規(guī)則(正則表達(dá)式定義)把文本格式化輸出。循環(huán)版本的正則表達(dá)式。
干什么用的?   通常用于數(shù)據(jù)格式化,比如解析源代碼,解析結(jié)構(gòu)化的文本等等。
怎么玩?           配置一個(gè).l模板文件,描敘規(guī)則,然后用 flex.exe 生成解析C源碼,復(fù)制到工程里去,完事。
存在的問題?   中文比較麻煩,目前沒需求,以后有需求再繼續(xù)折騰
價(jià)值收益?       開腦洞,又多了一個(gè)偷懶神器

模板范例,從sgml解析htm里抄來的,簡(jiǎn)單跑跑很舒爽。
test.l:
%{


   #include "stdio.h"
   #include "stdlib.h"


   int num_num=0,num_id=0;

   #define fileno _fileno
   int bShow = 0;
%}

/* $Id: sgml.l,v 1.9 1996/02/07 15:32:28 connolly Exp $ */
/* sgml.l -- a lexical analyzer for Basic+/- SGML Documents
* See: "A Lexical Analyzer for HTML and Baisc SGML"
*/

/*
* NOTE: We assume the locale used by lex and the C compiler
* agrees with ISO-646-IRV; for example: '1' == 0x31.
*/


/* Figure 1 -- Character Classes: Abstract Syntax */

Digit        [0-9]
LCLetter    [a-z]
Special        ['()_,\-\./:=?]
UCLetter    [A-Z]

/* Figure 2 -- Character Classes: Concrete Syntax */

LCNMCHAR    [\.-]
/* LCNMSTRT    [] */
UCNMCHAR    [\.-]
/* UCNMSTRT    [] */
/* @# hmmm. sgml spec says \015 */
RE        \n
/* @# hmmm. sgml spec says \012 */
RS        \r
SEPCHAR        \011
SPACE        \040

/* Figure 3 -- Reference Delimiter Set: General */

COM    "--"
CRO    "&#"
DSC    "]"
DSO    "["
ERO    "&"
ETAGO  "</"
LIT    \"
LITA   "'"
MDC    ">"
MDO    "<!"
MSC    "]]"
NET    "/"
PERO   "%"
PIC    ">"
PIO    "<?"
REFC   ";"
STAGO  "<"
TAGC   ">"

/* 9.2.1 SGML Character */

/*name_start_character    {LCLetter}|{UCLetter}|{LCNMSTRT}|{UCNMSTRT}*/
name_start_character    {LCLetter}|{UCLetter}
name_character        {name_start_character}|{Digit}|{LCNMCHAR}|{UCNMCHAR}|[\xa1-\xff]

/* 9.3 Name */

name        {name_start_character}{name_character}*
number        {Digit}+
number_token    {Digit}{name_character}*
name_token    {name_character}+

/* 6.2.1 Space */
s        {SPACE}|{RE}|{RS}|{SEPCHAR}
ps        ({SPACE}|{RE}|{RS}|{SEPCHAR})+

/* trailing white space */
ws        ({SPACE}|{RE}|{RS}|{SEPCHAR})*

/* 9.4.5 Reference End */
reference_end    ({REFC}|{RE})

/*
* 10.1.2 Parameter Literal
* 7.9.3  Attribute Value Literal
* (we leave recognition of character references and entity references,
*  and whitespace compression to further processing)
*
* @# should split this into minimum literal, parameter literal,
* @# and attribute value literal.
*/
literal        ({LIT}[^\"]*{LIT})|({LITA}[^\']*{LITA})



/* 9.6.1 Recognition modes */

/*
* Recognition modes are represented here by start conditions.
* The default start condition, INITIAL, represents the
* CON recognition mode. This condition is used to detect markup
* while parsing normal data charcters (mixed content).
*
* The CDATA start condition represents the CON recognition
* mode with the restriction that only end-tags are recognized,
* as in elements with CDATA declared content.
* (@# no way to activate it yet: need hook to parser.)
*
* The TAG recognition mode is split into two start conditions:
* ATTR, for recognizing attribute value list sub-tokens in
* start-tags, and TAG for recognizing the TAGC (">") delimiter
* in end-tags.
*
* The MD start condition is used in markup declarations. The COM
* start condition is used for comment declarations.
*
* The DS condition is an approximation of the declaration subset
* recognition mode in SGML. As we only use this condition after signalling
* an error, it is merely a recovery device.
*
* The CXT, LIT, PI, and REF recognition modes are not separated out
* as start conditions, but handled within the rules of other start
* conditions. The GRP mode is not represented here.
*/

/* EXCERPT ACTIONS: START */

/* %x CON == INITIAL */
%x CDATA

%x TAG
%x ATTR
%x ATTRVAL
%x NETDATA
%x ENDTAG
/* this is only to be permissive with bad end-tags: */
%x JUNKTAG

%x MD
%x COM
%x DS

/* EXCERPT ACTIONS: STOP */

%%

  int *types = NULL;
  char **strings = NULL;
  size_t *lengths = NULL;
  int qty = 0;

      /*
       * See sgml_lex.c for description of
       *   ADD, CALLBACK, ERROR, TOK macros.
       */

  /* <name -- start tag */
{STAGO}{name}{ws}        {
            //printf("TAG:[%s]\r\n",yytext);
                                  //ADDCASE(SGML_START, yytext, yyleng);
                  //BEGIN(ATTR);
                 //Sleep(200);
                }

  /* <a ^href = "xxx"> -- attribute name */
{name}{s}*={ws}        {
            if (stricmp(yytext,"href=")==0)
            {
                printf("ATTR:[%s]\r\n",yytext);
                bShow = 1;
            }
                }

  /* <a name = ^xyz> -- name token */
{name_token}{ws}        {
            //printf("ATTR2:[%s]\r\n",yytext);
                                }

  /* <a href = ^"a b c"> -- literal */
{literal}{ws}                {
            if (bShow)
            {
                printf("VALUE:[%s]\r\n",yytext);
                bShow = 0;
            }
                                }
                                                

.|\r|\n {}

%%

int main(int argc, char* argv[])
{
   yyin=fopen("./test.html","r");
   yylex();
   printf("num=%d,id=%d/n",num_num,num_id);
   return 0;
}

int yywrap()//此函數(shù)必須由用戶提供
{
    return 1;
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表