久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

折騰FLEX(詞法分析器)

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

模板范例,從sgml解析htm里抄來的,簡單跑跑很舒爽。
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ù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 欧美日韩视频在线 | 成人精品鲁一区一区二区 | 欧美一级淫片免费视频黄 | 欧美9999 | 国产成人精品a视频 | 亚洲一区二区三区在线 | 欧美国产视频 | 国产视频第一页 | 欧美一级欧美一级在线播放 | 午夜视频在线免费观看 | 丁香婷婷久久久综合精品国产 | 国产精品一区二区免费看 | 99久久婷婷国产综合精品电影 | 精品久久久久久久久久久院品网 | 色婷婷综合久久久中字幕精品久久 | 日韩一区二区在线免费观看 | 国产精品视频一二三区 | 欧美精选一区二区 | 亚洲欧美在线观看 | 久久免费视频1 | 国产综合一区二区 | 天天综合亚洲 | 日本不卡一区二区三区 | 久久av一区二区三区 | 免费看91| 男女午夜激情视频 | 国产成人免费网站 | 成人av影院 | 国产成人高清 | 国产一区二区三区视频免费观看 | 中文字幕不卡一区 | 成人在线观看免费视频 | 欧美久久一级 | aaaa一级毛片 | 成人在线免费观看 | 中文字幕亚洲一区二区三区 | 美女啪啪国产 | 91视频亚洲| 狠狠躁18三区二区一区 | 亚洲高清视频在线观看 | 一区在线播放 |