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 t
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
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.)
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.