- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试运行我在网上找到的计算器示例。但是每次运行我的 gcc 命令时都会显示此错误。这是我运行的命令:
flex -l calc3.l
yacc -vd calc3.y
gcc y.tab.c -lm -ll
/tmp/ccPOq58f.o : In function 'yyparse':
y.tab.c: undefined reference to 'yylex'
collect2: error: ld returned 1 exit status
%{
#include <stdlib.h>
#include "calc3.h"
#include "y.tab.h"
void yyerror(char *);
%}
%%
[a-z] {
yylval.sIndex = *yytext - 'a';
return VARIABLE;
}
0 {
yylval.iValue = atoi(yytext);
return INTEGER;
}
[1-9][0-9]* {
yylval.iValue = atoi(yytext);
return INTEGER;
}
[-()<>=+*/;{}.] {
return *yytext;
}
">=" return GE;
"<=" return LE;
"==" return EQ;
"!=" return NE;
"while" return WHILE;
"if" return IF;
"else" return ELSE;
"print" return PRINT;
[ \t\n]+ ; /* ignore whitespace */
. yyerror("Unknown character");
%%
int yywrap(void) {
return 1;
}
typedef enum { typeCon, typeId, typeOpr } nodeEnum;
/* constants */
typedef struct {
int value; /* value of constant */
} conNodeType;
/* identifiers */
typedef struct {
int i; /* subscript to sym array */
} idNodeType;
/* operators */
typedef struct {
int oper; /* operator */
int nops; /* number of operands */
struct nodeTypeTag **op; /* operands */
} oprNodeType;
typedef struct nodeTypeTag {
nodeEnum type; /* type of node */
union {
conNodeType con; /* constants */
idNodeType id; /* identifiers */
oprNodeType opr; /* operators */
};
} nodeType;
extern int sym[26];
%{
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "calc3.h"
/* prototypes */
nodeType *opr(int oper, int nops, ...);
nodeType *id(int i);
nodeType *con(int value);
void freeNode(nodeType *p);
int ex(nodeType *p);
int yylex(void);
void yyerror(char *s);
int sym[26]; /* symbol table */
%}
%union {
int iValue; /* integer value */
char sIndex; /* symbol table index */
nodeType *nPtr; /* node pointer */
};
%token <iValue> INTEGER
%token <sIndex> VARIABLE
%token WHILE IF PRINT
%nonassoc IFX
%nonassoc ELSE
%left GE LE EQ NE '>' '<'
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
%type <nPtr> stmt expr stmt_list
%%
program:
function { exit(0); }
;
function:
function stmt { ex($2); freeNode($2); }
| /* NULL */
;
stmt:
';' { $$ = opr(';', 2, NULL, NULL); }
| expr ';' { $$ = $1; }
| PRINT expr ';' { $$ = opr(PRINT, 1, $2); }
| VARIABLE '=' expr ';' { $$ = opr('=', 2, id($1), $3); }
| WHILE '(' expr ')' stmt { $$ = opr(WHILE, 2, $3, $5); }
| IF '(' expr ')' stmt %prec IFX { $$ = opr(IF, 2, $3, $5); }
| IF '(' expr ')' stmt ELSE stmt { $$ = opr(IF, 3, $3, $5, $7); }
| '{' stmt_list '}' { $$ = $2; }
;
stmt_list:
stmt { $$ = $1; }
| stmt_list stmt { $$ = opr(';', 2, $1, $2); }
;
expr:
INTEGER { $$ = con($1); }
| VARIABLE { $$ = id($1); }
| '-' expr %prec UMINUS { $$ = opr(UMINUS, 1, $2); }
| expr '+' expr { $$ = opr('+', 2, $1, $3); }
| expr '-' expr { $$ = opr('-', 2, $1, $3); }
| expr '*' expr { $$ = opr('*', 2, $1, $3); }
| expr '/' expr { $$ = opr('/', 2, $1, $3); }
| expr '<' expr { $$ = opr('<', 2, $1, $3); }
| expr '>' expr { $$ = opr('>', 2, $1, $3); }
| expr GE expr { $$ = opr(GE, 2, $1, $3); }
| expr LE expr { $$ = opr(LE, 2, $1, $3); }
| expr NE expr { $$ = opr(NE, 2, $1, $3); }
| expr EQ expr { $$ = opr(EQ, 2, $1, $3); }
| '(' expr ')' { $$ = $2; }
;
%%
nodeType *con(int value) {
nodeType *p;
/* allocate node */
if ((p = malloc(sizeof(nodeType))) == NULL)
yyerror("out of memory");
/* copy information */
p->type = typeCon;
p->con.value = value;
return p;
}
nodeType *id(int i) {
nodeType *p;
/* allocate node */
if ((p = malloc(sizeof(nodeType))) == NULL)
yyerror("out of memory");
/* copy information */
p->type = typeId;
p->id.i = i;
return p;
}
nodeType *opr(int oper, int nops, ...) {
va_list ap;
nodeType *p;
int i;
/* allocate node */
if ((p = malloc(sizeof(nodeType))) == NULL)
yyerror("out of memory");
if ((p->opr.op = malloc(nops * sizeof(nodeType *))) == NULL)
yyerror("out of memory");
/* copy information */
p->type = typeOpr;
p->opr.oper = oper;
p->opr.nops = nops;
va_start(ap, nops);
for (i = 0; i < nops; i++)
p->opr.op[i] = va_arg(ap, nodeType*);
va_end(ap);
return p;
}
void freeNode(nodeType *p) {
int i;
if (!p) return;
if (p->type == typeOpr) {
for (i = 0; i < p->opr.nops; i++)
freeNode(p->opr.op[i]);
free (p->opr.op);
}
free (p);
}
void yyerror(char *s) {
fprintf(stdout, "%s\n", s);
}
int main(void) {
yyparse();
return 0;
}
最佳答案
如果你只是使用
flex calc3.l
然后 flex 生成一个名为
lex.yy.c
的扫描仪. (我删除了原始问题中使用的
-l
选项。
-l
使 flex 与原始 lex 实用程序的某些方面更加兼容,除了编译古老的 lex 扫描程序外,它没有任何用处。)
yacc -vd calc3.y
野牛将生成名为
y.tab.c
的文件和
y.tab.h
.和
gcc y.tab.c -lm -ll
将生成一个名为
a.out
的文件.
-o
指定输出名称文件的命令行标志。
flex calc3.l
yacc -vd calc3.y
gcc lex.yy.c y.tab.c -lm -ll
但我会推荐这样的东西:
flex -o calc3.lex.c calc3.l
bison -o calc3.tab.c -vd calc3.y
gcc -o calc3 calc3.lex.c calc3.tab.c -lm -ll
执行此操作时,您需要更改
#include "y.tab.h"
至
#include "calc3.tab.h"
. (请注意,如果您将 bison 调用为
bison
而不是
yacc
,它将自动生成具有基于语法文件的名称的输出文件。但明确表示并没有什么坏处。)
关于yacc - y.tab.c : undefined reference to yylex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28211952/
本文整理了Java中jxl.biff.formula.Yylex.yylex()方法的一些代码示例,展示了Yylex.yylex()的具体用法。这些代码示例主要来源于Github/Stackoverf
本文整理了Java中org.json.simple.parser.Yylex.yylex()方法的一些代码示例,展示了Yylex.yylex()的具体用法。这些代码示例主要来源于Github/Stac
本文整理了Java中org.apache.dubbo.common.json.Yylex.yylex()方法的一些代码示例,展示了Yylex.yylex()的具体用法。这些代码示例主要来源于Githu
本文整理了Java中de.odysseus.staxon.json.stream.impl.Yylex.yylex()方法的一些代码示例,展示了Yylex.yylex()的具体用法。这些代码示例主要来
本文整理了Java中com.taobao.middleware.logger.json.parser.Yylex.yylex()方法的一些代码示例,展示了Yylex.yylex()的具体用法。这些代码
我正在尝试将位置信息添加到flex和bison中。但是我的野牛仍然使用int yylex(void),但是找不到int yylex(YYSTYPE *yylval_param, YYLTYPE *yy
所以我的问题很简单,我想之前已经有人回答了,但我无法找到关于它的好线索:我可以解析一次文件,收集一些必要的信息来初始化我的结构,然后解析第二次是“真的”吗? 当我两次调用 yylex() 时,它似乎什
我在 Flex 文件的定义部分中声明全局作用域指针时遇到问题,然后我在主文件的开头 malloc 它,但是 一旦我的程序运行到yylex(),指针的值就会设置为NULL。 我在整个程序中都需要这个指向
我正在尝试学习如何构建基于 C++ 的词法分析器/解析器,我正在关注多个在线教程和指南,我的代码主要是根据这两个指南构建的:1 , 2尽管我添加和调整了我认为合适的内容。 首先,我有一个词法分析器的头
我有一个简单的 flex 源代码,它跳过 /* */ 中的注释,应该得到找到的注释数: %{ int in_comment = 0; int count = 0; %} %% \/\* { i
我正在使用词法分析。为此,我使用 Flex然后我获取以下问题。 工作.l int cnt = 0,num_lines=0,num_chars=0; // Problem here. %% [" "]+
本文整理了Java中jxl.biff.formula.Yylex.yytext()方法的一些代码示例,展示了Yylex.yytext()的具体用法。这些代码示例主要来源于Github/Stackove
本文整理了Java中jxl.biff.formula.Yylex.zzUnpackAttribute()方法的一些代码示例,展示了Yylex.zzUnpackAttribute()的具体用法。这些代码
本文整理了Java中jxl.biff.formula.Yylex.zzUnpackTrans()方法的一些代码示例,展示了Yylex.zzUnpackTrans()的具体用法。这些代码示例主要来源于G
本文整理了Java中jxl.biff.formula.Yylex.zzScanError()方法的一些代码示例,展示了Yylex.zzScanError()的具体用法。这些代码示例主要来源于Githu
本文整理了Java中jxl.biff.formula.Yylex.zzUnpackRowMap()方法的一些代码示例,展示了Yylex.zzUnpackRowMap()的具体用法。这些代码示例主要来源
本文整理了Java中jxl.biff.formula.Yylex.zzUnpackAction()方法的一些代码示例,展示了Yylex.zzUnpackAction()的具体用法。这些代码示例主要来源
本文整理了Java中jxl.biff.formula.Yylex.getPos()方法的一些代码示例,展示了Yylex.getPos()的具体用法。这些代码示例主要来源于Github/Stackove
本文整理了Java中jxl.biff.formula.Yylex.zzRefill()方法的一些代码示例,展示了Yylex.zzRefill()的具体用法。这些代码示例主要来源于Github/Stac
本文整理了Java中jxl.biff.formula.Yylex.yybegin()方法的一些代码示例,展示了Yylex.yybegin()的具体用法。这些代码示例主要来源于Github/Stacko
我是一名优秀的程序员,十分优秀!