I've written a .l file and want to output the contents in "c17.isc".
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.
This is the c17.isc file The contents means
number gate_name gate_type output_number input_number fault
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
This is the flex file I've written.
First, this is declare file:
# 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
Second, this is the flex file:
%{
# 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.
For example, the first wrong line in the picture should be "num=10
", the second wrong line should be "il=1 il=8
" etc.
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** ...
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.For your sample input, the output is:
The line with
num1=10
hasil=1
andil=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.)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).I'm not sure I understand your scenario correctly, but it looks like you are doing all the work of parsing the file using Flex and regular expressions?
The usual way is to use Flex to generate a scanner (the function yylex) that just identifies the tokens. A token can be a single number or gate name. The scanner then returns as soon as it has found a token. So the scanner transforms the input (the sequence of characters on your file) to a sequence of tokens.
Then you use a parser generator, typically Bison, to generate a parser, which compares those individual tokens to the grammar, and the larger structure of your input is then handled on the parser level.
It gets very complicated when you are trying to do it all in Flex, which isn't really suited for it.