这篇文章主要介绍“PostgreSQL中Declarations的作用是什么”,在日常操作中,相信很多人在PostgreSQL中Declarations的作用是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”PostgreSQL中Declarations的作用是什么”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

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

%{Declarations%}Definitions%%Rules%%UsersubroutinesDeclarations

由%{和%}包含的部分为Declarations部分,这一部分都是C代码,会原封不动的copy到lex.yy.c文件中.
比较重要的定义包括:
YYSTYPE-Bison使用一个union联合体来存储所有可能类型的值,全局变量yyvalue的类型是YYSTYPE.

%top{/*-------------------------------------------------------------------------**scan.l*lexicalscannerforPostgreSQL*PostgreSQL的词法扫描器**NOTENOTENOTE:*特别特别特别注意:*Therulesinthisfilemustbekeptinsyncwithsrc/fe_utils/psqlscan.l!*这个文件中的规则必须与src/fe_utils/psqlscan.l文件中的规则保持一致!!!**Therulesaredesignedsothatthescannerneverhastobacktrack,*inthesensethatthereisalwaysarulethatcanmatchtheinput*consumedsofar(theruleactionmayinternallythrowbacksomeinput*withyyless(),however).Asexplainedintheflexmanual,thismakes*forausefulspeedincrease---aboutathirdfasterthanaplain-CF*lexer,insimpletesting.Theextracomplexityismostlyintherules*forhandlingfloatnumbersandcontinuedstringliterals.Ifyouchange*thelexicalrules,verifythatyouhaven'tbrokentheno-backtrack*propertybyrunningflexwiththe"-b"optionandcheckingthatthe*resulting"lex.backup"filesaysthatnobackingupisneeded.(Asof*Postgres9.2,thischeckismadeautomaticallybytheMakefile.)*之所以设计这一的规则是便于扫描器不需要回溯,确保对于输入一定有一条规则与其匹配*(但是,规则动作可能在内部用yyless()throwback一些输入).*正如Flex手册中所说明的,这可以提升性能--*在简单测试的情况下,相对于普通的-CF词法分析器,大概有1/3的性能提升.*额外的复杂性主要体现在处理浮点数和连续字符串文字的规则中.*如果修改了词法规则,通过以-b选项执行Flex以确保没有打破无回溯的约定,*并且坚持结果文件"lex.backup"以确认无需备份.*(在PG9.2,该检查通过Makefile自动执行)**PortionsCopyright(c)1996-2018,PostgreSQLGlobalDevelopmentGroup*PortionsCopyright(c)1994,RegentsoftheUniversityofCalifornia**IDENTIFICATION*src/backend/parser/scan.l**-------------------------------------------------------------------------*/#include"postgres.h"#include<ctype.h>#include<unistd.h>#include"common/string.h"#include"parser/gramparse.h"#include"parser/parser.h"/*onlyneededforGUCvariables*/#include"parser/scansup.h"#include"mb/pg_wchar.h"}//------------------声明部分%{/*LCOV_EXCL_START*//*Avoidexit()onfatalscannererrors(abitugly--seeyy_fatal_error)*///在扫描器出现致命错误时,避免调用exit()直接退出#undeffprintf#definefprintf(file,fmt,msg)fprintf_to_ereport(fmt,msg)staticvoidfprintf_to_ereport(constchar*fmt,constchar*msg){ereport(ERROR,(errmsg_internal("%s",msg)));}/**GUCvariables.ThisisaDIRECTviolationofthewarninggivenatthe*headofgram.y,ieflex/bisoncodemustnotdependonanyGUCvariables;*assuch,changingtheirvaluescaninduceveryunintuitivebehavior.*Butweshallhavetolivewithituntilwecanremovethesevariables.*GUC参数变量.这直接违反了gram.y中提出的约定,如flex/bison代码不能依赖GUC变量;*因此,改变他们的值会导致未知的后果.*但在去掉这些变量前,不得不"活下去"*/intbackslash_quote=BACKSLASH_QUOTE_SAFE_ENCODING;boolescape_string_warning=true;boolstandard_conforming_strings=true;/**SetthetypeofYYSTYPE.*设置YYSTYPE.*在Bison中,全局变量yylval的类型为YYSTYPE,默认为int*Internally,bisondeclareseachvalueasaCunionthatincludesallofthetypes.*Youlistallofthetypesin%uniondeclarations.*BisonturnsthemintoatypedefforauniontypecalledYYSTYPE.*/#defineYYSTYPEcore_YYSTYPE/**Setthetypeofyyextra.Allstatevariablesusedbythescannershould*beinyyextra,*not*staticallyallocated.*设置yyextra的数据类型.所有扫描器使用的状态变量应在yyextra中,不是静态分配的.*/#defineYY_EXTRA_TYPEcore_yy_extra_type*/**Eachcalltoyylexmustsetyylloctothelocationofthefoundtoken*(expressedasabyteoffsetfromthestartoftheinputtext).*Whenweparseatokenthatrequiresmultiplelexerrulestoprocess,*thisshouldbedoneinthefirstsuchrule,elseyyllocwillpoint*intothemiddleofthetoken.*每一次调用yylex必须设置yylloc指向发现的token所在的位置.*(从输入文本开始计算的字节偏移量)*在分析一个需要多个词法规则进行处理的token时,*在第一次应用规则时就应该完成这个动作,否则的话yylloc会指向到token的中间位置.*/#defineSET_YYLLOC()(*(yylloc)=yytext-yyextra->scanbuf)/**Advanceyyllocbythegivennumberofbytes.*通过给定的字节数调整yylloc的位置*/#defineADVANCE_YYLLOC(delta)(*(yylloc)+=(delta))#definestartlit()(yyextra->literallen=0)staticvoidaddlit(char*ytext,intyleng,core_yyscan_tyyscanner);staticvoidaddlitchar(unsignedcharychar,core_yyscan_tyyscanner);staticchar*litbufdup(core_yyscan_tyyscanner);staticchar*litbuf_udeescape(unsignedcharescape,core_yyscan_tyyscanner);staticunsignedcharunescape_single_char(unsignedcharc,core_yyscan_tyyscanner);staticintprocess_integer_literal(constchar*token,YYSTYPE*lval);staticboolis_utf16_surrogate_first(pg_wcharc);staticboolis_utf16_surrogate_second(pg_wcharc);staticpg_wcharsurrogate_pair_to_codepoint(pg_wcharfirst,pg_wcharsecond);staticvoidaddunicode(pg_wcharc,yyscan_tyyscanner);staticboolcheck_uescapechar(unsignedcharescape);#defineyyerror(msg)scanner_yyerror(msg,yyscanner)#definelexer_errposition()scanner_errposition(*(yylloc),yyscanner)staticvoidcheck_string_escape_warning(unsignedcharychar,core_yyscan_tyyscanner);staticvoidcheck_escape_warning(core_yyscan_tyyscanner);/**Workaroundabuginflex2.5.35:itemitsacoupleoffunctionsthat*itforgetstoemitdeclarationsfor.Sinceweuse-Wmissing-prototypes,*thiswouldcausewarnings.Providingourowndeclarationsshouldbe*harmlessevenwhenthebuggetsfixed.*Flex2.5.35存在一个bug:忽略了函数但没有忽略函数声明.*因为使用了-Wmissing-prototypes选项,这会导致警告出现.*就算bug修复,提供PG的声明也可能会存在问题.*/externintcore_yyget_column(yyscan_tyyscanner);externvoidcore_yyset_column(intcolumn_no,yyscan_tyyscanner);%}

到此,关于“PostgreSQL中Declarations的作用是什么”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!