Lex and Yacc declared type -- Getting an error

AI Thread Summary
The discussion revolves around a coding error encountered in a parser implementation using Yacc/Bison. The error message indicates that the variable $3 in the rule for the EXPI production does not have a declared type, which is causing a compilation issue. The user notes that $3 corresponds to the value of OP_OP in the provided code snippet, but it seems that the type for this token has not been properly defined in the grammar. The user is seeking clarification on why EXPI is flagged as the problematic variable. The issue arises because the grammar expects a specific type for each token, and if a type is not declared for $3, it leads to ambiguity in the parser's expectations. The conversation emphasizes the importance of ensuring that all tokens used in production rules have corresponding types declared in the union section of the grammar to avoid such errors.
anonim
Messages
39
Reaction score
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
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top