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:40:35 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.5.1

I've missed to remove some code from the lexer.l file:

%{

#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;

char *value = NULL;

%}

%%

"+"            { return ADD; }

"-"            { return SUB; }

"*"            { return MUL; }

"/"            { return DIV; }

"|"            { return ABS; }

[0-9]+        { yylval = atoi(yytext); return NUMBER; }

\n            { return EOL; }

[ \t]        { /* ignore whitespaces */ }

[a-zA-Z]    { printf("Unknow string :%s", yytext); }

.            { printf("Unknow char :%c", *yytext); }

%%

On 17.02.19 11:49, address@hidden wrote:
i'm new to Flex and Bison and are currently reading the book "Flex and Bison" and have a few troubles understanding things correctly.


First of all i've troubles compiling the first example that uses both, Flex and Bison. Flex compilation was easy but with Biton i've troubles, first the example source from the book:


*lexer.l:*

%{

#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;

char *value = NULL;

%}

%%

"+"            { return ADD; }

"-"            { return SUB; }

"*"            { return MUL; }

"/"            { return DIV; }

"|"            { return ABS; }

[0-9]+        { yylval = atoi(yytext); return NUMBER; }

\n            { return EOL; }

[ \t]        { /* ignore whitespaces */ }

[a-zA-Z]    { printf("Unknow string :%s", yytext); }

.            { printf("Unknow char :%c", *yytext); }

%%

int main(int argc, char **argv){

    int tok;

    while(tok = yylex()){

        printf("token = %d", tok);

        if(tok == NUMBER){

            printf(" = %d", yylval);

        }else{

            printf("\n");

        }

    }

    return(0);

}


*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);

}

yyerror(char *s){

    fprintf(stderr, "error: %s\n", s);

}

*And here is my 'MakeFile:*

test: lexer.l parser.tab.c parser.tab.c
    gcc parser.tabs.c lex.yy.c -lfl -o test

parser.tab.c: parser.tab.c paser.tab.c
    bison -d parser.y

lexer.l: lex.yy.c
    flex lexer.l

clean:
    rm -rf lex.yy.c test1.tab.* test

I can't get i compile and if i get i compile by hand i get errors ... please can someone help me out here,
the book doens't go to much into detail about this topic.

best reagards!

_______________________________________________
address@hidden https://lists.gnu.org/mailman/listinfo/help-bison



reply via email to

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