Flex文件输出错误 [英] Error in the output of my flex file
问题描述
我已经写了一个.l文件,想在"c17.isc"中输出内容.
I've written a .l file and want to output the contents in "c17.isc".
但是有一个错误,我不知道为什么.我已经提供了我计划读取的文件,flex文件和执行结果.
But there is an error I don't know why. I've given the file I plan to read, the flex file and the execution result.
这是c17.isc文件 内容是
This is the c17.isc file The contents means
number gate_name gate_type output_number input_number fault
带有"from"的行表示扇出. 具有2个数字的行仅表示输入列表.
The line with "from" means fanout. The line with 2 numbers only means input list.
*c17 iscas example (to test conversion program only)
*---------------------------------------------------
*
*
* total number of lines in the netlist .............. 17
* simplistically reduced equivalent fault set size = 22
* lines from primary input gates ....... 5
* lines from primary output gates ....... 2
* lines from interior gate outputs ...... 4
* lines from ** 3 ** fanout stems ... 6
*
* avg_fanin = 2.00, max_fanin = 2
* avg_fanout = 2.00, max_fanout = 2
*
*
*
*
*
1 1gat inpt 1 0 >sa1
2 2gat inpt 1 0 >sa1
3 3gat inpt 2 0 >sa0 >sa1
8 8fan from 3gat >sa1
9 9fan from 3gat >sa1
6 6gat inpt 1 0 >sa1
7 7gat inpt 1 0 >sa1
10 10gat nand 1 2 >sa1
1 8
11 11gat nand 2 2 >sa0 >sa1
9 6
14 14fan from 11gat >sa1
15 15fan from 11gat >sa1
16 16gat nand 2 2 >sa0 >sa1
2 14
20 20fan from 16gat >sa1
21 21fan from 16gat >sa1
19 19gat nand 1 2 >sa1
15 7
22 22gat nand 0 2 >sa0 >sa1
10 20
23 23gat nand 0 2 >sa0 >sa1
21 19
这是我编写的flex文件.
This is the flex file I've written.
首先,这是声明文件:
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# define INPT 1
# define NOR 2
# define NAND 3
# define NOT 4
# define XOR 5
# define AND 6
# define BUFF 7
# define FROM 8
第二,这是弹性文件:
%{
# include "declare.h"
/*gi=1,it's input;gi=7,it's fanout;otherwise,it's gate*/
int gi=-1;
int inum=0;
int val;
struct{
char *symbol;
int val;
} symtab[]={
"inpt", INPT,
"nor", NOR,
"nand", NAND,
"not", NOT,
"xor", XOR,
"and", AND,
"buff", BUFF,
"from",FROM,
"0",0
};
extern FILE *yyin;
%}
%start A B C D E
DIGITS [0-9]+
BLANK [ \t\n]+
ALPHA [a-z]+
%%
"*".*\n {ECHO; BEGIN A;}
<A>{BLANK}{DIGITS} {printf("num=%s\t",yytext); BEGIN B;}
<B>{BLANK}{DIGITS}{ALPHA} {printf("name=%s",yytext); BEGIN C;}
<C>{BLANK}{DIGITS} {printf("op=%s\t",yytext);BEGIN D;}
<C>{BLANK}{DIGITS}{ALPHA} {ECHO; BEGIN A;}
<D>{BLANK}{DIGITS} {inum=atoi(yytext);
printf("ip=%s\t",yytext);
if(gi==1)
{BEGIN A;}
if(gi!=1)
{BEGIN E;}
}
<E>{BLANK}{DIGITS} {inum--;
if(inum<0)
{printf("num=%s\t",yytext); BEGIN B;}
else
{printf("il=%s\t",yytext); BEGIN E;}
}
{ALPHA} {gi=lookup(yytext);
if(gi!=0) printf("\tty=%d\t",gi);
else ECHO;
}
{BLANK}">sa"[0-1] {val=atoi(&yytext[yyleng-1]);printf("\tfl=%d",val);}
{BLANK} ;
%%
lookup(s)
char* s;
{int i;
for (i=0;symtab[i].val!=0;i++)
{
if(strcmp(symtab[i].symbol,s)==0)
break;
}
return(symtab[i].val);
}
main()
{
FILE *x=fopen("c17.isc","r");
yyin=x;
yylex();
}
这是执行结果.而且我用*标记了错误的位置.基本上,错误发生在带有输入列表的行上.
This is the execution result. And I've marked the wrong places using *. Basically the errors occur at the lines with input lists.
例如,图片中的第一行错误应该是"num=10
",第二行错误应该是"il=1 il=8
",等等.
For example, the first wrong line in the picture should be "num=10
", the second wrong line should be "il=1 il=8
" etc.
我对flex文件中的输入列表的操作位于E部分.但是我不知道为什么它不起作用.
My operation on input lists in flex file lies in part E.But I don't know why it doesn't work.
num=1 name=1gat ty=1 op=1 ip=0 fl=1
num=2 name=2gat ty=1 op=1 ip=0 fl=1
num=3 name=3gat ty=1 op=2 ip=0 fl=0 fl=1
num=8 name=8fan ty=8 3gat fl=1
num=9 name=9fan ty=8 3gat fl=1
num=6 name=6gat ty=1 op=1 ip=0 fl=1
num=7 name=7gat ty=1 op=1 ip=0 fl=1
**il=10** name=10gat ty=3 op=1 ip=2 fl=1
**num=1** il=8
**il=11** name=11gat ty=3 op=2 ip=2 fl=0 fl=1
**num=9** il=6
**num=4** ...
**num=5** ...
**il=16** ...
**num=2** il=14
**num=0** ...
**num=1** ...
**il=19** ...
**num=15** il=7
**il=22** ...
**il=23** ...
推荐答案
对代码的这种修改似乎可以按预期工作.进行了各种更改,最显着的是输出了一些换行符,并明确了num=
部分的识别位置.
This adaptation of your code seems likely to be working as you intended. There are various changes, most notably outputting some newlines, and making it clear where the num=
parts are recognized.
%{
#include "declare.h"
/*gi=1, it's input;gi=7, it's fanout;otherwise, it's gate*/
static int gi = -1;
static int inum = 0;
extern int lookup(const char *s);
struct
{
char *symbol;
int val;
} symtab[]=
{
{ "inpt", INPT },
{ "nor", NOR },
{ "nand", NAND },
{ "not", NOT },
{ "xor", XOR },
{ "and", AND },
{ "buff", BUFF },
{ "from", FROM },
{ "0", 0 },
};
extern FILE *yyin;
%}
%start A B C D E
DIGITS [0-9]+
BLANK [ \t\n]+
ALPHA [a-z]+
%%
"*".*\n {ECHO; BEGIN A;}
<A>{DIGITS} {printf("\nnum1=%s\t", yytext); BEGIN B;}
<B>{DIGITS}{ALPHA} {printf(" name=%s\t", yytext); BEGIN C;}
<C>{DIGITS} {printf(" op=%s\t", yytext); BEGIN D;}
<C>{DIGITS}{ALPHA} {ECHO; BEGIN A;}
<D>{DIGITS} {
inum=atoi(yytext);
printf(" ip=%s\t", yytext);
if (gi==1)
{BEGIN A;}
if (gi!=1)
{BEGIN E;}
}
<E>{DIGITS} {inum--;
if (inum<0)
{printf("\nnum2=%s\t", yytext); BEGIN B;}
else
{printf(" il=%s\t", yytext); BEGIN E;}
}
{ALPHA} {
gi = lookup(yytext);
if (gi!=0) printf(" ty=%d (%s)\t", gi, yytext);
else { printf("Lookup failed: "); ECHO; }
}
">sa"[0-1] {int val=atoi(&yytext[yyleng-1]);printf(" fl=%d", val);}
{BLANK} ;
. { printf("Unmatched: %s\n", yytext); }
%%
int lookup(const char *s)
{
int i;
for (i = 0; symtab[i].val != 0; i++)
{
if (strcmp(symtab[i].symbol, s) == 0)
break;
}
return(symtab[i].val);
}
int main(void)
{
FILE *x=fopen("c17.isc", "r");
yyin=x;
yylex();
putchar('\n');
}
对于您的示例输入,输出为:
For your sample input, the output is:
*c17 iscas example (to test conversion program only)
*---------------------------------------------------
*
*
* total number of lines in the netlist .............. 17
* simplistically reduced equivalent fault set size = 22
* lines from primary input gates ....... 5
* lines from primary output gates ....... 2
* lines from interior gate outputs ...... 4
* lines from ** 3 ** fanout stems ... 6
*
* avg_fanin = 2.00, max_fanin = 2
* avg_fanout = 2.00, max_fanout = 2
*
*
*
*
*
num1=1 name=1gat ty=1 (inpt) op=1 ip=0 fl=1
num1=2 name=2gat ty=1 (inpt) op=1 ip=0 fl=1
num1=3 name=3gat ty=1 (inpt) op=2 ip=0 fl=0 fl=1
num1=8 name=8fan ty=8 (from) 3gat fl=1
num1=9 name=9fan ty=8 (from) 3gat fl=1
num1=6 name=6gat ty=1 (inpt) op=1 ip=0 fl=1
num1=7 name=7gat ty=1 (inpt) op=1 ip=0 fl=1
num1=10 name=10gat ty=3 (nand) op=1 ip=2 fl=1 il=1 il=8
num2=11 name=11gat ty=3 (nand) op=2 ip=2 fl=0 fl=1 il=9 il=6
num2=14 name=14fan ty=8 (from) 11gat fl=1
num1=15 name=15fan ty=8 (from) 11gat fl=1
num1=16 name=16gat ty=3 (nand) op=2 ip=2 fl=0 fl=1 il=2 il=14
num2=20 name=20fan ty=8 (from) 16gat fl=1
num1=21 name=21fan ty=8 (from) 16gat fl=1
num1=19 name=19gat ty=3 (nand) op=1 ip=2 fl=1 il=15 il=7
num2=22 name=22gat ty=3 (nand) op=0 ip=2 fl=0 fl=1 il=10 il=20
num2=23 name=23gat ty=3 (nand) op=0 ip=2 fl=0 fl=1 il=21 il=19
具有num1=10
的行具有关联的il=1
和il=8
,这似乎反映了数据. (我修改了打印输出,以包括类型名称和类型编号.)
The line with num1=10
has il=1
and il=8
associated with it, which seems to reflect the data. (I modified the printout to include the type name as well as the type number.)
我不确定哪些更改是重要的更改.我认为,丢失匹配数字和字母的规则的{BLANK}
部分会简化事情(对于扫描仪来说,基本上忽略间距是很常见的.)
I'm not sure which changes are the significant ones. Losing the {BLANK}
part of the rules that match digits and alpha simplifies things, I think (it is very common for scanners to substantially ignore spacing).
这篇关于Flex文件输出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!