【问题标题】:How to run a yacc file on windows如何在 Windows 上运行 yacc 文件
【发布时间】:2022-01-15 06:23:48
【问题描述】:

我从未见过 YACC 文件,这是我第一次遇到 YACC 程序。我的老师给了我这个程序,用于前缀编译器的中缀,但我什至不知道如何运行它。我是编译器的新手,所以请简单解释一下。我试过用谷歌搜索它,但我什么都不懂。

我只想知道我应该如何运行程序我不需要知道每一行的作用,因为我知道程序的一般作用,这就是重点,但我希望自己能看到它运行

文件in.l

/****************************************************/
* Problem Statement :- 
 Assignment To check whether given expression is 
 infix, postfix or prefix.
* Roll No :- 40 Batch :- A2
* BE(Computer)
* Date :- 2/09/2011
********************** in.l ***********************
%{
#include<stdio.h>
#include"y.tab.h"
#include<math.h>
%}
%%
[0-9] {yylval.dval=atoi(yytext);return NUM;}
[t];
n return 0;
. {return yytext[0];}
%%
void yyerror(char * str)
{
 printf("n Invalid Expression...");
}
int main()
{
 printf("n ENter Expression => ");
 yyparse();
 return(0);
}

文件in.y

********************** in.y ***********************
%{
#include<stdio.h>
int yylex(void);
%}
%union
{
 float dval;
}
%token <dval> NUMBER
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
%type <dval> exp
%%
state : exp {printf("n Infix Expression...");}
 ;
exp : NUMBER
 | exp '+' exp {}
 | exp '-' exp {}
 | exp '*' exp {}
 | exp '/' exp {}
 ;
%%

文件pre.y

********************** pre.y ***********************
%{
#include<stdio.h>
int yylex(void);
%}
%union
{
 int dval;
}
%token <dval> NUMBER
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
%type <dval> exp
%%
state : exp {printf("n Prefix Expression...");}
 ;
exp : NUMBER
 | '+' exp exp {}
 | '-' exp exp {}
 | '*' exp exp {}
 | '/' exp exp {}
 ;
%%

文件post.y

********************** post.y ***********************
%{
#include<stdio.h>
int yylex(void);
%}
%union
{
 int dval;
}
%token <dval> NUMBER
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
%type <dval> exp
%%
state : exp {printf("n Postfix Expression...");}
 ;
exp : NUMBER
 | exp exp '+' {}
 | exp exp '-' {}
 | exp exp '*' {}
 | exp exp '/' {}
 ;
%%

构建、运行、输入和输出

********************** Output ***********************
[a40@localhost ~]$ lex in.l
[a40@localhost ~]$ yacc -d in.y
[a40@localhost ~]$ cc lex.yy.c y.tab.c -ll
[a40@localhost ~]$ ./a.out

Enter Expression => 1+7
 Infix Expression...
[a40@localhost ~]$ ./a.out

Enter Expression => 1++7
 Invalid Expression...

[a40@localhost ~]$ lex in.l
[a40@localhost ~]$ yacc -d pre.y
[a40@localhost ~]$ cc lex.yy.c y.tab.c -ll
[a40@localhost ~]$ ./a.out

Enter Expression => 1+7
 Invalid Expression...
[a40@localhost ~]$ ./a.out

Enter Expression => +17
 Prefix Expression...

[a40@localhost ~]$ lex in.l
[a40@localhost ~]$ yacc -d pre.y
[a40@localhost ~]$ cc lex.yy.c y.tab.c -ll
[a40@localhost ~]$ ./a.out

Enter Expression => 12+34*-
 Postfix Expression...
[a40@localhost ~]$ ./a.out

Enter Expression => +17
 Invalid Expression...

【问题讨论】:

  • 您的问题包括 lex 和 yacc 在大引用块底部运行的示例
  • edit您的问题并添加更多详细信息。当您尝试下面显示的构建命令********************** Output *********************** 时是否收到错误消息?如果是,请复制并粘贴命令和相应的错误消息。如果问题是您没有或找不到适用于 Windows 的 lexyacc,请查找 flexbison

标签: c++ c compiler-construction yacc lex


【解决方案1】:

案例 1:只有 lex 程序需要运行和构建和执行

点击 IDE 中的直接执行 CMD 按钮。

通过输入命令 lex .l 编译 Lex 文件

在 CMD 中通过 gcc/cc 命令构建 Lex 文件,例如 gcc lex.yy.c -o

输入 .exe 执行程序

-o参数是可选的,你可以通过gcc lex.yy.c直接构建跳过该参数 然后直接输入 a.exe 执行你的程序

案例 2:Lex 和 Yacc 程序都必须链接并编译 - 执行

点击 IDE 中的 Execute CMD 按钮。

通过输入命令 yacc -dy 编译 Yacc 文件

通过输入命令 lex .l 编译 Lex 文件

在 CMD 中通过 gcc/cc 命令构建 Lex 文件,例如 gcc lex.yy.c y.tab.c -o

输入 .exe 执行程序

-o参数是可选的,你可以通过gcc lex.yy.c y.tab.c直接构建跳过该参数 然后直接输入 a.exe 执行你的程序

【讨论】:

  • 你认为 OP 有什么 IDE,因为他们没有提到一个
猜你喜欢
  • 2011-07-24
  • 1970-01-01
  • 2017-04-14
  • 2017-06-04
  • 1970-01-01
  • 2010-09-28
  • 2011-02-21
  • 2020-05-25
  • 2011-10-07
相关资源
最近更新 更多