我在任何声明中都遇到了这个错误
$$ = new vector<int>
如:
parser.y:109:29: error: expected unqualified-id before ‘int’
109 | stmts : stmt TSEMIC {$$ = new vector<int>; *$$ = $1;}
| ^~~
parser.y:109:29: error: expected ‘)’ before ‘int’
109 | stmts : stmt TSEMIC {$$ = new vector<int>; *$$ = $1;}
| ~ ^~~
| )
parser.y:109:61: error: expected unqualified-id before ‘int’
109 | stmts : stmt TSEMIC {$$ = new vector<int>; *$$ = $1;}
| ^
parser.y:109:61: error: expected ‘)’ before ‘int’
109 | stmts : stmt TSEMIC {$$ = new vector<int>; *$$ = $1;}
|
编译后找不到原因,这里是 .y 文件,只有语法部分会产生错误。
%define parse.error verbose
%{
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
extern int yylex();
extern int yylineno;
extern char *yytext;
extern int yyerrornum;
void yyerror (const char *msg) {
cout << "line " << yylineno <<": " << msg << " at token " << yytext << endl ;
yyerrornum++;
}
#include "Codigo.hpp"
#include "Exp.hpp"
expresionstruct makecomparison(std::string &s1, std::string &s2, std::string &s3) ;
expresionstruct makearithmetic(std::string &s1, std::string &s2, std::string &s3) ;
// Añado la declaración de la función unir. Si la hacéis diferente, debéis cambiar esta declaración.
vector<int> unir(vector<int> lis1, vector<int> lis2);
Codigo codigo;
%}
/*
qué atributos tienen los símbolos
*/
%union {
string *str ;
vector<string> *list ;
expresionstruct *expr ;
int number ;
vector<int> *numlist;
}
/* declaración de tokens. Esto debe coincidir con tokens.l */
%token <str> TIDENTIFIER TINTEGER TDOUBLE
%token <str> TCEQ TCNE TCLT TCLE TCGT TCGE TEQUAL
%token <str> TLPAREN TRPAREN TLBRACE TRBRACE TCOMMA TDOT
%token <str> TPLUS TMINUS TMUL TDIV
%token <str> TCOLON TSEMIC TASSIG
%token <str> RPROGRAM RIS RBEGIN RENDPROGRAM RVAR RINTEGER RFLOAT RENDPROCEDURE RPROCEDURE RIN ROUT RIF RTHEN RELSE RENDIF RGET RPUT_LINE RDO RWHILE RENDWHILE REXIT
/* declaración de símbolos no terminales con atributos */
%type <str> ident
%type <str> numeric
%type <expr> expr
%type <number> M
%type <numlist> N
%type <int> stmt
%type <int> stmts
//Falta declarar stmt stmts
%left TASSIG
%left TCEQ TCNE TCLT TCLE TCGT TCGE TEQUAL
%left TPLUS TMINUS
%left TMUL TDIV
%start program
%%
program : RPROGRAM { codigo.anadirInstruccion("prog" ) ;}
ident RIS
decls
RBEGIN
stmts
RENDPROGRAM TSEMIC {
codigo.anadirInstruccion("halt");
codigo.escribir() ;
}
;
decls : RVAR list TCOLON type TSEMIC
decls
|%empty
;
type : RFLOAT
| RINTEGER
;
list : ident
| list TCOMMA ident
;
stmts : stmt TSEMIC {$$ = new vector<int>; *$$ = $1;}
| stmts stmt TSEMIC {$$ = new vector<int>; *$$ = unir($1,$2);}
;
vector<int> unir(vector<int> lis1, vector<int> lis2){
vector<int> res;
res = lis1;
res.insert(res.end(), lis2.begin(), lis2.end());
return res;
}
这是 .cpp 部分:
#include "Codigo.hpp"
using namespace std;
/****************/
/* Constructora */
/****************/
Codigo::Codigo() {
siguienteId = 1;
}
/***********/
/* nuevoId */
/***********/
string Codigo::nuevoId() {
string nId("__t");
nId += to_string(siguienteId++);
return nId;
}
/*********************/
/* anadirInstruccion */
/*********************/
void Codigo::anadirInstruccion(const string &instruccion) {
string cadena;
cadena = to_string(obtenRef()) + ": " + instruccion;
instrucciones.push_back(cadena);
}
/***********************/
/* anadirDeclaraciones */
/***********************/
void Codigo::anadirDeclaraciones(const vector<string> &idNombres, const string &tipoNombre) {
vector<string>::const_iterator iter;
for (iter=idNombres.begin(); iter!=idNombres.end(); iter++) {
anadirInstruccion(tipoNombre + " " + *iter );
}
}
/*********************/
/* anadirParametros */
/*********************/
void Codigo::anadirParametros(const vector<string> &idNombres, const string &tipoNombre) {
vector<string>::const_iterator iter;
for (iter=idNombres.begin(); iter!=idNombres.end(); iter++) {
anadirInstruccion("param_" + tipoNombre + " " + *iter );
}
}
/**************************/
/* completarInstrucciones */
/**************************/
void Codigo::completarInstrucciones(vector<int> &numInstrucciones, const int valor) {
string referencia = " " + to_string(valor) ;
vector<int>::iterator iter;
for (iter = numInstrucciones.begin(); iter != numInstrucciones.end(); iter++) {
instrucciones[*iter-1].append(referencia);
}
}
/************/
/* escribir */
/************/
void Codigo::escribir() const {
vector<string>::const_iterator iter;
for (iter = instrucciones.begin(); iter != instrucciones.end(); iter++) {
cout << *iter << " ;" << endl;
}
}
/************/
/* obtenRef */
/************/
int Codigo::obtenRef() const {
return instrucciones.size() + 1;
}
还有.hpp
#ifndef CODIGO_HPP_
#define CODIGO_HPP_
#include <iostream>
#include <sstream>
#include <fstream>
#include <set>
#include <vector>
/* Estructura de datos para el código generado. El código, en vez de escribirlo directamente,
* se guarda en esta estructura y, al final, se escribirán en un fichero.
*/
class Codigo {
private:
/**************************/
/* REPRESENTACION INTERNA */
/**************************/
/* Instrucciones que forman el código. */
std::vector<std::string> instrucciones;
/* Clave para generar identificaciones nuevos. Cada vez que se crea un idse incrementa. */
int siguienteId;
public:
/************************************/
/* METODOS PARA GESTIONAR EL CODIGO */
/************************************/
/* Constructora */
Codigo();
/* Crea un nuevo identificador del tipo "__t1, __t2, ...", siempre diferente. */
std::string nuevoId() ;
/* Añade una nueva instrucción a la estructura. */
void anadirInstruccion(const std::string &instruccion);
/* Dada una lista de variables y su tipo, crea y añade las instrucciones de declaración */
void anadirDeclaraciones(const std::vector<std::string> &idNombres, const std::string &tipoNombre);
/* Dada una lista de parámetros y su tipo, crea y añade las instrucciones de declaración */
void anadirParametros(const std::vector<std::string> &idNombres, const std::string &tipoNombre) ;
/* Añade a las instrucciones que se especifican la referencia que les falta.
* Por ejemplo: "goto" => "goto 20;" */
void completarInstrucciones(std::vector<int> &numInstrucciones, const int valor);
/* Escribe las instrucciones acumuladas en la estructura en el fichero de salida. */
void escribir() const;
/* Devuelve el número de la siguiente instrucción. */
int obtenRef() const;
};
#endif /* CODIGO_HPP_ */
所有定义都在这段代码中,令牌、函数定义在其他档案中,在那里找不到任何问题,我猜是一些错误的声明,但这是我第一次在使用相同的声明编译代码后得到这个错误没有出现这个错误。
因为我是新用户,我不能写评论。
但是,最后一个语法规则和附加代码之间缺少
%%
应该是这样的:
stmts : stmt TSEMIC {$$ = new vector<int>; *$$ = $1;}
| stmts stmt TSEMIC {$$ = new vector<int>; *$$ = unir($1,$2);}
;
%%
vector<int> unir(vector<int> lis1, vector<int> lis2){
vector<int> res;
res = lis1;
res.insert(res.end(), lis2.begin(), lis2.end());
return res;
}