1

我正在尝试从文本文件及其结果中显示整个算术表达式,我尝试使用文件处理选项但它不起作用。

亚克:

%{
#include <stdio.h>
#include <string.h>
#define YYSTYPE int    /* the attribute type for Yacc's stack */ 
extern int yylval;     /* defined by lex, holds attrib of cur token */
extern char yytext[]; /* defined by lex and holds most recent token */
extern FILE * yyin;    /* defined by lex; lex reads from this file   */
%}

%token  NUM

%%

Calc  : Expr               {printf(" = %d\n",$1);} 
  | Calc Expr          {printf(" = %d\n",$2);}
  | Calc error         {yyerror("\n");}
  ;
Expr  : Term               { $$ = $1;         }
  | Expr '+' Term      { $$ = $1 + $3;    }
  | Expr '-' Term      { $$ = $1 - $3;    }
  ;
Term  : Fact               { $$ = $1;         }
  | Term '*' Fact      { $$ = $1 * $3;    }
  | Term '/' Fact      { if($3==0){ 
                yyerror("Divide by Zero Encountered.");
            break;}
               else
                $$ = $1 / $3;    
                   }
  ;
Fact  : Prim               { $$ = $1;        }
  | '-' Prim           { $$ = -$2;       }
  ;      
Prim  : '(' Expr ')'       { $$ = $2;        }
  | Id                 { $$ = $1;        }
  ;
Id    :NUM                 { $$ = yylval;    }
  ;
%%

void yyerror(char *mesg); /* this one is required by YACC */

main(int argc, char* *argv){
char ch,c;
FILE *f;    
if(argc != 2) {printf("useage:  calc filename \n"); exit(1);}
if( !(yyin = fopen(argv[1],"r")) ){ 
       printf("cannot open file\n");exit(1);
 }
/*  
 f=fopen(argv[1],"r");
 if(f!=NULL){
char line[1000];
while(fgets(line,sizeof(line),f)!=NULL)
    {
            fprintf(stdout,"%s",line);
        yyparse();
    }

}
*/
yyparse();
}

void yyerror(char *mesg){
printf("\n%s", mesg);
}

莱克斯

%{
#include <stdio.h>
#include "y.tab.h"  
int yylval; /*declared extern by yacc code. used to pass info to yacc*/  
%}

letter  [A-Za-z]
digit   ([0-9])*
op      "+"|"*"|"("|")"|"/"|"-"
ws      [ \t\n\r]+$
other   .

%%

{ws}    {  /*Nothing*/ }
{digit} {  yylval = atoi(yytext); return NUM;}
{op}    {  return yytext[0];}
{other} {  printf("bad%cbad%d\n",*yytext,*yytext); return  '?'; }

%%

我的文本文件包含这两个表达式:

4+3-2*(-7)
9/3-2*(-5)

我想要输出为:

4+3-2*(-7)=21
9/3-2*(-5)=13

但输出是:

=21
=13

因为解析器将一次完成所有计算,所以这个(注释代码)是不合法的。因此,需要在 Calc 块中显示将输入表达式传递给语法和打印。我无法在谷歌上找到任何有关在语法中显示输入的相关内容。提前感谢您的意见和建议。

4

2 回答 2

0

你不想在语法中这样做。太复杂了,太受制于语法可能做的任何重新排列。您可以考虑在词法分析器中执行此操作,即yytext在您返回它之前打印除空白操作之外的每个操作,但我会通过覆盖lex(1)的输入函数来回显读取的所有输入。

注意您应该使用flex(1),而不是lex(1),并注意如果您更改,则yyyext不再是 achar[]并变成 a char *

我在您之前的问题中没有提到它,但是这条规则:

{other} {  printf("bad%cbad%d\n",*yytext,*yytext); return  '?'; }

最好写成:

{other} {  return yytext[0]; }

这样解析器将看到它并产生语法错误,因此您不必自己打印任何内容。这种技术还可以让您摆脱单个特殊字符的规则+,-=*,/,(,),因为解析器将通过yytext[0].

于 2017-02-14T03:28:21.330 回答
-1

最后我得到了它 :

YACC

%{
#include <stdio.h>
#include <string.h>
#define YYSTYPE int    /* the attribute type for Yacc's stack */ 
extern int yylval; /* defined by lex, holds attrib of cur token */ 
extern char yytext[]; /* defined by lex and holds most recent token */ 
extern FILE *     yyin; /* defined by lex; lex reads from this 
file */ %}

%token  NUM

%%

Calc  : Expr               {printf(" = %d\n",$1);} 
  | Calc Expr          {printf(" = %d\n",$2);}
  | error              {yyerror("Bad Expression\n");}
  ;

Expr  : Term               { $$ = $1;         }
  | Expr Add Term      { $$ = $1 + $3;    }
  | Expr Sub Term      { $$ = $1 - $3;    }
  ;
Term  : Fact               { $$ = $1;         }
  | Term Mul Fact      { $$ = $1 * $3;    }
  | Term Div Fact      { if($3==0){ 
                yyerror("Divide by Zero Encountered.");
            break;}
               else
                $$ = $1 / $3;  
                   }
  ;
Fact  : Prim               { $$ = $1;        }
  | '-' Prim           { $$ = -$2;       }
  ;      
Prim  : LP Expr RP         { $$ = $2;        }
  | Id                 { $$ = $1;        }
  ;
Id    :NUM                 { $$ = yylval;   printf("%d",yylval); }
  ; 
Add   : '+' {printf("+");}
Sub   : '-' {printf("-");}
Mul   : '*' {printf("*");}
Div   : '/' {printf("/");}
LP    : '(' {printf("(");}
RP    : ')' {printf(")");}

%%

void yyerror(char *mesg); /* this one is required by YACC */

main(int argc, char* *argv){
char ch,c;
FILE *f;    
if(argc != 2) {printf("useage:  calc filename \n"); exit(1);}
if( !(yyin = fopen(argv[1],"r")) ){ 
       printf("cannot open file\n");exit(1);
 }
yyparse();
}

void yyerror(char *mesg){
printf("%s ", mesg);
}

感谢 EJP 和 EMACS 用户的回复。

于 2017-02-17T00:24:30.267 回答