나는이 오류 얻을은 yacc에 & 렉스를 내 코드를 컴파일하려고 : 사용 yytext에서
은 yacc 번호 :
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define YYSTYPE struct node*
typedef struct node{
char *token;
struct node *left;
struct node *right;
} node;
node* mknode(char* token, node *left, node *right);
void Printtree(node *tree);
int yyerror();
%}
%token NUM PLUS MINUS
%left PLUS MINUS
%%
S:exp {printf("OK\n"); Printtree($1);};
exp:exp PLUS exp {$$=mknode("+",$1,$3);}
| exp MINUS exp{$$=mknode("-",$1,$3);}
| NUM {$$=mknode(yytext, NULL, NULL);};
%%
#include "lex.yy.c"
int main(){
return yyparse();
}
node *mknode (char *token, node *left, node *right){
node *newnode = (node*)malloc(sizeof(node));
char *newstr = (char*)malloc(sizeof(token)+1);
strcpy (newstr, token);
newnode->left=left;
newnode->right=right;
newnode->token=newstr;
return newnode;
}
void Printtree(node* tree){
printf("%s\n", tree->token);
if (tree->left)
Printtree(tree->left);
if (tree->right)
Printtree(tree->right);
}
int yyerror(){
printf("ERROR\n");
return 0;
}
와 렉스 코드
%%
[0-9]+ return NUM;
\+ return PLUS;
\- return MINUS;
%%
내가 $ 1로 yytext를 변경하려고 할 때 컴파일되지만 코드를 실행하고 예를 들어 5 + 6을 말하면 : (세분화 오류 (코어 덤프 됨))
우분투 (64)를 사용하여 :
렉스 플렉스 버전 렉스 2.6.0 컴파일 :
yacc subProject.yacc
:
lex subProject.lex
와 Yacc에 들소 버전 들소 (GNU 들소) 3.0.4 컴파일
및 오류 메이커 :
cc subProject -o y.tab.c -ll -Ly
여기에 텍스트에 대한 링크 또는 그림을 게시하지 마십시오. 텍스트를 게시하십시오. 그것은 내가 신경 쓰지 않는 시간의 완전한 낭비이며 우리의 대역폭입니다. – EJP
당신이 실제로 사용하고있는 'yacc'가 실제로 들소인가요? 'yacc --version'에서 무엇을 얻습니까? 플렉스를 실제로 사용하고있는 '렉스'입니까? 'lex - 버전'에서 무엇을 얻습니까? 그것은 중요 할 수 있습니다. 원래 AT & T Yacc와 Lex는 Bison과 Flex와 약간 다릅니다 (이 Flex의 태그는 [tag : flex-lexer]가 아니고 [tag : flex]이고 다른 하나는 Adobe Flex 용임). 그리고 하나의 차이점은'yytext'의 유형입니다. 그러나 선언은 별도의 문제 일 수 있습니다. 단, 올바른 선언을 사용하는 것이 중요합니다. –
나는 flex와 bison을 사용하여 그 글을 썼지 만 지금은 버전을 추가했다. –