术语"*"的意思是"*".在lex [英] Meaning of "<*>" in lex

查看:196
本文介绍了术语"*"的意思是"*".在lex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,我们可以在lex中定义一些条件,匹配:

I know,we can define some conditions in lex, matching:

1.<DIRECTIVE>{STRING} {printf("Matching the DIRECTIVE state!");}
2.<REFERENCE>{INTEGER} {printf("Matching the REFERNCE state!");}
3.[\n] {printf("Matching the INITIAL state?");}

4.<*>{DOBULE} {printf("Matching all state include INITIAL? Seem not!");}

如何正确使用状态?第3行和第4行的条件有什么区别?

How to use the states in the right way? What is the difference in conditions on line 3 and 4?

由我剪切的整个.l文件,现在只是为了实现参考.我运行它时可以很好地工作,但是最后总是说"line:4:error:语法错误",我不知道不是为什么!我的test.vm只有两行文字.

The whole .l file, cut by me,now it just to realize a reference.When I run it,it can work well,but it always says "line:4: error: syntax error" at last,I don't why! My test.vm has only 2 lines text.

%{
/**支持了所有的token,不支持转义和多行注释*/
#include<stdio.h>
#include<string.h>
#include "context.h"
#include "bool.h"
#include "vtl4.tab.h"

extern int yylex();
/**bracket标志*/
int bracket_flag = 0;
/**引用标志*/
int ref_flag = 0;
/**多行注释标记*/
int mul_comment_flag = 0;
%}
%option stack
%option noyywrap yylineno

%x REF
VAR_NAME ([_a-zA-Z]+[a-zA-Z0-9_\-]*)
%%
/**这里去除$#\n防止覆盖状态REF和DIRECTIVE*/
[^$\n#]*? {printf("text:%s\n",yytext);yylval.string = yytext; return CONTENT;}
/**换行单独取出来,还没清楚为什么*/
\n {printf("newLine:%s\n",yytext);yylval.string = yytext; return CONTENT;}

/**添加^$防止只匹配最后一个变量
 例如:<p class="$b">$a $b</p> 只匹配了最后一个$b
 */
[^#$]*?/"$" {BEGIN REF;printf("begin ref text:%s\n",yytext);yylval.string = yytext; return       CONTENT;}

<REF>"$"|"$!"/"{"?{VAR_NAME} {ref_flag++;printf("$:%s\n",yytext);return DOLLAR;}
<REF>"{" {printf("{:%s\n",yytext);return BRACE;}
<DIRECTIVE>"}" {printf("}:%s\n",yytext);return BRACE_CLOSE;}
<REF>{VAR_NAME}/[^0-9A-Za-z_\-] {
    printf("ref name:%s\n",yytext);
    ref_flag--;
    yylval.sym = find_symbol(yytext);
    return ID;
}
<REF>[ /t"="\n] {BEGIN INITIAL; printf("ref end:%s\n",yytext);}
<REF>[}] {printf("}:%s\n",yytext);return BRACE_CLOSE;}

<<EOF>> {printf("lex end:%s\n",yytext);return LEX_EOF;}
%%

整个.y文件:

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bool.h"
#include "parser.h"
#include "context.h"
#include "vtl4.tab.h"

extern FILE * yyin;
extern FILE * yyout;
int yylex();
#define YYDEBUG 1
%}
/*priority level*/
%union {
struct simpleNode *ast;
double d;
int i;
bool b;
char* string;
struct symbol *sym;
}

%token ADD SUB MUL DIV MOD EQUAL PARENTHESIS CLOSE_PARENTHESIS BRACE BRACE_CLOSE  LOGICAL_AND LOGICAL_OR LOGIC_EQUAL LOGICAL_LT LOGICAL_LE LOGICAL_GT LOGICAL_GE LOGICAL_NOT_EQUALS

%token  LEX_EOF

%token <string> CONTENT STRING_LITERAL SINGLE_LINE_COMMENT MULTI_LINE_COMMENT
%token <b> BOOL

%token INTEGER_LITERAL

%token <d> DOUBLE_LITERAL

%token DOLLAR

%token <sym> ID

%token HASH SET PARSE IF ELSE ELSEIF FOREACH IN END

%type <ast> root statements statement reference content

%start root

%%
/*JJTPROCESS*/
root:statements LEX_EOF {printf("yacc root\n");$$ = process($1);}
;

statements
: statement {printf("yacc statements:statement\n"); $$ = $1; }
| statements statement {printf("yacc statements:statements statement\n"); $$ =     add_ybrother($1,$2);}
;

statement
 : reference {printf("yacc statement:ref\n"); $$ = $1;}
| content
;

reference
: DOLLAR ID {printf("yacc ref\n");$$ = reference($2);}
 ;

content
: CONTENT {$$ = text(NULL);}
;

%%
int main(){
printf("BEGIN:\n");
FILE *src;
src = fopen("test.vm","r");
yyin = src;
int result = yyparse();
fclose(src);
return result;
}

推荐答案

首先,启动条件不是Lex的功能.它们是GNU Flex的功能.

Firstly, start conditions are not a feature of Lex. They are a feature of GNU Flex.

<*>语法确实被记录为含义此规则将在任何状态下无条件触发".

The syntax <*> is indeed documented as meaning "this rule will fire unconditionally in any state".

您可能知道,条件可以是排他的(用%x定义)或包含的(用%s定义).

As you probably know, conditions can be exclusive (defined with %x) or inclusive (defined with %s).

如果编写的规则没有任何条件,则该规则在INITIAL状态下以及在所有包含状态下均处于活动状态:包含表示指定不指定任何条件的规则在该状态下将处于活动状态".

If you write a rule without any conditions, then it is active in the INITIAL state, and also in any states which are inclusive: inclusive means "rules which specify no condition will be active in this state".

如果当前状态为排他状态,则没有任何条件的规则将不起作用:它将排除没有条件的规则.

Rules without any conditions are not active if the current state is an exlusive one: it excludes rules that have no conditions.

<*>语法意味着该规则将在所有状态(包括排他状态)下激活.

The <*> syntax means that the rule will activate in all states, including the exlusive ones.

如果规则不具有<...>条件语法,则表示该规则仅在INITIAL状态或包含以下状态的状态激活.

If a rule has no <...> condition syntax, it means that the rule will activate only in the INITIAL state, or states that are inclusive.

在解析器中仍然有很多错误.请记住,最长的匹配规则是在输入中的任何一点触发的规则,并且可能被误解为开始条件不起作用.如果输入为catalog并且某些符合条件的规则想要匹配cat,则与c匹配的规则被赋予<*>没关系.这不是最长的比赛.

Plenty can still go wrong in your parser. Remember that the longest matching rule is the one that is triggered at any point in the input, and that can be misinterpreted as start conditions not working. If the input is catalog and some eligible rule wants to match cat, it doesn't matter that the rule which matches c is attributed with <*>. It's not the longest match.

这篇关于术语"*"的意思是"*".在lex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆