Reputation: 1
Im having trouble on this flex bison project, can't understand why i keep receiving this error"no declared type"..
my flex file
%token T_COM
%type <codigo> ficheiro
%type <codigo> idents
%type <codigo> comentario
%type <codigo> def_consts def_const def_vars def_var
%type <codigo> def_function tipo main bloco blocoSemMAin
%type <codigo> statements statement statementsSemMain
%type <codigo> valores_const valor_const expr
%type <i> const_integer_expr
%type <d> const_real_expr
%type <i> const_boolean_expr
%type <s> const_string_expr
%%
ficheiro
: def_vars def_function { printf( "%s %s\n",$1, $2); }
| def_vars { printf( "%s\n",$1); }
| def_function { printf( "%s\n", $1 );}
| comentario { printf( "%s\n", $1 );}
;
comentario
: T_COM { sprintf( $$,"REM %s ",$1);}
;
This is how i declared in bison
%option noyywrap nounput case-insensitive yylineno
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "consts.h"
/* Estamos a usar %union, pelo que NAO repetimos a definicao nem da %union,
nem de YYSTYPE, aqui. */
/* Incluir as definicoes criadas automaticamente pelo
bison, e exportadas por este para um ficheiro ".h"
*/
#include "pascal.tab.h"
%x COMENTARIO
%%
Uses return T_USES;
In return T_IN;
Label return T_LABEL;
Var return T_VAR;
Const return T_CONST;
int return T_INTEGER;
double return T_REAL;
Boolean return T_BOOLEAN;
char return T_CHAR;
String return T_STRING;
Procedure return T_PROCEDURE;
Function return T_FUNCTION;
"{" return T_BEGIN;
"END" return T_END; // não dava para alterar estava sempre a dar erro
if return T_IF;
Then return T_THEN;
else return T_ELSE;
Case return T_CASE;
Of return T_OF;
while return T_WHILE;
do return T_DO;
Repeat return T_REPEAT;
Until return T_UNTIL;
For return T_FOR;
To return T_TO;
DownTo return T_DOWNTO;
Goto return T_GOTO;
main return T_MAIN;
void return T_VOID;
True return T_TRUE;
False return T_FALSE;
"(" T_ABRE;
")" T_FECHA;
":=" return T_ASSIGN;
"<=" return T_LESS_EQ;
">=" return T_GREAT_EQ;
"!=" return T_NOT_EQ;
"==" return T_EQ;
"&&" return T_AND;
"||" return T_OR;
"*" return T_STAR;
"++" return T_INCRE;
"--" return T_DECR;
"!" return T_NOT;
Div return T_DIV;
Mod return T_MOD;
Ord return T_ORD;
Chr return T_CHR;
"strcpy" return T_STRCOPY;
"strcat" return T_STRCAT;
"strcmp" return T_STRCMP;
"/*"([^*]|[*]+[^/])*[*]+[/] {
return T_COM; }
[0-9]+ {
/* repara como nao tem sinal ao inicio: ve^ "expr" no Bison */
yylval.i = atoi( yytext );
return V_INTEGER;
}
([0-9]+([.][0-9]*)?|[.][0-9]+)([Ee][+-]?[0-9]+)? {
/* repara como nao tem sinal ao inicio: ve^ "expr" no Bison */
yylval.d = atof( yytext );
return V_REAL;
}
'[^']{0,255}' {
#if MAX_STR_LEN != 255
#error "Por favor atualize a expressao regular acima para refletir o novo valor de MAX_STR_LEN"
#endif
strncpy( yylval.s, yytext+1, MAX_STR_LEN );
yylval.s[ strlen(yylval.s)-1 ] = '\0';
return V_STRING;
}
[a-zA-Z_][a-zA-Z0-9_]{0,31} {
#if MAX_IDENT_LEN != 32
#error "Por favor atualize a expressao regular acima para refletir o novo valor de MAX_IDENT_LEN"
#endif
yylval.pci = encontrar_integer_const( yytext );
if( yylval.pci != NULL )
return V_INTEGER_CONST;
yylval.pcr = encontrar_real_const( yytext );
if( yylval.pcr != NULL )
return V_REAL_CONST;
yylval.pcb = encontrar_boolean_const( yytext );
if( yylval.pcb != NULL )
return V_BOOLEAN_CONST;
yylval.pcs = encontrar_string_const( yytext );
if( yylval.pcs != NULL )
return V_STRING_CONST;
#if MAX_STR_LEN < MAX_IDENT_LEN
#error "Nao consigo (sempre) guardar um identificador dentro de uma string: por favor verifique o codigo e corrija"
#endif
strcpy( yylval.s, yytext );
return V_IDENT;
}
[-+*/()<>=,;:.] return (int) yytext[0]; /* tokens de um so' caracter */
[ \t\r\n]+ /* ignorar */
[{][^}]*[}] /* ignorar */
. fprintf( stderr, "Caracter invalido na linha %d: '%c'\n", yylineno, yytext[0] );
%%
void inicia_flex( const char *filename )
{
FILE *fp;
if( filename != NULL )
{
fp = fopen( filename, "r" );
if( fp == NULL )
fprintf( stderr, "Ficheiro \"%s\" nao encontrado: a ler do teclado.\n", filename );
else
yyrestart( fp );
}
}
void termina_flex( void )
{
fclose( yyin );
}
So my error is if i try to make this project I get an error on the
: T_COM { sprintf( $$,"REM %s ",$1);}
and it says : pascal.y:172.85-86: $1 of `comentario' has no declared type
PS- I just copied the part of code related to the error the rest seems to work fine
Thanks :)
Upvotes: 0
Views: 242
Reputation: 241911
The terminal T_COM
($1
in that production) certainly has no declared type. Furthermore, although it is invisible to bison, the scanner never sets any member of yylval
when it returns a T_COM
, so it seems fair to say that the terminal really does not have a semantic value.
The use of $1
as an argument to printf
corresponding to a %s
format suggests that you intended it to have a string type. If so, you need to add the type tag to the %token
declaration, and make sure you set the corresponding member of yylval
in the scanner.
The %token
declaration would look something like this:
%token <s> T_COM
assuming that in your %union
declaration, the tag s
refers to a string-like object. (Although looking at your code it seems like it could equally well be the codigo
tag.)
Upvotes: 1