📄 parser.ypp
字号:
%{ /* bison spec */#include <stdio.h>#include <iostream>#include <string>#include <map> double variableTable[26];class symbol{ std::string id; double value; double (*action)(double);};std::map<std::string, symbol> symbolTable;int yyerror(char *s);extern int yylex();extern int yyget_lineno();%}%union { double doubleValue; int variableNumber;}%token IF ELSE ELSEIF ENDIF opSet opEqual %token WHILE ENDWHILE %token <variableNumber> ID %token <doubleValue> NUMBER%left "=="%left '+' '-'%left '*' '/'%type <doubleValue> expr%%program : program block | /* empty */ { printf("program:\n"); }block : instructions | if_block | while_block;if_block: IF '(' expr ')' block else_block ENDIF { printf("if\n"); }else_block: /*empty*/ | ELSE block | ELSEIF '(' expr ')' block;while_block: WHILE '(' expr ')' block ENDWHILE { printf("while\n"); }expr : ID { $$ = variableTable[$1]; printf("variable\n"); }| NUMBER { $$ = $1; }| expr '+' expr { $$ = $1 + $3; }| expr '-' expr { $$ = $1 - $3; }| expr '*' expr { $$ = $1 * $3; }| expr '/' expr { if ($3 == 0) yyerror("divide by zero"); else $$ = $1 / $3; }| expr opEqual expr { $$ = ($1 == $3); printf("equal\n"); }instructions: instruction | instructions instruction;instruction: ID opSet expr ';' { variableTable[$1]=$3; }%% /* skip */int yyerror(char *s) { printf("error: %s, line %d\n",s, yyget_lineno());}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -