Lex:同时匹配多个正则表达式

问题描述 投票:1回答:1

我有以下代码:我想使用

计算字符数
(.)   {charCount++;}

并且同时使用]计算单词数>

([a-zA-Z0-9])*    {wordcount++;}

使用lex规则是否可行,或者我是否必须使用c代码中的文件流“手动/编程地”对其进行计数。基本上有“继续匹配/正则表达式”的代码]

%% 
[\t ]+  ; //ignore white space
"\n" ;  //ignore next line // 
([a-zA-Z0-9])*    {wordcount++;}
(.)   {charCount++;}
%% 
int yywrap(void){} 
int main() 
{    
    // The function that starts the analysis 
    yyin=fopen("input.txt", "r");
    yylex(); 
    printf("The number of words in the file is %d and charCount %d",count,wordSize);
    return 0; 
}

我有下面的代码:我想使用(。){charCount ++;}计算字符数,同时使用([a-zA-Z0-9])* {wordcount ++; }是否可能...

c regex compiler-construction lex lexical-analysis
1个回答
2
投票

yyleng中提供了与规则匹配的字符数,因此您可以这样做:

© www.soinside.com 2019 - 2024. All rights reserved.