0

Hello guys i try to complie my files ast.l and ast.y but i dont understand why i have warrings.

lex file (ast.l)

%%
[0-9]+ {return NUM;}
\+ return PLUS;
\- return MINUS;
%%

yacc file (ast.y):

%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define YYSTYPE struct node*

typedef struct node
{
 char* token;
 struct node* left;
 struct node* right;
} node;
node* mknode(char* token, node* left, node* right);
void printtree(node* tree);
%}

%token NUM PLUS MINUS
%left PLUS MINUS


%%
s:exp {printf("OK\n"); printtree($1);}
exp:     exp PLUS exp {$$ = mknode("+",$1,$3);}
        |exp MINUS exp {$$ = mknode("-",$1,$3);}
        |NUM {$$ = mknode(yytext,NULL,NULL);};
%%

#include "lex.yy.c"
int main(){
 return yyparse();
}

void printtree(node* tree){
 printf("%s\n",tree->token);
 if(tree->left)
   printtree(tree->left);
 if(tree->right)
   printtree(tree->right);
}

node* mknode(char* token, node* left, node* right){
 node* newnode = (node*)malloc(sizeof(node));
 char* newstr = (char*)malloc(sizeof(char));
 strcpy(newstr, token);
 newnode->left = left;
 newnode->right = right;
 newnode->token = newstr;
 return newnode;
}


int yyerror(){
 printf("MY ERROR\n");
 return 0;
}

i dont understand why i get this error because in other computer i write the same and is work but when i move to my macbook pro and writes this in my treminal i get error like i miss something.

and the errors i get!:

y.tab.c:1230:16: error: implicit declaration of function 'yylex' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
      yychar = YYLEX;
               ^
y.tab.c:586:16: note: expanded from macro 'YYLEX'
# define YYLEX yylex ()
               ^
ast.y:25:23: error: use of undeclared identifier 'yytext'
    {(yyval) = mknode(yytext,NULL,NULL);}
                      ^
y.tab.c:1368:7: error: implicit declaration of function 'yyerror' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
      yyerror (YY_("syntax error"));
      ^
y.tab.c:1514:3: error: implicit declaration of function 'yyerror' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
  yyerror (YY_("memory exhausted"));
  ^
4 errors generated.
  • Unfortunately I can't comment, but this seems to be a duplicate https://stackoverflow.com/questions/23717039/generating-a-compiler-from-lex-and-yacc-grammar – Accácio Nov 16 '21 at 09:37

0 Answers0