Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5942247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:13:33+00:00 2026-05-22T16:13:33+00:00

When I execute the command yacc -dv c2p.y on my c2p.y yacc file the

  • 0

When I execute the command yacc -dv c2p.y on my c2p.y yacc file the error $1 of `…’ has no declared type in Bison/Yacc occurs several times.

What I know is that I have to add a %type something, and delete $1.string but it still doesn’t work.

Can you please help solve this error ?

Here is my initial code:

%{
#include
#include
int func;
char* ch;
int mainf=-1; 
%}
%start prog
%token MAIN_
%token PRINTF_
%token STRING_
%token TYPE_ 
%token ID_
%token IF_ 
%token COND_ 
%token ELSE_
%token FOR_
%token WHILE_
%token DO_
%token UNTIL_
%token ATRIB_
%token PLUSPLUS_

%union {
    char* string;
};

%%

prog: funcs
;

funcs:
| func funcs
;

func: head block
;

head: TYPE_ MAIN_ '(' args ')' {mainf++;}
| TYPE_ {if(!strcmp($1.string, "void")) {func = 0; printf("\n\nprocedure "); }
        else { func = 1; printf("\n\nfunction ");} } 
      ID_ '(' {printf("%s(", $3.string);}
      args ')'
      {printf(")"); if(func == 1) transRetType($1.string);
      else printf(";");}
;

args: 
| TYPE_ ID_ {translateType($1.string, $2.string);} args
| TYPE_ ID_ ',' {translateType($1.string, $2.string);printf("; ");} args
;

block:
| '{' {if(mainf==0)
{    printf("\n\nBEGIN");mainf--;}
    else    printf("\nbegin");
    } 
        called_funcs 
        '}' {
        if(t_main == 0) printf("\nEND.");  else printf("\nend;");}
| '{' decvars {printf("\nbegin");} called_funcs '}' {printf("\nend"); 
        if(t_main == 0) printf("."); else printf(";");}
;

decvars: {printf("\nvar ");} listdecl
;

listdecl: decl | listdecl decl
;

decl: 
TYPE_ listvars ';' { transRetType($1.string);}
;

listvars: ID_ {printf("%s", $1.string);} | ID_ ',' listvars {printf(", %s", $1.string);}
;

called_funcs: 
| block
| called_func called_funcs
;

called_func: printf
| if
| func_apel
| for
| while
| do
| atrib
| inc
;

inc:
ID_ PLUSPLUS_ ';' {printf("%s = %s + 1 ;",$1,$1);}
;

expr:
ID_ {printf("%s",$1);}
| expr '+'{printf("+");} expr
| expr '-'{printf("-");} expr
| expr '*'{printf("*");} expr
| expr '/'{printf(" div ");} expr
| expr '%'{printf(" mod ");} expr
| '-'{printf("- ");} expr     
| '('{printf("(");} expr ')'{printf(")");}
;

for:
FOR_ {printf("\nfor ");}
'(' ID_ {printf("%s :=", yylval.string);} 
    ATRIB_ ID_ {printf("%s to ", yylval.string);} 
    ';' ID_ COND_ ID_ {printf("%s do",yylval.string);} 
    ';'
     ID_
     '+' 
     ')' 
    called_funcs
;

while:
WHILE_  {printf("\nwhile ");}
'(' ID_ {printf("%s", $3);} 
    COND_ 
        {
        if(!strcmp(yylval.string, "==")) printf("="); 
        else if(!strcmp(yylval.string, "!=")) printf("");
        else printf("%s", yylval.string);}
    ID_ 
        {printf(" %s do", yylval.string);}  
')' called_funcs            
;

do:
DO_ {printf("\nrepeat");}
called_funcs
UNTIL_ {printf("\nuntil ");}
'(' ID_ {printf("%s", yylval.string);} 
    COND_   {
        if(!strcmp(yylval.string, "==")) printf("="); 
        else if(!strcmp(yylval.string, "!=")) printf("");
        else printf("%s", yylval.string);}
    ID_ 
        {printf(" %s ;", yylval.string);}  
')'  ';'
;

atrib:
ID_ {printf("\n%s := ",$1);} ATRIB_ expr ';' {printf(";");}
| ID_ "++" {printf("\n %s = %s + 1",$1,$1);}
| ID_ '--' {printf("\n %s = %s - 1",$1,$1);}
;

if: IF_ '(' ID_ {printf("\nif %s", yylval.string);} COND_ 
        {if(!strcmp(yylval.string, "==")) printf("="); else printf("%s", yylval.string);}
        ID_ {printf(" %s then", yylval.string);} ')' called_funcs else
;

else:
| ELSE_ {printf("\nelse");} called_funcs
;

func_apel: 
ID_ '(' {printf("\n%s(", $1.string);} call_args ')' ';' {printf(");");}
;

call_args: call_args ',' {printf(", ");} call_arg 
| call_arg
;

call_arg: 
| ID_ {printf("%s", yylval.string);}
;

printf: PRINTF_ '(' STRING_ {output(yylval.string);} ')' ';'
;

%%
void transRetType(char* str) {
    if(!strcmp(str, "int")) printf(":integer;");
    if(!strcmp(str, "long")) printf(":longint;");
    if(!strcmp(str, "char")) printf(":byte;");
    if(!strcmp(str, "float")) printf(":real;");
}
void translateType(char* str1, char* str2) {
    if(!strcmp(str1, "int")) printf("%s:integer", str2);
    if(!strcmp(str1, "long")) printf("%s:longint", str2);
    if(!strcmp(str1, "char")) printf("%s:byte", str2);
    if(!strcmp(str1, "float")) printf("%s:real", str2);
}
void output(char* str) {
    int i;
    printf("\nwriteln(\"");
    for(i = 1; i 

Thank you, Horatiu

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:13:34+00:00Added an answer on May 22, 2026 at 4:13 pm

    You need to add a type to the declarations of tokens with semantic values, like this:

    %token <string> TYPE_
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to execute a command 100-200 times, and so far my research indicates
I don't know how to execute a command stored as a variable or how
im looking to execute to execute command line commands from objective c. I know
i have a file contain list of path now i want to execute command
I want to execute a command, have the output of that command get gzip'd
I need help creating a batch file that first runs a command on every
I'm trying to create a LALR(1) parser in Yacc/Bison that can accept commands with
How do I execute a command-line program from C# and get back the STD
How can I execute a command line from within a WiX script? I want
When I execute the command adb logcat while running the android emulator, all of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.