lex生成工具生成统计文本.txt

来自「利用LEX生成工具生成统计文本文件中字符、单词和行数」· 文本 代码 · 共 39 行

TXT
39
字号
程序源代码:
%{
#include<stdio.h>
#include<ctype.h>
int chars=0;   
int words=0;  
int lines=1;   
%}
final [\t\n]                                   /*定义正则表达式*/
letter [A-Za-z]
digit [0-9]
word ({letter}|{digit})+

%%
{final}             {lines++;}                /*行数+1*/                     
{letter}              {chars++;}  
{digit}              {chars++;}               /*字符+1*/
{word}              {words++; chars+=yyleng; } /*word时单词数和字符数同时+1*/

%%
void main()
{
    yylex();
    printf("the numbers of the line is %d\n",lines);    
    printf("the numbers of the word is %d\n",words); 
    printf("the numbers of the char is %d\n",chars); 
}
int yywrap()
{  
   return 1;
}

程序输出测试:
输入:i:integer
      J:integer
输出:the number of the line is 2
      the number of the word is 4
      the number of the char is 20

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?