parser.ypp
来自「Lexx Bison programm Lexx Bison Lexx Biso」· YPP 代码 · 共 72 行
YPP
72 行
%{ /* 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 + =
减小字号Ctrl + -
显示快捷键?