私は構文解析が初めてです。バイソンのシンプルな電卓
Parser.y::私は入力文字列10 - 3 * 2 + 6
が/解析された演算子の優先順位に付着して処理されるかを理解するために苦労しています
%{
#include <stdio.h>
%}
/* declare tokens */
%token NUMBER
%token ADD SUB MUL DIV ABS
%token EOL
%%
calclist: /* nothing */
| calclist exp EOL { printf("= %d\n", $1); }
;
exp: factor
| exp ADD factor { $$ = $1 + $3; }
| exp SUB factor { $$ = $1 - $3; }
;
factor: term
| factor MUL term { $$ = $1 * $3; }
| factor DIV term { $$ = $1/$3; }
;
term: NUMBER
| ABS term { $$ = $2 >= 0? $2 : - $2; }
;
%%
main(int argc, char **argv)
{ yyparse();
}
yyerror(char *s)
{ fprintf(stderr, "error: %s\n", s);
}
バイソンでのパーサーのコードスニペットを以下に示します。誰でも解析メカニズムを段階的に記述できますか?例えば、
Step1: 10 is read and token NUMBER is returned
Step2: etc....
助けてください。
ありがとうございました。
質問から - トークン** 10 **が入力から読み込まれ、 'NUMBER - > term - > factor'に縮小されると、なぜそれがさらに' exp'に還元されないのでしょうか?ルール 'exp:factor'(ルール7)。代わりに、次のトークン(「** - **」)を読み取るようになりますか? –
@Raj:それはルール7を使って減らします。それはトレースではっきりしています。 – rici