gpt4 book ai didi

syntax-error - 为什么我的 Bison 规则不起作用

转载 作者:行者123 更新时间:2023-12-03 08:12:09 30 4
gpt4 key购买 nike

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

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

*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

我的伸缩文件如下,它是正确的。您可以在此处找到有关扫描仪工作方式的一些信息。
Error in the output of my flex 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

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

野牛文件中的正确规则如下
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
{...}

;

最佳答案

将关键评论转换为答案。

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

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

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.



迈克尔评论:

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添加到入门的编译选项中。警告也有可能需要优化。

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.

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调试。

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



这样做的意义在于,语法使用的 token 集趋于相当稳定,而与规则关联的 Action 会随着语法实现的发展而一直变化。因此,如果需要花费足够的时间来担心,可以创建文件 grammar.h,它是 grammar.tab.h的副本,但是只有在 grammar.h的内容更改时才更新 grammar.tab.h
cmp -s grammar.tab.h grammar.h || cp grammar.tab.h grammar.h

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

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

关于syntax-error - 为什么我的 Bison 规则不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15526259/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com