yaac.c

来自「C-词法分析」· C语言 代码 · 共 55 行

C
55
字号
%{
/****************************************************************************
myparser.y
ParserWizard generated YACC file.

Date: 2007年11月29日
****************************************************************************/

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

%token NUMBER

%left '+'
%left '*'
%left '-'
%%
command : exp {printf("%d\n",$1);}
		;
exp : exp '+' term {$$ = $1 + $3;}
	| exp '-' term {$$ = $1 - $3;}
	| term {$$ = $1;}
	;

term : term '*' factor { $$ = $1 * $3;}
	 | factor { $$ = $1;}
	 ;

factor : NUMBER { $$ = $1;}
	   | '(' exp ')' { $$ = $2;}
	   ;
%%

int main()
{
	return yyparse();
}

int yylex(void)
{
	int c;
	while(( c = getchar())==' ');
	
	if( isdigit(c) ) {
	ungetc(c,stdin);
	scanf("%d",&yylval);
	return (NUMBER);
	}
	if( c == '\n' )	return 0;
	
	return (c);
	}
	

⌨️ 快捷键说明

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