如何理解lex和yacc

本篇文章給大家分享的是有關如何理解lex和yacc,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

我們提供的服務有:做網(wǎng)站、網(wǎng)站制作、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、西平ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的西平網(wǎng)站制作公司

一、背景

     從零開始學習下lex和yacc

     1. 基礎

      lex只有狀態(tài)和狀態(tài)轉(zhuǎn)換,沒有棧,善于模式匹配;yacc能處理帶棧的FSA(有限狀態(tài)機),更適合更復雜的任務。

模式匹配原語

元字符
匹配說明
.
任意字符( 除了換行)

換行
*
0次或者多次重復前面的表達式
+
1次或者多次重復前面的表達式
?
0次或者1次重復前面的表達式
^
行的開始
$
行的結(jié)尾
a|b
a or b
(ab)+

1次或者多次重復組ab

[...]
任意一個出現(xiàn)的字符

一些匹配的例子

表達式
匹配說明
abc
abc
abc*
ab, abc, abcc, abccc,.....
abc+
abc, abcc, baccc,......
a(bc)+
abc, abcbc, abcbcbc,......
a(bc)?
a, abc
[abc]
a, b, c
[a-z]
a到z的任意字符
[a\-z]
a, -, z
[-az]
-, a, z
[a-zA-Z0-9]+
一個或者多個任何數(shù)字字母
[ \t\n]
witespace
[^ab]
除了a,b的任何字符
[a^b]
a, ^, b
[a|b]
a, |, b
a|b
a or b

匹配規(guī)則:

    1. 貪心: 兩個模式去匹同一個字符串,匹配上最長的模式

    2. 順序優(yōu)先: 兩個相同長度的模式, 匹配上先定義的模式

.l文件內(nèi)容的格式被%%分成了三部分,如下:

     ....definitions.....

%%

  .....rules....

%%

 ...subroutines...

    其中rules是必須的,其他部分可選

    一些內(nèi)置函數(shù)以及變量

int yylex(void)
調(diào)用分析器,返回 token
char *yytext
指定匹配的字符串
yyleng
匹配上的字符串的長度
int yywrap(void)
返回1 則結(jié)束了
FILE *yyout
輸出文件,默認 stdout
FILE *yyin
輸入文件, 默認stdin
INITIAL
initial start condition
BEGIN condition
switch start condition
ECHO 
write mached string

#define ECHO fwrite(yytext, yyleng, 1, yyout)

二、開始第一個例子

   環(huán)境說明:

          VMware Workstation 12 Pro, ubuntu17.04 ,lex2.6.1

    輸出文件的內(nèi)容并且在前面增加行號

   lineno.l

%{ 
    int yylineno;
%}
%%
^(.*)\n printf("%4d\t%s",  ++yylineno, yytext);
%%
int main(int argc, char *argv[])
{ 
    FILE *fp = NULL;
    if (argc == 2) {
      fp = fopen(argv[1], "r");
      if (NULL != fp) {
         yyin = fp;  
      } 
    }  
     
    yylex();
    
    if (NULL != fp) { 
       fclose(fp); 
    }
    
    return 0;
}

 使用lex將lineno.l文件轉(zhuǎn)換為.c文件

  $ lex lineno.l
  $ls
  lex.yy.c lineno.l

   使用gcc 將lex.yy.c編譯成可執(zhí)行文件

   $ gcc lex.yy.c -o lineno

    /tmp/ccNgesbZ.o:在函數(shù)‘yylex’中:
    lex.yy.c:(.text+0x55c):對‘yywrap’未定義的引用
    /tmp/ccNgesbZ.o:在函數(shù)‘input’中:
    lex.yy.c:(.text+0x116c):對‘yywrap’未定義的引用
    collect2: error: ld returned 1 exit status

    網(wǎng)上查詢說是要在.l文件中實現(xiàn)yywrap函數(shù)

    修改后:

%{   
  int yylineno;
%}
%%
^(.*)\n printf("%4d\t%s",  ++yylineno, yytext);
%%
int main(int argc, char *argv[])
{    
     FILE *fp = NULL;
     yylineno = 0; 
     
     if (argc == 2) {    
         fp = fopen(argv[1], "r");    
         if (NULL != fp) {   
            yyin = fp;   
          }   
      }   
        
      yylex();  
      
      if (NULL != fp) {    
          fclose(fp);  
      }   
      
     return 0;
}

int yywrap()
{ 
    return 1;
}

再次編譯成功

$gcc lex.yy.c -o lineno
$lineno lineno.l

  1    %{
   2            int yylineno;
   3    %}
   4
   5    %%
   6    ^(.*)\n printf("%4d\t%s",  ++yylineno, yytext);
   7    %%
   8
   9    int main(int argc, char *argv[])
  10    {
  11            FILE *fp = NULL;
  12            yylineno = 0;
  13
  14            if (argc == 2) {
  15                    fp = fopen(argv[1], "r");
  16                    if (NULL != fp) {
  17                            yyin = fp;
  18                    }
  19            }
  20
  21            yylex();
  22
  23            if (NULL != fp) {
  24                    fclose(fp);
  25            }
  26
  27            return 0;
  28    }
  29
  30    int yywrap()
  31    {
  32            return 1;

  33    }

以上就是如何理解lex和yacc,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

文章名稱:如何理解lex和yacc
文章源于:http://muchs.cn/article32/pisjpc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供域名注冊、網(wǎng)站策劃、靜態(tài)網(wǎng)站、Google、網(wǎng)頁設計公司、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設