Re: Hi everyone,

"[email protected]" <[email protected]>
Newsgroups gmane.comp.parsers.bison.general
Message-ID <[email protected]>
I've managed to come a bit more forward, now my lexer.l files look like 
this:

%{
#include <stdio.h>
#include <stdlib.h>
#include "./parser.tab.h"
enum yytokentype {
     NUMBER = 285,
     VAL = 292,
     ADD = 286,
     SUB = 287,
     MUL = 288,
     DIV = 289,
     ABS = 290,
     EOL    = 291
};
int yylval;
%}

%%

"+"            { return ADD; }
"-"            { return SUB; }
"*"            { return MUL; }
"/"            { return DIV; }
"|"            { return ABS; }
[0-9]+        { yylval = atoi(yytext); return NUMBER; }
\n            { return EOL; }
[ \t]        { /* ignore whitespaces */ }
.            { printf("Unknow char :%c", *yytext); }

%%


parser.y:

%{
#include <stdio.h>
#include <stdlib.h>
%}


%token NUMBER
%token ADD SUB MUL DIV ABS
%token EOL

%%

calclist: /* nothing */
     | calclist exp EOL { printf(" = %d\n", $2); }
     ;

exp: factor
     | exp ADD factor { $$ = $1 + $3; }
     | exp SUB factor { $$ = $1 - $3; }
     ;

factor: term
     | factor MUL term { $$ = $1 * $3; }
     | factor DIV term { $$ = $1 / $3; }
     ;

term: NUMBER
     | ABS term { $$ = $2 >=0? $2 : - $2; }
     ;

%%
int main(int argc, char **argv){
     yyparse();
     return(0);
}

int yyerror(char *s){
     fprintf(stderr, "error: %s\n", s);
     return(0);
}

I can compile with flex -l lexer.l; bison -d parser.y; gcc -o parser 
parser.tab.c lex.yy.c -lfl but i get the following error messages:


parser.tab.c: In function ‘yyparse’:
parser.tab.c:1124:16: warning: implicit declaration of function ‘yylex’ 
[-Wimplicit-function-declaration]
        yychar = yylex ();
                 ^~~~~
parser.tab.c:1289:7: warning: implicit declaration of function ‘yyerror’ 
[-Wimplicit-function-declaration]
        yyerror (YY_("syntax error"));
        ^~~~~~~
lexer.l:5:6: error: nested redefinition of ‘enum yytokentype’
  enum yytokentype {
       ^~~~~~~~~~~
lexer.l:5:6: error: redeclaration of ‘enum yytokentype’
In file included from lexer.l:4:0:
./parser.tab.h:46:8: note: originally defined here
    enum yytokentype
         ^~~~~~~~~~~
lexer.l:6:2: error: redeclaration of enumerator ‘NUMBER’
   NUMBER = 285,
   ^~~~~~
In file included from lexer.l:4:0:
./parser.tab.h:48:5: note: previous definition of ‘NUMBER’ was here
      NUMBER = 258,
      ^~~~~~
lexer.l:8:2: error: redeclaration of enumerator ‘ADD’
   ADD = 286,
   ^~~
In file included from lexer.l:4:0:
./parser.tab.h:49:5: note: previous definition of ‘ADD’ was here
      ADD = 259,
      ^~~
lexer.l:9:2: error: redeclaration of enumerator ‘SUB’
   SUB = 287,
   ^~~
In file included from lexer.l:4:0:
./parser.tab.h:50:5: note: previous definition of ‘SUB’ was here
      SUB = 260,
      ^~~
lexer.l:10:2: error: redeclaration of enumerator ‘MUL’
   MUL = 288,
   ^~~
In file included from lexer.l:4:0:
./parser.tab.h:51:5: note: previous definition of ‘MUL’ was here
      MUL = 261,
      ^~~
lexer.l:11:2: error: redeclaration of enumerator ‘DIV’
   DIV = 289,
   ^~~
In file included from lexer.l:4:0:
./parser.tab.h:52:5: note: previous definition of ‘DIV’ was here
      DIV = 262,
      ^~~
lexer.l:12:2: error: redeclaration of enumerator ‘ABS’
   ABS = 290,
   ^~~
In file included from lexer.l:4:0:
./parser.tab.h:53:5: note: previous definition of ‘ABS’ was here
      ABS = 263,
      ^~~
lexer.l:13:2: error: redeclaration of enumerator ‘EOL’
   EOL = 291
   ^~~
In file included from lexer.l:4:0:
./parser.tab.h:54:5: note: previous definition of ‘EOL’ was here
      EOL = 264
      ^~~



_______________________________________________
[email protected] https://lists.gnu.org/mailman/listinfo/help-bison
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.