help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Hi everyone,


From: address@hidden
Subject: Re: Hi everyone,
Date: Sun, 17 Feb 2019 12:57:03 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.5.1

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
     ^~~





reply via email to

[Prev in Thread] Current Thread [Next in Thread]