гибкий бизон windows введение - PullRequest
0 голосов
/ 21 февраля 2020

Поскольку я новичок в лексере и парсере, поэтому я пытаюсь прочитать и понять чужой код.

Вот код, который я пытаюсь использовать: https://gist.github.com/justjkk/436828

Но это дает мне ошибку. Как я могу решить это?

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>

Или они должны остаться как есть.

Все команды, которые я дал:

flex json.l
bison -dy json.y
gcc lex.yy.c y.tab.c -o json.exe

1 Ответ

2 голосов
/ 21 февраля 2020

Просто:

#include <string.h>

в вашем разделе определения флекса поверх json.l должно исправить это для вас.

В хранилище, на которое вы указали, также есть Makefile. Может быть, вы должны использовать это. Вы, кажется, не генерируете файлы анализатора должным образом. См. Комментарий ниже.

Что касается остальных предупреждений:

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

Это можно легко исправить, добавив объявления yylex() и yyerror должны присутствовать в прологе bison раздел в верхней части вашего json.y:

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

Что касается следующих:

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

Они немного более тонкие. Я бы посоветовал взглянуть здесь о том, как использовать yylval для правильной передачи строк из токенов lex в действия парсера. Проблема в том, что yylval - это пустое int, но в итоге ему назначаются char указатели для токенов NUMBER и STRING.

...