Hunter的大杂烩 技术学习笔记

2005-09-15

html模板引擎之yacc语法分析

Filed under: HHTE — hunter @ 11:51 am

%{
/*
* Copyright (c) 2005 Hunter (whywhathow at hotmail dot com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 4. Neither the name of the Hunter nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*!
* \file hhte_template.l
* \brief H html template engine’s yacc parser, some code from php3.
* \author HunterLiu
* \date 2005-9-15
* \version 1.1
*/

#include “hhte_struct.h”
#include “hhte_template.tab.h”
#include “h_html_template.h”
#include
#include

#define YYDEBUG 1
extern CTemplate g_cHTemplate;
extern void end_hhte();
//void yyerror(const char *s);
void yyerror(const char *s)
{
fprintf(stderr, “%s\n”, s);
}

%}

%union {
char *sval;
}

%token HHTE_LOOP
%token HHTE_ECHO
%token HHTE_DONE
%token END_HHTE
%token INLINE_HTML
%token HHTE_STRING

%type label
%type var

%error-verbose
%%

statement_list:
statement_list statement
| /* empty */
;
statement:
‘{‘ statement_list ‘}’
| INLINE_HTML {
boost::shared_ptr cSymbol(new CStringSymbol($1));
g_cHTemplate.getSymbolManager()->addSymbol(cSymbol);
free($1);
}
| HHTE_LOOP label {
boost::shared_ptr cSymbol(new CLoopSymbol($2));
g_cHTemplate.getSymbolManager()->addSymbol(cSymbol);
free($2);
}
statement_list HHTE_DONE { g_cHTemplate.getSymbolManager()->setState(SYMBOL_DONE); }
| HHTE_ECHO var end_statement {
boost::shared_ptr cSymbol(new CTagSymbol($2));
g_cHTemplate.getSymbolManager()->addSymbol(cSymbol);
free($2);
}
| END_HHTE { end_hhte(); }
;

end_statement:
‘;’
| END_HHTE { end_hhte(); }
;

var:
‘$’ HHTE_STRING { $$=$2; }
;

label: ‘:’HHTE_STRING {
$$=$2;
}
;

%%

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress