Lex and Yacc declared type -- Getting an error

Click For Summary
SUMMARY

The discussion revolves around a type declaration error in a Lex and Yacc implementation. The user encounters the error "error: $3 of ‘EXPI’ has no declared type" when attempting to assign a value in a rule. The issue arises because the token for $3, which is expected to have a type, is not properly declared in the Yacc grammar. The user needs to ensure that all tokens used in expressions have corresponding types defined in the %type declarations.

PREREQUISITES
  • Understanding of Lex and Yacc syntax and structure
  • Familiarity with type declarations in Yacc
  • Knowledge of token definitions and their usage in parsing
  • Basic programming skills in C for implementing Lex and Yacc
NEXT STEPS
  • Review Yacc documentation on %type and %token declarations
  • Learn about error handling in Yacc and how to debug type-related issues
  • Explore examples of Lex and Yacc implementations with complex type systems
  • Investigate the use of semantic actions in Yacc to manage values during parsing
USEFUL FOR

Developers working with parser generators, specifically those using Lex and Yacc, as well as programmers debugging type-related issues in their grammar definitions.

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.
 

Similar threads

Replies
6
Views
2K
Replies
55
Views
7K
Replies
63
Views
5K
  • · Replies 10 ·
Replies
10
Views
10K
  • · Replies 9 ·
Replies
9
Views
5K
Replies
1
Views
3K
  • · Replies 29 ·
Replies
29
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 2 ·
Replies
2
Views
1K