【发布时间】:2016-10-07 01:26:56
【问题描述】:
我最近开始学习 lex 并尝试了一些示例。 我正在尝试计算以“a”开头并以数字结尾的变量数量以及一维数组的数量。
%{
#undef yywrap
#define yywrap() 1
#include<stdio.h>
int count1;
int count2;
%}
%option noyywrap
%%
int|char|bool|float" "a[a-z,A-Z,0-9]*[0-9] {count1++;}
int|char|float|bool" "[a-z,A-Z]+[0-9,a-z,A-Z]*"["[0-9]+"]" {count2++;}
%%
void main(int argc,char** argv){
FILE *fh;
if (argc == 2 && (fh = fopen(argv[1], "r")))
yyin = fh;
printf("%d %d",count1,count2);
yylex();
}
我正在尝试计算 (1) 以“a”开头并以数字结尾的变量数量和 (2) 一维数组的数量。输入来自“f.c”文件。
//f.c
#include<stdio.h>
void main(){
char a;
char b;
char c;
int ab[5];
int bc[2];
int ca[7];
int ds[4];
}
两个计数都显示为零,输出为:
0 0#include<stdio.h>
void main(){
a;
b;
c;
ab[5];
bc[2];
ca[7];
ds[4];
}
另外,我如何计算同时属于这两个类别的变量?
【问题讨论】:
标签: c arrays flex-lexer lex