I'm trying to make a simple parser to analyze BNF grammars as the input using flex and bison with c++. I'm getting some compile time errors. I have searched in other questions with similar errors and have corrected my files to match theirs, I'm still getting the errors.
This is my lex.l
%{
#include <iostream>
#include <string>
#define tkerror -1
#include "sintactic.tab.h"
using namespace std;
extern int row =1;
int col=0;
%}
%option caseful
%option noyywrap
%option yylineno
%option c++
ignora " "|\t|\n
ID [a-zA-Z]([a-zA-Z0-9_])*
%%
{ignora}+ {;}
"terminal" {col = col + strlen(yylval.cad); strcpy(yylval.cad, yytext); return tkterminal;}
";" {col = col + strlen(yylval.cad); strcpy(yylval.cad, yytext); return tkptocma;}
"," {col = col + strlen(yylval.cad); strcpy(yylval.cad, yytext); return tkcma;}
"no" {col = col + strlen(yylval.cad); strcpy(yylval.cad, yytext); return tkno;}
"iniciar" {col = col + strlen(yylval.cad); strcpy(yylval.cad, yytext); return tkiniciar;}
"con" {col = col + strlen(yylval.cad); strcpy(yylval.cad, yytext); return tkcon;}
"=" {col = col + strlen(yylval.cad); strcpy(yylval.cad, yytext); return tkasignar;}
".rule" {col = col + strlen(yylval.cad); strcpy(yylval.cad, yytext); return tkrule;}
"|" {col = col + strlen(yylval.cad); strcpy(yylval.cad, yytext); return tkor;}
"%" {col = col + strlen(yylval.cad); strcpy(yylval.cad, yytext); return tksep;}
"EPSILON" {col = col + strlen(yylval.cad); strcpy(yylval.cad, yytext); return tkeps;}
{ID} {col = col + strlen(yylval.cad); strcpy(yylval.cad, yytext); return tkid;}
[\r\n] {row++; col = 0;}
. {return tkerror;}
%%
And this is my sintactic.y
%{
#include <iostream>
#include <string>
#include "lex.yy.cc"
using namespace std;
extern int row;
extern int yylineno;
extern int col;
extern char* yytext;
extern "C" int yylex();
int yyerror(const char* men)
{
string output = yytext;
std::cout<<"Error sintactico "<<output<<" linea "<<row<<" columna "<<col<<endl;
return 0;
}
%}
%union{
int entero;
char cad [256];
}
%token<cad> TOK_EMPTY_LINE;
%token<cad> tkterminal;
%token<cad> tkptocma;
%token<cad> tkno;
%token<cad> tkiniciar;
%token<cad> tkcon;
%token<cad> tkasignar;
%token<cad> tkrule;
%token<cad> tkor;
%token<cad> tksep;
%token<cad> tkeps;
%token<cad> tkid;
%token<cad> tkcma;
%type<nodo> Lenguaje
%type<nodo> Area_Declaraciones;
%type<nodo> Area_NTInicial;
%type<nodo> Area_Gramatica;
%type<nodo> Lista_Declaraciones;
%type<nodo> Declaracion;
%type<nodo> Dec_Terminal;
%type<nodo> Dec_NoTerminal;
%type<nodo> Ids;
%type<nodo> Producciones;
%type<nodo> Produccion;
%type<nodo> Izquierda;
%type<nodo> Derecha;
%type<nodo> Id_Eps;
%%
Lenguaje: Area_Declaraciones tksep Area_NTInicial tksep Area_Gramatica;
Area_Declaraciones: Lista_Declaraciones;
Lista_Declaraciones: Declaracion Lista_Declaraciones
| ;
Declaracion: Dec_Terminal
| Dec_NoTerminal;
Dec_Terminal: tkterminal Ids tkptocma;
Dec_NoTerminal: tkno tkterminal Ids tkptocma;
Ids: tkid tkcma Ids
| tkid;
Area_NTInicial: tkiniciar tkcon tkid tkptocma;
Area_Gramatica: Producciones;
Producciones: Produccion Producciones
| ;
Produccion: Izquierda tkasignar Derecha tkptocma;
Izquierda: Ids tkrule;
Derecha: Id_Eps Derecha
| Id_Eps
| tkor Derecha;
Id_Eps: tkid
| tkeps;
%%
Compiling through the console using
bison -d sintactic.y
flex lex.l
g++ sintactic.tab.c -lfl -o scanner.sh
The errors that I get are these:
/usr/lib/x86_64-linux-gnu/libfl_pic.a(libmain.o): On the function `main':(.text.startup+0x9): undefined reference to `yylex'
/tmp/ccatti3x.o: On the function `yyerror(char const*)': sintactic.tab.c:(.text+0x23): undefined reference to `yytext[abi:cxx11]'
/tmp/ccatti3x.o: On the function `yyparse()': sintactic.tab.c:(.text+0x409): undefined reference to `yylex()'
collect2: error: ld returned 1 exit status
I will add the actions to sintactic.y after I can compile these without errors. I've seen other examples with almost the same stuff as mine and they seem to compile fine. I'm not used to c++ or flex/bison so I don't really know where those errors may come from.
-o scanner.sh
Seriously? – user0042lex.yy.c
. You'll need to compile that and link it with your scanner. – Jerry Coffinyytext
is achar*
, not astd::string
. – rici