1
A
답변
0
참고 : 당신이 그것에 대해 검색해야 할 질문을 게시하기 전에, 나는거야 게시물을 나는 다음 대답은 내가 Google 검색에서 얻은 위의 링크에서 얻을 수있다.
다음 코드는 YACC 및 LEX를 사용하여 계산기 프로그램을 구현하는 데 사용됩니다.
cal.l
DIGIT [0-9]+\.?|[0-9]*\.[0-9]+
%option noyywrap
%%
[ ]
{DIGIT} { yylval=atof(yytext); return NUM;}
\n|. {return yytext[0];}
cal.y
%{
#include<ctype.h>
#include<stdio.h>
#define YYSTYPE double
%}
%token NUM
%left ‘+’ ‘-‘
%left ‘*’ ‘/’
%right UMINUS
%%
S : S E ‘\n’ { printf(“Answer: %g \nEnter:\n”, $2); }
| S ‘\n’
|
| error ‘\n’ { yyerror(“Error: Enter once more…\n”);yyerrok; }
;
E : E ‘+’ E { $$ = $1 + $3; }
| E’-‘E { $$=$1-$3; }
| E’*’E { $$=$1*$3; }
| E’/’E { $$=$1/$3; }
| ‘(‘E’)’ { $$=$2; }
| ‘-‘E %prec UMINUS { $$= -$2; }
| NUM
;
%%
#include “lex.yy.c”
int main()
{
printf(“Enter the expression: “);
yyparse();
}
yyerror (char * s)
{
printf (“% s \n”, s);
exit (1);
}
당신이 – Billa
내가 한 @Billa,하지만 내 평판 미만 15에 대한 검색해야 할 질문을 게시하기 전에, 그래서 내 upvote 표시되지 않습니다! 고마워, btw! –
도움이되었다고 기쁘게 생각합니다. – Billa