我一直在尝试将按位运算符添加到给定的前缀/中缀/后缀计算器,但由于某种原因,我无法使用 yacc 识别运算符“<<”“>>”“**”。我四处寻找有类似问题的其他人,尤其是这个问题yacc/bison tokens error。'>>>' 和 '>>' 都分配了编号 62,但还没有找到可行的解决方案。
我的 lex 文件
%{
#include "zcalc.h"
#include <math.h>
int ch;
int flag = 0;
#define NAME 257
#define SEMISYM 268
#define COMMASYM 269
#define LPARSYM 270
#define RPARSYM 271
#define EQSYM 272
#define PLUSSYM 273
#define MULTSYM 274
#define ASGNSYM 275
#define MINUSSYM 276
#define NUMBER 277
#define TILDE 278
/*New stuff I added*/
#define BITAND 279
#define BITOR 280
#define BITXOR 281
/*#define BITNOT 282*/
#define LSHIFT 283
#define RSHIFT 284
#define POWER 285
void yyerror( char *mesg ); /* yacc error checker */
/* definitions for lex analyzer */
letter [A-Za-z]
digit [0-9]+
ident {letter}({letter}|{digit})*
ws [ \t\n]+
other .
%%
{ws} ; /*---- Tokens and Actions---- */
/*New Stuff*/
"&" return BITAND;
"|" return BITOR;
"^" return BITXOR;
"<<" return LSHIFT;
">>" return RSHIFT;
"**" return POWER;
"//".* ;
";" return SEMISYM;
"," return COMMASYM;
"(" return LPARSYM;
")" return RPARSYM;
"==" return EQSYM;
"+" return PLUSSYM;
"*" return MULTSYM;
"=" return ASGNSYM;
"-" return MINUSSYM;
"~" return TILDE;
/*New Stuff
"&" return BITAND;
"|" return BITOR;
"^" return BITXOR;
"<<" return LSHIFT;
">>" return RSHIFT;
"**" return POWER;*/
{ident} {
return NAME;
}
{digit} {
return NUMBER;
}
"$" { return 0; }
{other} ; /* ignore other stuff */
%%
void yyerror( char *mesg ); /* yacc error checker */
/* yacc error function */
void yyerror( char *mesg ) {
flag = 1;
printf("%s \n" , mesg);
}
int main() {
printf("Lex \t\tToken\t\t\n"); /* header on columns */
printf("----------------------------\n");
do
{
ch = yylex();
if (ch == SEMISYM)
printf("%s\t\tSEMICOLON ", yytext);
else if (ch == COMMASYM)
printf("%s\t\tCOMMA ", yytext);
else if (ch == LPARSYM)
printf("%s\t\tL_PARENTHESIS ", yytext);
else if (ch == RPARSYM)
printf("%s\t\tR_PARENTHESIS ", yytext);
else if (ch == EQSYM)
printf("%s\t\tEQ_OP ", yytext);
else if (ch == PLUSSYM)
printf("%s\t\tPLUS_OP ", yytext);
else if (ch == MULTSYM)
printf("%s\t\tMULT_OP ", yytext);
else if (ch == ASGNSYM)
printf("%s\t\tASSIGNMENT_STMT ", yytext);
else if (ch == MINUSSYM)
printf("%s\t\tMINUS_OP ", yytext);
else if (ch == NUMBER)
printf("%s\t\tNUMBER ", yytext);
else if (ch == NAME)
printf("%s\t\tNAME\t\t", yytext);
else if (ch == TILDE)
printf("%s\t\tTILDE\t\t", yytext);
else
printf("%c ",ch);
printf("\n"); /* end check token read */
}
while(ch != 0); /* read until end of file */
return 0;
}
int yywrap() {
return 1;
}
%}
还有我的 yacc 文件
%{
#include "zcalc.h"
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
int flag = 0;
void yyerror( char *mesg ); /* yacc error checker */
%}
%union {
double dval;
struct symtab *symp;
}
%token <symp> NAME
%token <dval> NUMBER
// %token LSHIFT
// %token RSHIFT
%token POWER
%left '-' '+'
//%left "**"
%left '*' '/'
//%left LSHIFT RSHIFT
//%left POWER
%type <dval> expression
%%
statement_list: statement '\n'
| statement_list statement '\n'
statement: NAME '=' expression { $1->value = $3; }
| expression { printf("= %g\n", $1); }
expression: '+' expression expression { $$ = $2 + $3; }
| '-' expression expression { $$ = $2 - $3; }
| POWER expression expression { $$ = $3; }
| '*' expression expression { $$ = $2 * $3; }
| '/' expression expression { $$ = $2 / $3; }
| '&' expression expression { $$ = (int)$2 & (int)$3; }
| '|' expression expression { $$ = (int)$2 | (int)$3; }
| '^' expression expression { $$ = (int)$2 ^ (int)$3; }
| '<' '<' expression expression { $$ = (int)$3 << (int)$4; }
| '>' '>' expression expression { $$ = (int)$3 >> (int)$4; }
//| "**" expression expression { $$ = pow($2, $3); }
| '~' expression { $$ = ~ (int)$2; }
| '(' expression ')' { $$ = $2; }
| NUMBER
| NAME { $$ = $1->value; }
%%
struct symtab * symlook( char *s ) {
char *p;
struct symtab *sp;
for(sp = symtab; sp < &symtab[NSYMS]; sp++) {
/* is it already here? */
if (sp->name && !strcmp(sp->name, s))
return sp;
/* is it free */
if (!sp->name) {
sp->name = strdup(s);
return sp;
}
/* otherwise continue to the next */
}
yyerror("Too many symbols...\n");
exit(1);
}
void addfunc( char *name, double (*func)() ) {
struct symtab *sp = symlook(name);
sp->funcptr = func;
}
/* yacc error function */
void yyerror( char *mesg ) {
flag = 1;
printf("%s \n" , mesg);
}
int main() {
yyparse();
return 0;
}
我在标题中摆弄了定义和规则的位置,但这似乎不起作用。我可以使用 '<' '<' 让“<<”和“>>”工作,但它会抛出参数计数,并且看起来更像是一种解决方法而不是正确的解决方案。谢谢您的帮助!