0

As I'm new to lexer and parser, so I'm trying to read and understand others code.

Here is the code i'm trying to use : https://gist.github.com/justjkk/436828

But it's giving me error. How can I resolve this?

E:\flex_bison_test>gcc lex.yy.c y.tab.c -o json.exe json.l: In function 'yylex': json.l:34:11: warning: assignment to 'YYSTYPE' {aka 'int'} from 'char *' makes integer from pointer without a cast [-Wint-conversion] yylval=strclone(yytext); ^ json.l:38:11: warning: assignment to 'YYSTYPE' {aka 'int'} from 'char *' makes integer from pointer without a cast [-Wint-conversion] yylval=strclone(yytext); ^ json.l: In function 'strclone': json.l:82:15: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration] int len = strlen(str); ^~~~~~ json.l:82:15: warning: incompatible implicit declaration of built-in function 'strlen' json.l:82:15: note: include '<string.h>' or provide a declaration of 'strlen' json.l:79:1: +#include <string.h> %% json.l:82:15: int len = strlen(str); ^~~~~~ json.l:84:5: warning: implicit declaration of function 'strcpy' [-Wimplicit-function-declaration] strcpy(clone,str); ^~~~~~ json.l:84:5: warning: incompatible implicit declaration of built-in function 'strcpy' json.l:84:5: note: include '<string.h>' or provide a declaration of 'strcpy' y.tab.c: In function 'yyparse': y.tab.c:627:16: warning: implicit declaration of function 'yylex' [-Wimplicit-function-declaration] # define YYLEX yylex () ^~~~~ y.tab.c:1272:16: note: in expansion of macro 'YYLEX' yychar = YYLEX; ^~~~~ y.tab.c:1540:7: warning: implicit declaration of function 'yyerror'; did you mean 'yyerrok'? [-Wimplicit-function-declaration] yyerror (YY_("syntax error")); ^~~~~~~ yyerrok json.y: At top level: json.y:80:6: warning: conflicting types for 'yyerror' void yyerror (char const *s) { ^~~~~~~ y.tab.c:1540:7: note: previous implicit declaration of 'yyerror' was here yyerror (YY_("syntax error")); ^~~~~~~ E:\flex_bison_test> 

Or these should remain as it is.

All the commands, I have given :

flex json.l bison -dy json.y gcc lex.yy.c y.tab.c -o json.exe 
1
  • 2
    Honestly, I think you'd be better off studying the examples in the flex manual and the bison manual. It's true (and unfortunate) that these examples don't show how bison and flex can be used together, but they do show how to use both tools. Most importantly, the manuals explain the examples. The code you link here is simply not a good basis for using flex and bison, and the fact that it doesn't compile without errors is just one obvious symptom. Commented Feb 21, 2020 at 14:38

1 Answer 1

2

Simply:

#include <string.h> 

in your flex definitions section on top of json.l should fix it for you.

There's also a Makefile in the repository you pointed to. Maybe you should use that. You don't seem to be generating the parser files properly. See comment below.

As for the remaining warnings:

warning: implicit declaration of function 'yyerror'; warning: implicit declaration of function 'yylex'; 

These can be easily fixed by adding declarations of yylex() and yyerror should be present in the bison prologue section at the top of your json.y:

%{ int yylex(); void yyerror(const char *s); %} 

As for these ones:

json.l:34:11: warning: assignment to 'YYSTYPE' {aka 'int'} from 'char *' makes integer from pointer without a cast [-Wint-conversion] yylval=strclone(yytext); ^ json.l:38:11: warning: assignment to 'YYSTYPE' {aka 'int'} from 'char *' makes integer from pointer without a cast [-Wint-conversion] yylval=strclone(yytext); ^ 

They're a bit more subtle. I would suggest have a look here on how to use yylval for correctly passing on strings from the lex's tokens into the parser's actions. The problem now is that yylval is a bare int but it ends up being assigned char pointers for both NUMBER and STRING tokens.

Sign up to request clarification or add additional context in comments.

5 Comments

@Danial The remaining ones are warnings. That -dy flag in your bison invocation seems odd - Notice that you're generating y.tab.{c,h} but your lexer needs #include "json.tab.h"
@dragosht: The warning about yylval having the wrong type in the lexer needs to be taken seriously. Also, forward declarations of yylex and yyerror are required in the bison prologue.
@rici I was just about to suggest adding yylex and yyerror into bison. As for those yylval assignments - I can't really say what all that's about ...
@dragosht: it's from only redefining YYSTYPE in the bison file. But modern bison has a better way to declare semantic type which automatically puts the declaration into the generated header file. See Defining Language Semantics in the manual.
@rici I would refer to this post here on the topic of properly using yylval.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.