Error in the output of my flex file

2020-05-06 10:30发布

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**  ...

2条回答
姐就是有狂的资本
2楼-- · 2020-05-06 11:03

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

查看更多
在下西门庆
3楼-- · 2020-05-06 11:09

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.

查看更多
登录 后发表回答