/*************************************************************************
> File Name: xml.y
> Created Time: 2015年11月16日 星期一 19时46分36秒
************************************************************************/
%{
#include <string.h>
#include <stdio.h>
#include "xml_tree.h"
void yyerror(const char *);
extern char * yytext;
extern int yylineno;
#define YYDEBUG_LEXER_TEXT ""
%}
%union {
Xml_tree *node;
List *list;
};
%token <node>STAG ETAG TEXT
%type <list>tag_list
%%
start : tag_list {
print_tree(0, $1);
}
;
tag_list : STAG tag_list ETAG tag_list {
char* result= "";
strcat(result,'<');
strcat(result,$1->tag_name);
strcat(result,'>');
strcat(result,$2->node->tag_name);
strcat(result,'<');
strcat(result,$3->tag_name);
strcat(result,'>');
strcat(result,$4->node->tag_name);
$$ = result;
}
| STAG tag_list error tag_list{
char* result= "";
strcat(result,'<'); /* 匹配到开始标签 */
strcat(result,$1->tag_name);
strcat(result,'>');
strcat(result,$2->node->tag_name);/* 添加孩子 */
strcat(result,'<');
strcat(result,$1->tag_name);/* 添加同级标签 */
strcat(result,'>');
strcat(result,$4->node->tag_name);
$$ = result;
}
| TEXT tag_list {
char* result= ""; /* 处理标签不匹配问题 , 添加容错*/
strcat(result,$1->tag_name);
strcat(result,$2->node->tag_name);
$$ = result;
}
| /* empty */ {
$$ = NULL;/* 匹配到结束标签 直接返回空 */
}
;
%%
int main()
{
yyparse();
return 0;
}
void yyerror(const char *s)
{
printf("%s\n");
}
/*************************************************************************
> File Name: xml.y
> Author: 秦贤康
> Mail: qinxiankang@gmail.com
> Created Time: 2015年11月16日 星期一 19时46分36秒
************************************************************************/
%{
#include <string.h>
#include <stdio.h>
#include "xml_tree.h"
void yyerror(const char *);
extern char * yytext;
extern int yylineno;
#define YYDEBUG_LEXER_TEXT ""
%}
%union {
Xml_tree *node;
List *list;
};
%token <node>STAG ETAG TEXT
%type <list>tag_list
%%
start : tag_list {
print_tree(0, $1);
}
;
tag_list : STAG tag_list ETAG tag_list { /* < tag_list /> tag_list */
/* 匹配到开始标签 */
Xml_tree list = make_list(tag_list);
/* 添加孩子 */
add_child(tag_list, Xml_tree);
/* 添加同级标签 */
add_list(list,tag_list);
}
| STAG tag_list error tag_list{ /* < tag_list error tag_list */
/* 处理标签不匹配问题 , 添加容错?*/
Xml_tree list = make_list(tag_list);
Error(error);
add_list(list,tag_list);
}
| TEXT tag_list { /* text tag_list */
list = make_list(yylval);
add_list(list, tag_list);
}
| /* empty */ {
/* 匹配到结束标签 直接返回空 */
$$ = NULL;
}
;
%%
int main()
{
yyparse();
return 0;
}
void yyerror(const char *s)
{
printf("%s\n",s);
}