myparser.y

来自「自动生成工具lex yacc构造的小计算器」· Y 代码 · 共 70 行

Y
70
字号
%{
/****************************************************************************
myparser.y
ParserWizard generated YACC file.

Date: 2007年12月5日
****************************************************************************/

#include <stdio.h>
#include <ctype.h>

#include "mylexer.h"
%}

/////////////////////////////////////////////////////////////////////////////
// declarations section

%token NUMBER
%token LEFT
%token RIGHT
%token PLUS
%token MINUS
%token TIMES

// attribute type
%include {
#ifndef YYSTYPE
#define YYSTYPE int
#endif
}

// place any declarations here

%%

/////////////////////////////////////////////////////////////////////////////
// rules section





// place your YACC rules here (there must be at least one)


command : exp  {printf("%d\n", $1); }
	;
exp     : exp PLUS term  {$$ = $1 + $3;}
        | exp MINUS term  {$$ = $1 - $3; }
        | term {$$ = $1; }
        ;
term    : term TIMES factor {$$ = $1 * $3; }
        | factor {$$ = $1; }
        ;
factor  : NUMBER {$$ = $1; }
        | LEFT exp RIGHT {$$ = $2; }
        ; 


%%

/////////////////////////////////////////////////////////////////////////////
// programs section

int main(void)
{
	return yyparse();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?