Action has no declared type error

patr1c1a

I'm using Flex and Bison with my grammar for a very simple programming language. But I'm a beginner, so I'm not exactly sure about how to work with them.

Besides some basic operations (like arithmetic and logical operations, assignments, etc.) I also have a function that needs to be recognized by this language: this function is called "ftoc" and it takes one numeric argument.

In my flex .l file I have (among others) these definitions:

DIGITO [0-9]
PUNTO ["."]
CONST_REAL {DIGITO}+{PUNTO}{DIGITO}+
CONST_ENTERA {DIGITO}+
P_A ["("]
P_C [")"]

I also have this rule for the "ftoc" function:

"FtoC" { printf("%s\n", yytext);return FTOC;}

My bison .y file has this union to accept three data types:

%union {
    char *text;
    int integer;
    double real;
}

It also has these token definitions:

%token <integer> CONST_ENTERA
%token <real> CONST_REAL
%token <text> INTEGER
%token <text> REAL
%token <text> STRING
%token P_A P_C
%token FTOC

And the rules involving the "ftoc" function are these:

conversion:
    FTOC P_A constante P_C
;
constante:
    CONST_ENTERA
    | CONST_REAL
;

Now, I need to write a function that prints "ftoc" plus its argument every time the function is found. E.g., if the parser finds the function used as: FtoC(57.6) I need it to print "ftoc57.6".

My function to print this would look somewhat like this:

printFtoc(char* function, double argument){
    snprintf("%s, %f", function, argument);
}

My problem here is how exactly I should pass the parameters to this function. I tried with $n in the "conversion" rule, like this:

FTOC P_A constante P_C {printFtoC($1, $3)}

But then I get 2 errors in Bison (one for each $n), saying that "conversion" has no declared type.

I've been trying to read from Bison documentation but I don't really get what exactly I should be doing. From other questions and forums I get I might need to do something with %type or with %token, but I really don't know.

I just need to get the function name and the argument whenever they're found, but no clue on how to achieve that, since $n didn't work.

Chris Dodd

The error tells you exactly what is wrong -- you have a $3 use in a rule that corresponds to a constante, but you haven't declared a type for constante. You probably need a declaration something like:

%type<real> constante

to tell bison that a constante is in the real field of the union. You also have a problem with $1 in that action, because FTOC has no type. You could fix that by either giving FTOC a type and modifying the lex rule to yylval appropriately, or by getting rid of the $1 and using a string literal like "ftoc" instead

Any given non-terminal can only have a single type, so you'll need to add actions to your constante rules. With the above %type declaration, you would have:

constante: CONST_ENTERA  { $$ = $1; /* implicit int->double conversion */ }
         | CONST_REAL    { $$ = $1; /* simple assignment of a double */ }
;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Does not name type and was not declared error

From Dev

XSD Error: Type is not declared, or is not a simple type

From Dev

error: 'ios_base' has not been declared

From Dev

Error: Initializers may only be declared within a type

From Dev

ERROR: return type mismatch in function declared to return

From Dev

ERROR: return type mismatch in function declared to return

From Dev

Implicit type compilation error although declared

From Dev

Discover if a class has been declared inside a particular module type

From Dev

Difference between errors "does not name a type" and "has not been declared"

From Dev

typescript function declared a non-void type but has no return expression

From Dev

Discover if a class has been declared inside a particular module type

From Dev

Difference between errors "does not name a type" and "has not been declared"

From Dev

Type 'FILE' has not been declared jpeglib.h

From Dev

Member "has already been declared" error with CUDA and Eigen

From Dev

C++, error: '__locale_t' has not been declared

From Dev

"Error: '::hypot' has not been declared" in cmath while trying to embed Python

From Dev

C++ make error: object_var has not been declared

From Dev

build insert statement error Https has already been declared

From Dev

C++ Compile error on NetBSD '::system' has not been declared

From Dev

ERROR: 'Namespace for prefix 'xsi' has not been declared.'

From Dev

The field was declared on serializer , but has not been included error in django rest

From Dev

Ajax has error but there's no exception in action method

From Dev

error: field has incomplete type

From Dev

error: field has incomplete type

From Dev

How to show an error in the Xtext IDE if a type is referenced before it is declared

From Dev

No type errors reported when list literal has elements not matching declared generic type. Why?

From Dev

Bool has not been declared

From Dev

Namespace has not been declared

From Dev

Action as generic type in constructor gives compile error

Related Related

  1. 1

    Does not name type and was not declared error

  2. 2

    XSD Error: Type is not declared, or is not a simple type

  3. 3

    error: 'ios_base' has not been declared

  4. 4

    Error: Initializers may only be declared within a type

  5. 5

    ERROR: return type mismatch in function declared to return

  6. 6

    ERROR: return type mismatch in function declared to return

  7. 7

    Implicit type compilation error although declared

  8. 8

    Discover if a class has been declared inside a particular module type

  9. 9

    Difference between errors "does not name a type" and "has not been declared"

  10. 10

    typescript function declared a non-void type but has no return expression

  11. 11

    Discover if a class has been declared inside a particular module type

  12. 12

    Difference between errors "does not name a type" and "has not been declared"

  13. 13

    Type 'FILE' has not been declared jpeglib.h

  14. 14

    Member "has already been declared" error with CUDA and Eigen

  15. 15

    C++, error: '__locale_t' has not been declared

  16. 16

    "Error: '::hypot' has not been declared" in cmath while trying to embed Python

  17. 17

    C++ make error: object_var has not been declared

  18. 18

    build insert statement error Https has already been declared

  19. 19

    C++ Compile error on NetBSD '::system' has not been declared

  20. 20

    ERROR: 'Namespace for prefix 'xsi' has not been declared.'

  21. 21

    The field was declared on serializer , but has not been included error in django rest

  22. 22

    Ajax has error but there's no exception in action method

  23. 23

    error: field has incomplete type

  24. 24

    error: field has incomplete type

  25. 25

    How to show an error in the Xtext IDE if a type is referenced before it is declared

  26. 26

    No type errors reported when list literal has elements not matching declared generic type. Why?

  27. 27

    Bool has not been declared

  28. 28

    Namespace has not been declared

  29. 29

    Action as generic type in constructor gives compile error

HotTag

Archive