0
の値を持つI bison
仕様で、以下の生産を持っている:は/バイソンは0
op : '+' { printf("%d %d %c\n", $1, '+', '+'); }
ときI入力、私は次の出力を得る+
:
0 43 +
を誰かが$1
の値が0である理由を説明できますか、それは43ではありませんか?私は何が欠けていますか?
EDIT
そこにはフレックスファイルがありませんが、私はbison
文法を提供することができます:
%{
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int yylex();
int yyerror();
%}
%token NUMBER
%%
lexp : NUMBER
| '(' op lexp-seq ')'
;
op : '+' { printf("%d %d %c\n", $1, '+', '+'); }
| '-' { printf("%d %d %c\n", $1, '-', '-'); }
| '*' { printf("%d %d %c\n", $1, '*', '*'); }
;
lexp-seq : lexp-seq lexp
| lexp
;
%%
int main(int argc, char** argv) {
if (2 == argc && (0 == strcmp("-g", argv[1])))
yydebug = 1;
return yyparse();
}
int yylex() {
int c;
/* eliminate blanks*/
while((c = getchar()) == ' ');
if (isdigit(c)) {
ungetc(c, stdin);
scanf("%d", &yylval);
return (NUMBER);
}
/* makes the parse stop */
if (c == '\n') return 0;
return (c);
}
int yyerror(char * s) {
fprintf(stderr, "%s\n", s);
return 0;
} /* allows for printing of an error message */
flexファイルを表示する必要があります。このコードは値をbisonに返すためです。 –
@ BrianTompsett-汤莱恩私はちょうど編集をしました。多分それは助けることができます。 – flashburn