【发布时间】:2014-04-29 04:55:38
【问题描述】:
我正在尝试创建一棵树。基本思想是每当我看到“+”、“-”、“/”或“*”时,该变量就会成为主树的父级。例如,如果用户输入 1 + 1 ,树应该看起来像
+
1 1
请看一下我的插入函数。我写了一个 sudo 代码,但我不知道如何编码。我对双指针感到不舒服。如果可以的话,请逐行解释,以便我跟进。谢谢
这是我的代码
%{
#include <stdio.h>
#define true 1
#define false 0
int lookahead;
int lookahead1;
int error;
int count;
%}
// LEX codes
%%
[(] {return(111);}
[)] {return(222);}
[0-9] {return(333);}
[a-zA-Z] {return(444);}
[\+] {return(33);}
[\*] {return(44);}
[\/] {return(55);}
[\-] {return(66);}
[ \n\t] {return(0);}
%%
// C code
struct node
{
char a;
struct node * left;
struct node * right;
};
struct node *root, * current;
// inserting into tree
void insert( struct node ** tree, struct node *item)
{
printf("%c", item -> a);
if (!(*tree))
{
*tree = item;
return;
}
// this is where I need help
if ( item-> a == '+' || item-> a == '-' || item->a == '*' || item->a == '/' )
{
item -> left = tree;
break;
/* struct node ** temp;
temp= tree;
*tree = item;
(*tree) -> left = *temp;
*/
//insert (&(*tree) -> left, item);
}
else if ( (*tree) -> left == NULL )
{
insert (&(*tree) -> left, item);
}
else
{
insert (&(*tree) -> right, item);
}
}
// c code ends here
// lex code
Goal ()
{
return Expr();
}
Expr()
{
if ( Term() )
return ExprP();
else
return false;
}
ExprP()
{
if ( lookahead == 33 || lookahead == 66 )
{
current = malloc(sizeof(struct node));
current -> a = yytext[0];
current -> left = NULL;
current -> right = NULL;
insert(&root, current);
lookahead = yylex();
if ( Term() )
return ExprP();
else
return false;
}
else if ( lookahead == 222 || lookahead == 0 )
return true;
else
return false;
}
Term()
{
if (Factor())
{
return TermP();
}
else
return false;
}
TermP()
{
if ( lookahead == 44 || lookahead == 55 )
{
current = malloc(sizeof(struct node));
current -> a = yytext[0];
current -> left = NULL;
current -> right = NULL;
insert(&root, current);
lookahead = yylex();
if ( Factor() )
{
return TermP();
}
else
return false;
}
else if ( lookahead == 33 || lookahead == 222 || lookahead == 0 || lookahead == 66 )
return true;
else
return false;
}
Factor()
{
if (lookahead == 111 )
{
lookahead = yylex();
if ( !Expr() )
return false;
if ( lookahead != 222 )
return false;
lookahead = yylex();
return true;
}
else if ( lookahead == 333 )
{
current = malloc(sizeof(struct node));
current -> a = yytext[0];
current -> left = NULL;
current -> right = NULL;
insert(&root, current);
lookahead = yylex();
return true;
}
else
return false;
}
int yywrap(void)
{
return 1;
}
void printout(struct node * tree, int h) {
if(tree->right) printout(tree->right, h+1);
int i ;
for ( i = 0; i < h ; i++)
printf(" ");
printf("%c\n",tree->a);
if(tree->left) printout(tree->left, h+1);
}
// maing in c code
int main()
{
root = NULL;
lookahead = yylex();
if ( Goal() )
printf ("\n\nThe string you entered successfully accepted\n\n");
else
{
printf("\n\nThe string you entered failed to accept\n\n");
printf("%s\n", yytext);
}
printout(root, 0);
return 0;
}
【问题讨论】:
-
所以……你写了一些代码,但你不明白你写了什么?还是我误解了什么?顺便说一句,这里看起来像一些
gnuflex代码... -
请准确指出您使用的语言。这看起来可能是 flex 输入 - 但绝对不是普通的 c 或 c++。
-
您好 Chnossos,我确实了解我的代码,但我对双指针不太满意。我似乎无法正确编码的部分是插入功能。我做了一个笔记说我需要帮助。您好 Gha.st,我对我使用的语言发表了一些评论。我在我的程序中同时使用了 lex 和 C 代码。