计算行数、字符数、数字和关键字数的 Lex 程序

Lex program to count number of lines, characters, digits, and key words

我一直致力于开发 lex 扫描器,但是当我将它输入我的输入文件时,它产生了错误的输出。这是我的源代码:

%{
#include <stdio.h>

int NumberOfLines=0;
int NumberOfChar=0;
int NumberOfIntegers=0;
int KWCount=0;
int NumberOfComments=0;
%}

DIGIT   [0-9]*
ID  [a-z][a-z0-9]*
%x COMMENT
%option noyywrap
%%

^[\t]*"/*" {BEGIN COMMENT;}
^[\t]*"/*".*"*/"[\t]*\n {NumberOfComments++;}

<COMMENT>"*/"[\t]*\n {BEGIN 0; NumberOfComments++;}
<COMMENT>"*/" {BEGIN 0;}
<COMMENT>\n {NumberOfComments++;}
<COMMENT>.\n {NumberOfComments++;}

\n {NumberOfLines++, NumberOfChar++; NumberOfChar +=strlen(yytext);}
. {NumberOfChar++; NumberOfChar +=strlen(yytext);}



{DIGIT}     {NumberOfIntegers++; NumberOfChar +=strlen(yytext); }


{DIGIT}+"."{DIGIT}* {
    printf("A flot: %s (%g) \n", yytext, atof(yytext));
    NumberOfChar +=strlen(yytext); 
    }

if|else|while|return    {
    printf("A keyword: %s\n", yytext); KWCount++;
    NumberOfChar +=strlen(yytext); 
    }

{ID}        {
    printf("An identifier: %s\n", yytext);
    NumberOfChar +=strlen(yytext); 
    }
"{"[^}\n]*"}"   {
    /*each up one-line comments*/
    NumberOfChar +=strlen(yytext);
    }

%%
int main(int argc, char **argv){
    ++argv, --argc; /*skip over program name */
    if (argc > 0)
        yyin = fopen(argv[0], "r"); 
    else
        yyin = stdin; 
    yylex();
    printf("Character count: %d",NumberOfChar);
    printf("\n");
    printf("Number count: %d",NumberOfIntegers);
    printf("\n");
    printf("Keyword count: %d",KWCount);
    printf("\n");
    printf("Line count: %d",NumberOfLines);
    printf("\n");
    printf("Comment count: %d", NumberOfComments);
    printf("\n"); 
    return 0; 
}

每当我 运行 带有源的输入文件时,它都会给我错误的输出。例如文件的输出应该是:

输出:

Number of Keywords: 3

Number of Characters: 196

Number of Lines: 17

Number of Digits: 3

但是它当前产生的输出是:

输出:

Number of keywords: 0

Number of Characters: 3

Number of Lines: 7

Number of Digits: 0   

我怀疑这与我的正则表达式有关,任何帮助将不胜感激,因为我仍在学习正则表达式!

这是我的输入文件内容:

/*comment 1*/
/*comment
  comment 2 
  */
  /*comment 3*
   */if this is a line
{comment 4}
int i = 789; 
int j = 689;
if i == 172 then
 {comment 5}
else
{comment 6}
{comment 7}
/*8 comments
 *
 */ 
end

下面是一些基本可用的代码,与您的代码密切相关。

%{
#include <stdio.h>

int NumberOfLines=0;
int NumberOfChar=0;
int NumberOfIntegers=0;
int KWCount = 0;
int IDCount = 0;
int RCCount = 0;
int OCCount = 0;
int DTCount = 0;
int FLCount = 0;
%}

%option noyywrap
%option noinput
%option nounput

DIGIT   [0-9]*
ID  [a-z][a-z0-9]*

%%

\n {NumberOfLines++; NumberOfChar++; RCCount += strlen(yytext); }
. {NumberOfChar++; DTCount++; RCCount++; printf(" '%c'", yytext[0]); }

{DIGIT}     {NumberOfIntegers++; RCCount += strlen(yytext); }

{DIGIT}+"."{DIGIT}* {
    printf("\nA float: %s (%g) \n", yytext, atof(yytext)); 
    RCCount += strlen(yytext);
    FLCount++;
    }

if|else|while|return    {
    printf("\nA keyword: %s\n", yytext); 
    KWCount++;
    RCCount += strlen(yytext);
    }

{ID}        {
    printf("\nAn identifier: %s\n", yytext); 
    IDCount++;
    RCCount += strlen(yytext);
    }
"{"[^}\n]*"}"   {
    RCCount += strlen(yytext);
    OCCount += strlen(yytext);
    }

%%
int main(int argc, char **argv){
    ++argv, --argc; /*skip over program name */
    if (argc > 0)
        yyin = fopen(argv[0], "r"); 
    else
        yyin = stdin; 
    yylex();
    printf("Character count: %d\n", NumberOfChar);
    printf("Number count:    %d\n", NumberOfIntegers);
    printf("Keyword count:   %d\n", KWCount);
    printf("Line count:      %d\n", NumberOfLines);
    printf("ID count:        %d\n", IDCount);
    printf("Dot count:       %d\n", DTCount);
    printf("Raw count:       %d\n", RCCount);
    printf("Float count:     %d\n", FLCount);
    printf("Other count:     %d\n", OCCount);
    printf("\n"); 
    return 0; 
}

当运行在数据文件上:

/*commEnt 1*/
/*COMMENT
  commEnt 2 
  */
  /*commEnt 3*
   */if this is a linE
{commEnt 4}
int i = 789; 
int j = 689;
if i == 172 thEn
 {commEnt 5}
ElsE
{commEnt 6}
{commEnt 7}
float 12.34
/*8 commEnts
 *
 else
 return
 while
 the
 going
 is
 good
 */ 
end

我得到输出:

 '/' '*'
An identifier: comm
 'E'
An identifier: nt
 ' ' '1' '*' '/' '/' '*' 'C' 'O' 'M' 'M' 'E' 'N' 'T' ' ' ' '
An identifier: comm
 'E'
An identifier: nt
 ' ' '2' ' ' ' ' ' ' '*' '/' ' ' ' ' '/' '*'
An identifier: comm
 'E'
An identifier: nt
 ' ' '3' '*' ' ' ' ' ' ' '*' '/'
A keyword: if
 ' '
An identifier: this
 ' '
An identifier: is
 ' ' 'a' ' '
An identifier: lin
 'E'
An identifier: int
 ' ' 'i' ' ' '=' ' ' ';' ' '
An identifier: int
 ' ' 'j' ' ' '=' ' ' ';'
A keyword: if
 ' ' 'i' ' ' '=' '=' ' ' ' '
An identifier: th
 'E' 'n' ' ' 'E'
An identifier: ls
 'E'
An identifier: float
 ' '
A float: 12.34 (12.34) 
 '/' '*' '8' ' '
An identifier: comm
 'E'
An identifier: nts
 ' ' '*' ' '
A keyword: else
 ' '
A keyword: return
 ' '
A keyword: while
 ' '
An identifier: the
 ' '
An identifier: going
 ' '
An identifier: is
 ' '
An identifier: good
 ' ' '*' '/' ' '
An identifier: end
Character count: 115
Number count:    3
Keyword count:   5
Line count:      26
ID count:        21
Dot count:       89
Raw count:       258
Float count:     1
Other count:     44

wc 的输出是:

$ wc data.2
      26      49     258 data.2
$

'raw count' 个字符与 wc 中的字符数匹配;行数也匹配。整数、浮点数、关键字和标识符的数量看起来都是正确的,因为大写字母被计算在 'dot characters' 中。你可以看看是否还有其他问题;我认为整数的计数是错误的,但我不确定为什么。