Re: Hi everyone,

"[email protected]" <[email protected]>
Newsgroups gmane.comp.parsers.bison.general
Message-ID <[email protected]>
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, [email protected] 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!
>
> _______________________________________________
> [email protected] https://lists.gnu.org/mailman/listinfo/help-bison

_______________________________________________
[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.