Created
January 3, 2014 13:49
-
-
Save LeslieZhu/8238153 to your computer and use it in GitHub Desktop.
一个Lex输入文件,可生成计算文本文件的字符、单词和行数且能报告该数字的程 序。单词的定义是不带标点或空格的字符和/或数字的序列。标点和空白格不计算为单 词。习题2.24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%option noyywrap | |
%{ | |
int ch_c = 0; | |
int word_c = 0; | |
int line_c = 0; | |
%} | |
fix [\ \,\;\.\}\{\)\(\n\:\#\%\-\+\*\&\^\$\!\~\`\=] | |
word [a-zA-Z0-9/\_]+ | |
%% | |
. { ch_c += 1;} | |
\n { line_c += 1;ch_c += 1;} | |
{fix}{word}/{fix} | | |
^{word}/{fix} { | |
word_c += 1; | |
//yytext++; | |
//printf("%d: %s\n",word_c,yytext); | |
//yytext--; | |
while(*yytext != '\0'){ | |
if(*yytext == '\n'){ | |
line_c += 1; | |
} | |
ch_c += 1; | |
yytext++; | |
} | |
} | |
%% | |
void main(){ | |
yylex(); | |
printf("char count: %d\nword count: %d\nline count: %d\n",ch_c,word_c,line_c); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment