武汉大学编译原理第4次作业

/*************************************************************************
	> 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);
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值