Flex野牛:构建C编译器——错误消息中的行号

时间:2022-11-13 09:37:26

When I try to print my line number in my yyerror function, I get an error. I'm working on OSX.

当我尝试在yyerror函数中打印行号时,会得到一个错误。我工作在OSX。

(line number in compiler.l)

(在compiler.l行号)

%{

#include "compiler.tab.h"
int lineNumber = 1;

%}
%%

int { return INT; }
float { return FLOAT; }
void { return VOID; }
do { return DO; }
while { return WHILE; }
if { return IF; }
return { return RETURN; }
else { return ELSE; }
\/\/(.)* { return COMMENT; }
\/\*(.)*\*\/ { return COMMENT; }
[a-zA-Z][a-zA-Z0-9_]* { return IDENT; }
[0-9]+\.[0-9]+ { sscanf(yytext, "%lf", &yylval.reell); return FVAL; }
[0-9]+ { yylval.integer = atoi(yytext); return IVAL; }
\( { return OPA; }
\) { return CPA; }
\{ { return OB; }
\} { return CP; }
\; { return SEMICOLON; }
\, { return COMMA; }
\! { return NOT; }
\-\- { return DECREMENT; }
\+\+ { return INCREMENT; }
\<\< { return LSHIFT; }
\=\= { return EQUALS; }
\<\= { return LTOE; }
\>\= { return GTOE; }
\&\& { return AND; }
\|\| { return OR; }
\= { return ASSIGNMENT; }
\< { return LT; }
\> { return GT; }
\/ { return DIVIDE; }
\* { return MULTIPLY; }
\+ { return PLUS; }
\- { return MINUS; }
[ ]+ { return BLNK; }
[\n]* { ++lineNumber; return NL; }

%%

compiler.c

compiler.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "compiler.h"

extern int lineNumber;

int main (void)
{
    yyparse();
    return 0;
}

void yyerror(char * str)
{
    fprintf(stderr, "Error: %s, Line %d\n", str, lineNumber);
}

I get the following error message:

我得到以下错误信息:

Undefined symbols for architecture x86_64: "_lineNumber", referenced from: _yyerror in compiler.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status

架构x86_64的未定义符号:“_lineNumber”,由编译器中的_yerror引用。在架构x86_64 collect2中没有找到符号(s): ld返回1退出状态。

This is the command (shell file) I use to compile:

这是我用来编译的命令(shell文件):

#!/bin/bash

flex  compiler.l
bison -d compiler.y

gcc -g -c compiler.tab.c -o compiler_y.o
gcc -g -Wall -std=c99 -c lex.yy.c
gcc -g -c compiler.tab.c -o compiler_y.o

gcc -g -Wall -std=c99 -c compiler.c
gcc -g -o compiler compiler.o compiler_y.o lex.yy.o -lm -lfl

1 个解决方案

#1


6  

You can use %option yylineno in your flex source and then the variable int yylineno will give the line-number of your current token. Beware not to use yylineno from the grammar (bison/yacc) as the tokenizer in general is ahead of the parser. If you want to use line numbers in the parser, typically the line numbers are attached to the tokens, so the parser has access through the tokens.

您可以在flex源代码中使用%option ylineno,然后变量int ylineno将给出当前令牌的行号。注意不要在语法(bison/yacc)中使用ylineno,因为标记器一般都在解析器之前。如果您希望在解析器中使用行号,通常会将行号附加到令牌,因此解析器可以通过令牌访问。

The error message is likely caused by not linking in compiler.tab.o, obtained from compiler.l by running flex on it and then passing it through the C-compiler.

错误消息可能是由于没有在compiler.tab中链接而引起的。啊,从编译器。通过在它上运行flex,然后通过c编译器传递它。

#1


6  

You can use %option yylineno in your flex source and then the variable int yylineno will give the line-number of your current token. Beware not to use yylineno from the grammar (bison/yacc) as the tokenizer in general is ahead of the parser. If you want to use line numbers in the parser, typically the line numbers are attached to the tokens, so the parser has access through the tokens.

您可以在flex源代码中使用%option ylineno,然后变量int ylineno将给出当前令牌的行号。注意不要在语法(bison/yacc)中使用ylineno,因为标记器一般都在解析器之前。如果您希望在解析器中使用行号,通常会将行号附加到令牌,因此解析器可以通过令牌访问。

The error message is likely caused by not linking in compiler.tab.o, obtained from compiler.l by running flex on it and then passing it through the C-compiler.

错误消息可能是由于没有在compiler.tab中链接而引起的。啊,从编译器。通过在它上运行flex,然后通过c编译器传递它。