我目前正在使用Flex/Bison開發HTML解析器/模板庫。我在if語句中遇到了一些問題。表達式解析得很好(如果你>我),但是當涉及到標籤之間的開始和結束之間的語句時,它只會獲得第一個詞並在它們之間的空間中死去。只是想知道如何確保我獲得標籤之間的所有內容,而不是在遇到的第一個空間中死掉。Flex/Bison的空間問題
我基本上做的是用變量({{var}})的新值和來自語句的結果(如{%if表達式%} blah {%endif%})(即.djangoish)重建文件
輸出
you > me
If Statement:
if(you) {
Do
}
示例模板
%{
#include <stdio.h>
#include "ink.tab.h"
using namespace std;
extern "C" {
int yyparse(void);
int yylex(void);
int yywrap();
}
extern void yyerror(char *err);
extern int LINENO;
const char *context;
%}
%%
/* Open/Close template tags */
"{{" { return OPENPRINT; }
"}}" { return CLOSEPRINT; }
"{%" { return OPENACTION; }
"%}" { return CLOSEACTION; }
/* Conditionals */
"!" { return BANG; }
"<" { return LT; }
">" { return GT; }
"==" { return EQ; }
"!=" { return NEQ; }
"<=" { return LTEQ; }
">=" { return GTEQ; }
"&&" { return ANDOP; }
"||" { return OROP; }
/* IF/ELSE handler */
"if" { return IF_TOKEN; }
"else" { return ELSE_TOKEN; }
"endif" { return ENDIF_TOKEN; }
/* FOR handler */
"for" { return FOR_TOKEN; }
"in" { return IN_TOKEN; }
"endfor" { return ENDFOR_TOKEN; }
/* Context grab */
[a-zA-Z0-9_]* { yylval.strval = strdup(yytext); return CONTEXT; }
/* Excuse the HTML tags */
\<[^>]*\> { context = strdup(yytext); fwrite(context, sizeof(char), strlen(context), yyout); }
/* Some catch alls */
[ \t]+ { context = strdup(yytext); fwrite(context, sizeof(char), strlen(context), yyout); }
\n { LINENO++; context = "\n"; fwrite(context, sizeof(char), strlen(context), yyout); }
. ;
%%
void yyerror(char *err)
{
printf("\nLine:\t%d\nError:\t%s\nText:\t%s\n", LINENO, err, yytext);
}
int yywrap()
{
return 1;
}
int main()
{
yyout = fopen("test.out", "w");
yyin = fopen("test.jhtml", "r");
yyparse();
}
野牛
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <v8.h>
using namespace std;
using namespace v8;
extern "C" {
int yyparse(void);
int yylex(void);
int yywrap();
}
int LINENO = 1;
void yyerror(char *err);
extern FILE *yyout;
%}
%union {
int inval;
char *strval;
}
%token OPENPRINT
%token CLOSEPRINT
%token OPENACTION
%token CLOSEACTION
%token <strval> CONTEXT
%token IF_TOKEN
%token ELSE_TOKEN
%token ENDIF_TOKEN
%token FOR_TOKEN
%token IN_TOKEN
%token ENDFOR_TOKEN
%token TAGS
%token BANG
%token LT
%token GT
%token EQ
%token NEQ
%token LTEQ
%token GTEQ
%token ANDOP
%token OROP
%type <strval> context
%type <strval> expression
%type <strval> contexts
%%
commands:
command
|
commands command
;
command:
OPENPRINT echo CLOSEPRINT
|
expression
|
stmt
;
echo:
context {
char *context = $1;
fwrite(context, sizeof(char), strlen(context), yyout);
}
;
stmt:
OPENACTION IF_TOKEN expression CLOSEACTION contexts OPENACTION ENDIF_TOKEN CLOSEACTION {
printf("\nIf Statement: \n");
printf("if(%s) {\n\t%s\n}\n", $3, $5);
}
;
contexts:
context
|
contexts context
;
context:
CONTEXT { $$ = $1; }
;
expression:
context
|
context GT context { printf("\n%s > %s\n", $1, $3); }
|
context LT context {}
|
context EQ context {}
|
context NEQ context {}
|
context LTEQ context {}
|
context GTEQ context {}
|
context ANDOP context {}
|
context OROP context {}
|
BANG context {}
;
%%
例HTML模板
{% if you > me %}
Do something here
{% endif %}
這裏有問題嗎?語法和詞法分析器編譯良好,並接受示例輸入就好了,產生如預期的輸出... – 2013-05-06 23:28:19