Compiler errors 使用flex和yacc的整数令牌

Compiler errors 使用flex和yacc的整数令牌,compiler-errors,bison,flex-lexer,yacc,Compiler Errors,Bison,Flex Lexer,Yacc,我正在尝试使用flex和yacc识别一个整数标记。这是我的整数flex文件语法 %{ #include "main.h" #include "y.tab.h" #include <stdlib.h> #include <string.h> #define YYSTYPE char * void yyerror(char *); %} code "code" special ":" turn "turn" sen

我正在尝试使用flex和yacc识别一个整数标记。这是我的整数flex文件语法

%{
#include "main.h"
#include "y.tab.h"
#include <stdlib.h>
#include <string.h>
#define YYSTYPE char *
void yyerror(char *);
%}

code            "code"
special         ":"
turn            "turn"
send            "send"
on              "on"
pin             "pin"
for             "for"
num             "[0-9]+"

%%
{code}          {return CODE;}
{special}       {return SPECIAL; }
{turn}          {return OPRTURN;}
{send}          {return OPRSEND;}
{on}            {return OPRON;}
{pin}           {return PORT;}
{for}           {return FOR;}
{num}           { yylval.iValue = atoi(yytext); return INTEGER;}
%%

int yywrap(void) {
return 1;
}
%{
#包括“main.h”
#包括“y.tab.h”
#包括
#包括
#定义YYSTYPE字符*
无效错误(字符*);
%}
代码“代码”
特别“:”
转“转”
发送“发送”
关于“开”
别针“别针”
为了“为了”
数字“[0-9]+”
%%
{code}{返回代码;}
{special}{return special;}
{turn}{return-OPRTURN;}
{send}{return OPRSEND;}
{on}{return OPRON;}
{pin}{返回端口;}
{for}{return for;}
{num}{yylval.iValue=atoi(yytext);返回整数;}
%%
int-yywrap(无效){
返回1;
}
在yacc文件中

%{
    #include "main.h"
    #include <stdio.h>
    #include <stdarg.h>
    #include <stdlib.h>
//    char *name[10];

void  startCompiler();

/* prototypes */
nodeType *enm(char* c);
nodeType *opr(int oper,int nops,...);
nodeType *id(int i);
nodeType *con(int value);
nodeType *cmd(char *c);

void freeNode(nodeType *p);
int ex(nodeType *p);
int yylex(void);

void yyerror(char *s);
//int sym[26];                    /* symbol table */
%}

%union {
    int iValue;                 /* integer value */
    char sIndex;                /* symbol table index */
    char *str;                /* symbol table index */
    nodeType *nPtr;             /* node pointer */
};

%token <iValue> INTEGER

%token CODE SPECIAL OPRTURN OPRSEND OPRON PORT FOR
%type <nPtr> stmt stmt_list oper operation duration pin location



%%

input   : function { exit(0);}
        ;

input   : function { exit(0);}
        ;

function : function stmt  { ex($2); freeNode($2);}
        |
        ;

stmt    : 

        | PORT location          { printf("Port taken"); }
        ;


location : INTEGER  { printf("Number is %d",$1); }

                ;
%{
#包括“main.h”
#包括
#包括
#包括
//字符*名称[10];
void startCompiler();
/*原型*/
节点类型*enm(字符*c);
节点类型*opr(int-oper,int-nops,…);
节点类型*id(inti);
节点类型*con(int值);
nodeType*cmd(char*c);
void freeNode(节点类型*p);
int-ex(节点类型*p);
int yylex(无效);
无效错误(字符*s);
//int sym[26];/*符号表*/
%}
%联合{
int iValue;/*整数值*/
char sIndex;/*符号表索引*/
char*str;/*符号表索引*/
节点类型*nPtr;/*节点指针*/
};
%令牌整数
%令牌代码特殊操作发送操作端口
%类型stmt stmt_列表操作持续时间引脚位置
%%
输入:函数{exit(0);}
;
输入:函数{exit(0);}
;
函数:函数stmt{ex($2);freeNode($2);}
|
;
stmt:
|端口位置{printf(“接收端口”);}
;
位置:整数{printf(“数字是%d,$1);}
;
但是当我执行程序时,它不识别数字

输入是

引脚4

输出

四,

输出应该是

端口号是4

我错过了什么? 所有其他代币都正常工作


提前感谢。

您得到的输出是来自
yyrerror
函数的输出,默认情况下,该函数只将无效令牌打印到stderr

结果是“pin”被正确识别,但“4”却没有被正确识别。因此,语句规则没有生成任何输出,因为它仍在等待整数。因此,唯一的输出是
yyerror

它无法识别4的原因是您将数字的正则表达式作为字符串文字引用。所以它在寻找文本字符串“[0-9]+”。删除引号以将其解释为正则表达式


PS:您还需要添加一个跳过空格的规则,否则您必须输入不带空格的
pin4

请发布足够的代码来运行代码并重现问题。还要更清楚地了解输入、实际输出和预期输出。输入端是否为“引脚4”,输出端是否为“4”?而预期的输出将是“Number take”,没有其他内容?@sepp2k我已经添加了完整的代码。实际上,我需要识别的是我的代码获得
pin4
并识别pin和number。然后我可以在我的C代码中使用这个数字进行进一步处理。我仍然不清楚您的输入、实际输出和预期输出是什么。我不明白输出怎么会是
4
,在你的代码中没有任何东西只打印一个数字而没有额外的文本。@sepp2k这里我想做的是,当输入作为
pin 4
时,我的语法应该识别它,
端口号是4
。这只是一个大代码的片段,我试图将一个伪代码转换成arduino代码。好的,会发生什么?因为看看代码,这正是我所期望的。是的,那是我的错误。非常感谢@sepp2k