为什么我的野牛规则不起作用 [英] Why my rules of bison don't work

查看:115
本文介绍了为什么我的野牛规则不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我运行解析器时,它都会出现第1行中<>附近的语法错误"(因为存在子例程yyerror(char * s)).我认为这是因为我的野牛规则有问题.

Every time I run my parser, it will appear "syntax error in line 1 near <>" (Because there is a subroutine yyerror(char *s)). I think that's because there is something wrong with my rules in bison.

我要解析的文件(c17.isc).

The file (c17.isc) I want to parse.

*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文件如下,它是正确的.您可以在此处找到有关扫描仪工作方式的一些信息. 弹性文件输出中的错误

My flex file is as follows and it is right. You can find some information about how my scanner work here. Error in the output of my flex file

declare.h

declare.h

# 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

flex文件是

%{
# include "declare.h"
# include "parse.tab.h"

/*gi=1,it's input;gi=8,it's fanout;otherwise,it's gate*/
static int gi=-1;
static int inum=0;



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;

extern int yylval;
%}

%start A B C D E

DIGITS [0-9]+
BLANK [ \t\n\r\f\v\b]+
ALPHA [a-z]+

%%

"*".*\n     {BEGIN A; return(COMMENT);}

<A>{DIGITS}        {yylval=atoi(yytext); BEGIN B; return(NUM);}
<B>{DIGITS}{ALPHA} {yylval=atoi(yytext); BEGIN C; return(GNAME);}
<C>{DIGITS}        {yylval=atoi(yytext); BEGIN D; return(OPNUM);}
<C>{DIGITS}{ALPHA} {yylval=atoi(yytext); BEGIN A; return(FR);}
<D>{DIGITS}        {inum=atoi(yytext);
                    yylval=inum;
                    if(gi==1)
                    {BEGIN A;}
                    if(gi!=1)
                    {BEGIN E;}
                    return(IPNUM);
                   }

<E>{DIGITS}        {inum--;
                    yylval=atoi(yytext);
                    if(inum<0)
                    {BEGIN B; return(NUM);}
                    else
                    {BEGIN E; return(ILIST);} 
                   }


{ALPHA} {yylval=lookup(yytext);
         return(GTYPE);      
         }

">sa"[0-1] {yylval=atoi(&yytext[yyleng-1]);return(FAULT);}

{BLANK}    ;

.          ;



%%
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);
}

野牛档案中的正确规则如下

The right rules in bison file are as follows

parto: 
     | parto COMMENT
     | parto parti
     ;

parti: NUM
       {...}

       GNAME
       {...}


       GTYPE
       {...}

      | parti partii 
      | parti partiii
      ;

partii:OPNUM
       {...}

       IPNUM
       {...}

       partiv

       partv
       ;

partiii: FR
        {...}

         partiv
         ;

partiv: 
      | partiv FAULT
      {...}

      ;

partv:
     | partv ILIST
     {...}

     ;

推荐答案

将主要评论转化为答案.

Transferring the key comments into an answer.

第一版代码有两个问题.在扫描器代码中,有如下几行:

The first edition of the code had a couple of problems. In the scanner code, there were lines like this:

<A>{DIGITS}        { yylval=atoi(yytext); return(NUM); BEGIN B; }

您应该从返回后出现的BEGIN操作中收到有关无法访问代码的警告. BEGIN操作必须执行.它们没有被执行,因此您不会切换到开始状态.

You should be getting warnings about unreachable code from the BEGIN operations appearing after return. The BEGIN operations have to be executed. They aren't being executed, so you're not switching into your start states.

迈克尔评论:

没有警告.我已经按照您说的进行了修改,并在问题中编辑了我的代码.现在,我在开始之后放回去.不过,第1行中<.>附近的语法错误".

There is no warning. I've modified it as you say and edit my codes in the question. Now I put return after BEGIN. Still, "syntax error in line 1 near <�>".

这可能意味着您在编译C代码时没有得到足够的警告.假设您使用的是GCC,请将-Wall添加到启动程序的编译选项中.警告也有可能也需要优化.

This probably means you aren't compiling the C code with enough warnings. Assuming you're using GCC, add -Wall to the compilation options for starters. There's a chance the warning requires optimization too.

您是否在令牌返回时(在Flex扫描仪中)打印了令牌?您是否已使用-DYYDEBUG编译了Bison语法?您还需要打开调试:main()程序中的yydebug = 1;.当您期望它们时,您可能没有得到期望的令牌.我还没有尝试编译此代码. (以我的经验)跟踪令牌是使语法正常工作的关键.否则,您会失明.

Have you printed the tokens as they're returned (in the Flex scanner)? Have you compiled the Bison grammar with -DYYDEBUG? You also need to turn the debug on: yydebug = 1; in the main() program. You're probably not getting the tokens you expect when you expect them. I've not tried compiling this code yet. Tracking the tokens is key (in my experience) to getting grammars to work. Otherwise, you're running blind.

另一个问题(密切相关)是您需要从语法生成FAULT等的符号名(bison -d grammar.y生成grammar.tab.h).例如,您会发现为COMMENT分配了值258.但是,您的扫描仪将完全返回其他数字,因为它们位于declare.h中.您必须解决此不匹配问题.一种选择是在扫描仪中#include "grammar.tab.h".这或多或少是正常的.

The other problem (closely related) is that you need to generate the symbolic names for FAULT etc from the grammar (bison -d grammar.y generates grammar.tab.h). You'll find that COMMENT is assigned the value 258, for example. Your scanner, though, is returning other numbers altogether because they're in declare.h. You'll have to fix this mismatch. One option is to #include "grammar.tab.h" in your scanner; this is more or less normal.

回想起来,我认为这可能是最重要的发现.解决此问题后,一切似乎恢复为正常的C调试.

In retrospect, I think this is probably the most important observation; things seemed to revert to normal C debugging after this was resolved.

(人们通常包括"grammar.h",并且仅在"grammar.tab.h"的内容发生更改时才更新"grammar.h",因此您不必一直重新编译扫描程序).

(People often include 'grammar.h' and only update 'grammar.h' if the content of 'grammar.tab.h' changes, so you don't recompile the scanner all the time).

这样做的意义在于,随着语法实现的发展,与语法相关联的动作始终在变化,而语法使用的标记集往往会相当稳定.因此,如果需要花费足够的时间来担心,则可以创建文件grammar.h,该文件是grammar.tab.h的副本,但是仅当grammar.tab.h的内容更改时才更新grammar.h.

The significance of this is that the set of tokens used by a grammar tends to be fairly stable while the actions associated with the rules change all the time as the implementation of the grammar evolves. So, if it takes enough time to be worth worrying about, you can create file grammar.h that is a copy of grammar.tab.h, but only update grammar.h when the content of grammar.tab.h changes.

cmp -s grammar.tab.h grammar.h || cp grammar.tab.h grammar.h

您会将其包含在makefile规则中,该规则将该语法转换为C文件(或目标文件).

You'd include this in the makefile rule that converts that grammar into a C file (or an object file).

如果扫描仪足够小,并且您的机器足够快,那么不打扰这种简化可能会更简单;在拥有少量MiB内存的50 MHz机器的今天,它比在2 GHz以上运行多核且具有少量GiB RAM的多核更重要.

If the scanner is small enough and your machine fast enough, it may be simpler not to bother with this refinement; it mattered more in the days of 50 MHz machines with a few MiB of RAM than it does in these days of multiple cores running at 2+ GHz with a few GiB of RAM.

这篇关于为什么我的野牛规则不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆