文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

PostgreSQL中的​Rules有什么作用

2024-04-02 19:55

关注

本篇内容介绍了“PostgreSQL中的Rules有什么作用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

Flex输入文件由四部分组成:

%{
Declarations
%}
Definitions
%%
Rules
%%
User subroutines

Rules

在Flex的模式文件中,%%和%%之间的内容被称为规则(rules),每一行表示一条规则,每条规则由匹配模式(pattern)和 动作(action)组成。其中模式在前面,用正则表达式表示,动作在后面,即C代码。每当一个模式被匹配到时,后面的C代码将被执行。
Flex会将规则翻译成名为yylex的函数,该函数扫描输入文件(默认标准输入),当扫描到一个完整的、最长的、可以和某条规则的正则表达式所匹配的输入时,函数会执行此规则后面的C代码。如果代码中没有return语句,则执行完毕后,yylex会继续运行,开始下一轮的扫描和匹配。注意:当有多条规则的模式被匹配到时, yylex会优先选择匹配长度最长的那条规则,如果有匹配长度相等的规则,则选择排在最前面的那条规则。

PG中的规则定义如下:

%%
{whitespace}    {
                    //--------- 空白字符
                    //忽略,不作任何处理
                    
                }
{xcstart}        {
                    //--------- C风格注释
                    
                    //设置位置,以防注释中的语法错误
                    SET_YYLLOC();
                    //深度
                    yyextra->xcdepth = 0;
                    //进入xc状态
                    BEGIN(xc);
                    
                    //把斜杠星后的字符放回去
                    // 注意:"
                    //类似的,把之后的字符放回去
                    yyless(2);
                }
<xc>{xcstop}    {
                    //层次≤0,回到INITIAL状态,否则层次减1
                    if (yyextra->xcdepth <= 0)
                        BEGIN(INITIAL);
                    else
                        (yyextra->xcdepth)--;
                }
<xc>{xcinside}    {
                    //注释里面的内容,忽略
                    
                }
<xc>{op_chars}    {
                    //注释里面的内容,忽略
                    
                }
<xc>\*+            {
                    //注释里面的内容,忽略
                    
                }
<xc><<EOF>>        { yyerror("unterminated 
                    //--------- 二进制位串
                    //在某些点上,我们应该简单的把字符串向前传递给解析器并标记它
                    //在此期间,设置一个打头的字符"b"以标记该输入为二进制串
                    SET_YYLLOC();
                    BEGIN(xb);
                    startlit();
                    addlitchar('b', yyscanner);
                }
<xb>{quotestop}    |
<xb>{quotefail} {
                    yyless(1);
                    BEGIN(INITIAL);
                    yylval->str = litbufdup(yyscanner);
                    return BCONST;
                }
<xh>{xhinside}    |
<xb>{xbinside}    {
                    addlit(yytext, yyleng, yyscanner);
                }
<xh>{quotecontinue}    |
<xb>{quotecontinue}    {
                    
                }
<xb><<EOF>>        { yyerror("unterminated bit string literal"); }
{xhstart}        {
                    //------------- 十六进制串
                    
                    SET_YYLLOC();
                    BEGIN(xh);
                    startlit();
                    addlitchar('x', yyscanner);
                }
<xh>{quotestop}    |
<xh>{quotefail} {
                    yyless(1);
                    BEGIN(INITIAL);
                    yylval->str = litbufdup(yyscanner);
                    return XCONST;
                }
<xh><<EOF>>        { yyerror("unterminated hexadecimal string literal"); }
{xnstart}        {
                    //------------- 国家字符
                    
                    const ScanKeyword *keyword;
                    SET_YYLLOC();
                    yyless(1);    
                    keyword = ScanKeywordLookup("nchar",
                                                yyextra->keywords,
                                                yyextra->num_keywords);
                    if (keyword != NULL)
                    {
                        yylval->keyword = keyword->name;
                        return keyword->value;
                    }
                    else
                    {
                        
                        yylval->str = pstrdup("n");
                        return IDENT;
                    }
                }
{xqstart}        {
                    yyextra->warn_on_first_escape = true;
                    yyextra->saw_non_ascii = false;
                    SET_YYLLOC();
                    if (yyextra->standard_conforming_strings)
                        BEGIN(xq);
                    else
                        BEGIN(xe);
                    startlit();
                }
{xestart}        {
                    yyextra->warn_on_first_escape = false;
                    yyextra->saw_non_ascii = false;
                    SET_YYLLOC();
                    BEGIN(xe);
                    startlit();
                }
{xusstart}        {
                    SET_YYLLOC();
                    if (!yyextra->standard_conforming_strings)
                        ereport(ERROR,
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                 errmsg("unsafe use of string constant with Unicode escapes"),
                                 errdetail("String constants with Unicode escapes cannot be used when standard_conforming_strings is off."),
                                 lexer_errposition()));
                    BEGIN(xus);
                    startlit();
                }
<xq,xe>{quotestop}    |
<xq,xe>{quotefail} {
                    yyless(1);
                    BEGIN(INITIAL);
                    
                    if (yyextra->saw_non_ascii)
                        pg_verifymbstr(yyextra->literalbuf,
                                       yyextra->literallen,
                                       false);
                    yylval->str = litbufdup(yyscanner);
                    return SCONST;
                }
<xus>{quotestop} |
<xus>{quotefail} {
                    
                    yyless(1);
                    
                    BEGIN(xusend);
                }
<xusend>{whitespace} {
                    
                }
<xusend><<EOF>> |
<xusend>{other} |
<xusend>{xustop1} {
                    
                    yyless(0);
                    BEGIN(INITIAL);
                    yylval->str = litbuf_udeescape('\\', yyscanner);
                    return SCONST;
                }
<xusend>{xustop2} {
                    
                    BEGIN(INITIAL);
                    if (!check_uescapechar(yytext[yyleng - 2]))
                    {
                        SET_YYLLOC();
                        ADVANCE_YYLLOC(yyleng - 2);
                        yyerror("invalid Unicode escape character");
                    }
                    yylval->str = litbuf_udeescape(yytext[yyleng - 2],
                                                   yyscanner);
                    return SCONST;
                }
<xq,xe,xus>{xqdouble} {
                    addlitchar('\'', yyscanner);
                }
<xq,xus>{xqinside}  {
                    addlit(yytext, yyleng, yyscanner);
                }
<xe>{xeinside}  {
                    addlit(yytext, yyleng, yyscanner);
                }
<xe>{xeunicode} {
                    pg_wchar    c = strtoul(yytext + 2, NULL, 16);
                    check_escape_warning(yyscanner);
                    if (is_utf16_surrogate_first(c))
                    {
                        yyextra->utf16_first_part = c;
                        BEGIN(xeu);
                    }
                    else if (is_utf16_surrogate_second(c))
                        yyerror("invalid Unicode surrogate pair");
                    else
                        addunicode(c, yyscanner);
                }
<xeu>{xeunicode} {
                    pg_wchar    c = strtoul(yytext + 2, NULL, 16);
                    if (!is_utf16_surrogate_second(c))
                        yyerror("invalid Unicode surrogate pair");
                    c = surrogate_pair_to_codepoint(yyextra->utf16_first_part, c);
                    addunicode(c, yyscanner);
                    BEGIN(xe);
                }
<xeu>.            { yyerror("invalid Unicode surrogate pair"); }
<xeu>\n            { yyerror("invalid Unicode surrogate pair"); }
<xeu><<EOF>>    { yyerror("invalid Unicode surrogate pair"); }
<xe,xeu>{xeunicodefail}    {
                    ereport(ERROR,
                            (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
                             errmsg("invalid Unicode escape"),
                             errhint("Unicode escapes must be \\uXXXX or \\UXXXXXXXX."),
                             lexer_errposition()));
                }
<xe>{xeescape}  {
                    if (yytext[1] == '\'')
                    {
                        if (yyextra->backslash_quote == BACKSLASH_QUOTE_OFF ||
                            (yyextra->backslash_quote == BACKSLASH_QUOTE_SAFE_ENCODING &&
                             PG_ENCODING_IS_CLIENT_ONLY(pg_get_client_encoding())))
                            ereport(ERROR,
                                    (errcode(ERRCODE_NONSTANDARD_USE_OF_ESCAPE_CHARACTER),
                                     errmsg("unsafe use of \\' in a string literal"),
                                     errhint("Use '' to write quotes in strings. \\' is insecure in client-only encodings."),
                                     lexer_errposition()));
                    }
                    check_string_escape_warning(yytext[1], yyscanner);
                    addlitchar(unescape_single_char(yytext[1], yyscanner),
                               yyscanner);
                }
<xe>{xeoctesc}  {
                    unsigned char c = strtoul(yytext + 1, NULL, 8);
                    check_escape_warning(yyscanner);
                    addlitchar(c, yyscanner);
                    if (c == '\0' || IS_HIGHBIT_SET(c))
                        yyextra->saw_non_ascii = true;
                }
<xe>{xehexesc}  {
                    unsigned char c = strtoul(yytext + 2, NULL, 16);
                    check_escape_warning(yyscanner);
                    addlitchar(c, yyscanner);
                    if (c == '\0' || IS_HIGHBIT_SET(c))
                        yyextra->saw_non_ascii = true;
                }
<xq,xe,xus>{quotecontinue} {
                    
                }
<xe>.            {
                    
                    addlitchar(yytext[0], yyscanner);
                }
<xq,xe,xus><<EOF>>        { yyerror("unterminated quoted string"); }
{dolqdelim}        {
                    SET_YYLLOC();
                    yyextra->dolqstart = pstrdup(yytext);
                    BEGIN(xdolq);
                    startlit();
                }
{dolqfailed}    {
                    SET_YYLLOC();
                    
                    yyless(1);
                    
                    return yytext[0];
                }
<xdolq>{dolqdelim} {
                    if (strcmp(yytext, yyextra->dolqstart) == 0)
                    {
                        pfree(yyextra->dolqstart);
                        yyextra->dolqstart = NULL;
                        BEGIN(INITIAL);
                        yylval->str = litbufdup(yyscanner);
                        return SCONST;
                    }
                    else
                    {
                        
                        addlit(yytext, yyleng - 1, yyscanner);
                        yyless(yyleng - 1);
                    }
                }
<xdolq>{dolqinside} {
                    addlit(yytext, yyleng, yyscanner);
                }
<xdolq>{dolqfailed} {
                    addlit(yytext, yyleng, yyscanner);
                }
<xdolq>.        {
                    
                    addlitchar(yytext[0], yyscanner);
                }
<xdolq><<EOF>>    { yyerror("unterminated dollar-quoted string"); }
{xdstart}        {
                    SET_YYLLOC();
                    BEGIN(xd);
                    startlit();
                }
{xuistart}        {
                    SET_YYLLOC();
                    BEGIN(xui);
                    startlit();
                }
<xd>{xdstop}    {
                    char       *ident;
                    BEGIN(INITIAL);
                    if (yyextra->literallen == 0)
                        yyerror("zero-length delimited identifier");
                    ident = litbufdup(yyscanner);
                    if (yyextra->literallen >= NAMEDATALEN)
                        truncate_identifier(ident, yyextra->literallen, true);
                    yylval->str = ident;
                    return IDENT;
                }
<xui>{dquote} {
                    yyless(1);
                    
                    BEGIN(xuiend);
                }
<xuiend>{whitespace} {
                    
                }
<xuiend><<EOF>> |
<xuiend>{other} |
<xuiend>{xustop1} {
                    
                    char       *ident;
                    int            identlen;
                    yyless(0);
                    BEGIN(INITIAL);
                    if (yyextra->literallen == 0)
                        yyerror("zero-length delimited identifier");
                    ident = litbuf_udeescape('\\', yyscanner);
                    identlen = strlen(ident);
                    if (identlen >= NAMEDATALEN)
                        truncate_identifier(ident, identlen, true);
                    yylval->str = ident;
                    return IDENT;
                }
<xuiend>{xustop2}    {
                    
                    char       *ident;
                    int            identlen;
                    BEGIN(INITIAL);
                    if (yyextra->literallen == 0)
                        yyerror("zero-length delimited identifier");
                    if (!check_uescapechar(yytext[yyleng - 2]))
                    {
                        SET_YYLLOC();
                        ADVANCE_YYLLOC(yyleng - 2);
                        yyerror("invalid Unicode escape character");
                    }
                    ident = litbuf_udeescape(yytext[yyleng - 2], yyscanner);
                    identlen = strlen(ident);
                    if (identlen >= NAMEDATALEN)
                        truncate_identifier(ident, identlen, true);
                    yylval->str = ident;
                    return IDENT;
                }
<xd,xui>{xddouble}    {
                    addlitchar('"', yyscanner);
                }
<xd,xui>{xdinside}    {
                    addlit(yytext, yyleng, yyscanner);
                }
<xd,xui><<EOF>>        { yyerror("unterminated quoted identifier"); }
{xufailed}    {
                    char       *ident;
                    SET_YYLLOC();
                    
                    yyless(1);
                    
                    ident = downcase_truncate_identifier(yytext, yyleng, true);
                    yylval->str = ident;
                    return IDENT;
                }
{typecast}        {
                    SET_YYLLOC();
                    return TYPECAST;
                }
{dot_dot}        {
                    SET_YYLLOC();
                    return DOT_DOT;
                }
{colon_equals}    {
                    SET_YYLLOC();
                    return COLON_EQUALS;
                }
{equals_greater} {
                    SET_YYLLOC();
                    return EQUALS_GREATER;
                }
{less_equals}    {
                    SET_YYLLOC();
                    return LESS_EQUALS;
                }
{greater_equals} {
                    SET_YYLLOC();
                    return GREATER_EQUALS;
                }
{less_greater}    {
                    
                    SET_YYLLOC();
                    return NOT_EQUALS;
                }
{not_equals}    {
                    
                    SET_YYLLOC();
                    return NOT_EQUALS;
                }
{self}            {
                    SET_YYLLOC();
                    return yytext[0];
                }
{operator}        {
                    
                    int            nchars = yyleng;
                    char       *slashstar = strstr(yytext, "
                        if (slashstar > dashdash)
                            slashstar = dashdash;
                    }
                    else if (!slashstar)
                        slashstar = dashdash;
                    if (slashstar)
                        nchars = slashstar - yytext;
                    
                    if (nchars > 1 &&
                        (yytext[nchars - 1] == '+' ||
                         yytext[nchars - 1] == '-'))
                    {
                        int            ic;
                        for (ic = nchars - 2; ic >= 0; ic--)
                        {
                            char c = yytext[ic];
                            if (c == '~' || c == '!' || c == '@' ||
                                c == '#' || c == '^' || c == '&' ||
                                c == '|' || c == '`' || c == '?' ||
                                c == '%')
                                break;
                        }
                        if (ic < 0)
                        {
                            
                            do {
                                nchars--;
                            } while (nchars > 1 &&
                                 (yytext[nchars - 1] == '+' ||
                                  yytext[nchars - 1] == '-'));
                        }
                    }
                    SET_YYLLOC();
                    if (nchars < yyleng)
                    {
                        
                        yyless(nchars);
                        
                        if (nchars == 1 &&
                            strchr(",()[].;:+-*/%^<>=", yytext[0]))
                            return yytext[0];
                        
                        if (nchars == 2)
                        {
                            if (yytext[0] == '=' && yytext[1] == '>')
                                return EQUALS_GREATER;
                            if (yytext[0] == '>' && yytext[1] == '=')
                                return GREATER_EQUALS;
                            if (yytext[0] == '<' && yytext[1] == '=')
                                return LESS_EQUALS;
                            if (yytext[0] == '<' && yytext[1] == '>')
                                return NOT_EQUALS;
                            if (yytext[0] == '!' && yytext[1] == '=')
                                return NOT_EQUALS;
                        }
                    }
                    
                    if (nchars >= NAMEDATALEN)
                        yyerror("operator too long");
                    yylval->str = pstrdup(yytext);
                    return Op;
                }
{param}            {
                    SET_YYLLOC();
                    yylval->ival = atol(yytext + 1);
                    return PARAM;
                }
{integer}        {
                    SET_YYLLOC();
                    return process_integer_literal(yytext, yylval);
                }
{decimal}        {
                    SET_YYLLOC();
                    yylval->str = pstrdup(yytext);
                    return FCONST;
                }
{decimalfail}    {
                    
                    yyless(yyleng - 2);
                    SET_YYLLOC();
                    return process_integer_literal(yytext, yylval);
                }
{real}            {
                    SET_YYLLOC();
                    yylval->str = pstrdup(yytext);
                    return FCONST;
                }
{realfail1}        {
                    
                    yyless(yyleng - 1);
                    SET_YYLLOC();
                    yylval->str = pstrdup(yytext);
                    return FCONST;
                }
{realfail2}        {
                    
                    yyless(yyleng - 2);
                    SET_YYLLOC();
                    yylval->str = pstrdup(yytext);
                    return FCONST;
                }
{identifier}    {
                    //---------- 标识符
                    const ScanKeyword *keyword;
                    char       *ident;
                    SET_YYLLOC();
                    
                    //是否关键字?
                    keyword = ScanKeywordLookup(yytext,
                                                yyextra->keywords,
                                                yyextra->num_keywords);
                    if (keyword != NULL)
                    {
                        //是,则返回关键字值
                        yylval->keyword = keyword->name;
                        return keyword->value;
                    }
                    
                    //如果不是关键字,则设置为小写字母,如需要则截断
                    ident = downcase_truncate_identifier(yytext, yyleng, true);
                    yylval->str = ident;
                    return IDENT;
                }
{other}            {
                    SET_YYLLOC();
                    return yytext[0];
                }
<<EOF>>            {
                    SET_YYLLOC();
                    yyterminate();
                }
%%

“PostgreSQL中的Rules有什么作用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯