Lex and Yacc declared type -- Getting an error

In summary: Looking at the code, it appears that the code fragment is to be executed when KW_WHILE is parsed. The problem seems to be that the token NEWLINE is not declared and is used in the code fragment. The solution would be to specify the type of NEWLINE, as has been done for other tokens.
  • #1
anonim
40
2
in .y code:

Code:
%union{
    int ival;
    int *ivals;
    char id[20];
    char *str;
    bool *b;
}
%start START

%token COMMENT OP_PLUS OP_MINUS OP_DIV OP_MULT OP_OP OP_CP OP_DBLMULT OP_OC OP_CC OP_COMMA KW_AND KW_OR KW_NOT KW_EQUAL KW_LESS
KW_NIL KW_LIST KW_APPEND KW_CONCAT KW_SET KW_DEFFUN KW_FOR KW_IF KW_EXIT KW_DEFVAR KW_WHILE
KW_LOAD KW_DISP KW_TRUE KW_FALSE NEWLINE
FLOAT_NUMBER LISTOP%token <ival> LIMITED_VALUE
%token <id> IDENTIFIER
%token <str> FLE
%token <ival> NUMBER

%type <ival> START
%type <ival> EXPI
%type <ival> EXPB
%type <ivals> VALUES
%type <ivals> EXPLISTI
%type <ivals> LISTVALUE
%type <b> BinaryValue

in l code:

Code:
[[:alpha:]][[:alnum:]]*   {strcpy(yylval.id, yytext); return IDENTIFIER;}
[[1-9][:digit:]]+         {yylval.ival = atoi(yytext); return VALUE;}
{LIMITED_VALUE}                   {yylval.ival = atoi(yytext); return VALUE;}

I got an error:

error: $3 of ‘EXPI’ has no declared type $$=$3;

code blog with the line where I got the error:
Code:
| OP_OP KW_WHILE OP_OP EXPB OP_CP EXPLISTI OP_CP{
    $$=$3;
    if(!$3){
        listIndex=0;
        listValues[0]=NULL;
    }
    list_flag=1;
}
Why am I getting an error when I declare it? Can you help me solve this?
 
Technology news on Phys.org
  • #2
As I recall in yacc, $3 refers to the value of OP_OP in the code fragment given. The %token expression given doesn't assign a type which is an error. Why EXPI is reported as the offending variable, I don't know.
 

FAQ: Lex and Yacc declared type -- Getting an error

What is Lex and Yacc?

Lex and Yacc are tools used in computer programming to generate lexical analyzers and parsers for programming languages.

What does the error "Lex and Yacc declared type" mean?

This error indicates that there is an issue with the declared type in the code, specifically in relation to the use of Lex and Yacc.

How can I fix the "Lex and Yacc declared type" error?

The error can be fixed by checking the code for any syntax errors related to the use of Lex and Yacc, and ensuring that the declared type is used correctly.

Are there any common mistakes that can cause the "Lex and Yacc declared type" error?

Yes, some common mistakes include misspelling the declared type, using the wrong syntax for declaring the type, or not properly including necessary libraries for Lex and Yacc.

Can I use other tools besides Lex and Yacc for lexical analysis and parsing?

Yes, there are other tools such as Flex and Bison that can also be used for these tasks. However, Lex and Yacc are commonly used and have been around for a long time, making them a popular choice for many programmers.

Back
Top