PostgreSQL中​Declarations的作用是什么

46次阅读
没有评论

共计 5850 个字符,预计需要花费 15 分钟才能阅读完成。

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

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

%{
Declarations
Definitions
Rules
User subroutines

Declarations

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

%top{
/*-------------------------------------------------------------------------
 *
 * scan.l
 * lexical scanner for PostgreSQL
 * PostgreSQL 的词法扫描器  
 *
 * NOTE NOTE NOTE:
 *  特别特别特别注意:
 * The rules in this file must be kept in sync with src/fe_utils/psqlscan.l!
 *  这个文件中的规则必须与 src/fe_utils/psqlscan.l 文件中的规则保持一致!!! 
 *
 * The rules are designed so that the scanner never has to backtrack,
 * in the sense that there is always a rule that can match the input
 * consumed so far (the rule action may internally throw back some input
 * with yyless(), however). As explained in the flex manual, this makes
 * for a useful speed increase --- about a third faster than a plain -CF
 * lexer, in simple testing. The extra complexity is mostly in the rules
 * for handling float numbers and continued string literals. If you change
 * the lexical rules, verify that you haven t broken the no-backtrack
 * property by running flex with the  -b  option and checking that the
 * resulting  lex.backup  file says that no backing up is needed. (As of
 * Postgres 9.2, this check is made automatically by the Makefile.)
 *  之所以设计这一的规则是便于扫描器不需要回溯, 确保对于输入一定有一条规则与其匹配
 * (但是, 规则动作可能在内部用 yyless() throw back 一些输入 ).
 *  正如 Flex 手册中所说明的, 这可以提升性能  -- 
 *  在简单测试的情况下, 相对于普通的 -CF 词法分析器, 大概有 1 / 3 的性能提升.
 *  额外的复杂性主要体现在处理浮点数和连续字符串文字的规则中.
 *  如果修改了词法规则, 通过以 - b 选项执行 Flex 以确保没有打破无回溯的约定,
 *  并且坚持结果文件 lex.backup 以确认无需备份.
 * (在 PG 9.2, 该检查通过 Makefile 自动执行)
 *
 * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * 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  /* only needed for GUC variables */
#include  parser/scansup.h 
#include  mb/pg_wchar.h 
//------------------  声明部分
/* LCOV_EXCL_START */
/* Avoid exit() on fatal scanner errors (a bit ugly -- see yy_fatal_error) */
// 在扫描器出现致命错误时, 避免调用 exit() 直接退出
#undef fprintf
#define fprintf(file, fmt, msg) fprintf_to_ereport(fmt, msg)
static void
fprintf_to_ereport(const char *fmt, const char *msg)
 ereport(ERROR, (errmsg_internal( %s , msg)));
 * GUC variables. This is a DIRECT violation of the warning given at the
 * head of gram.y, ie flex/bison code must not depend on any GUC variables;
 * as such, changing their values can induce very unintuitive behavior.
 * But we shall have to live with it until we can remove these variables.
 * GUC 参数变量. 这直接违反了 gram.y 中提出的约定, 如 flex/bison 代码不能依赖 GUC 变量;
 *  因此, 改变他们的值会导致未知的后果.
 *  但在去掉这些变量前, 不得不 活下去 
 */
int backslash_quote = BACKSLASH_QUOTE_SAFE_ENCODING;
bool escape_string_warning = true;
bool standard_conforming_strings = true;
 * Set the type of YYSTYPE.
 *  设置 YYSTYPE.
 *  在 Bison 中, 全局变量 yylval 的类型为 YYSTYPE, 默认为 int
 * Internally, bison declares each value as a C union that includes all of the types. 
 * You list all of the types in %union declarations. 
 * Bison turns them into a typedef for a union type called YYSTYPE.
 */
#define YYSTYPE core_YYSTYPE
 * Set the type of yyextra. All state variables used by the scanner should
 * be in yyextra, *not* statically allocated.
 *  设置 yyextra 的数据类型. 所有扫描器使用的状态变量应在 yyextra 中, 不是静态分配的.
 */
#define YY_EXTRA_TYPE core_yy_extra_type *
 * Each call to yylex must set yylloc to the location of the found token
 * (expressed as a byte offset from the start of the input text).
 * When we parse a token that requires multiple lexer rules to process,
 * this should be done in the first such rule, else yylloc will point
 * into the middle of the token.
 *  每一次调用 yylex 必须设置 yylloc 指向发现的 token 所在的位置.
 * (从输入文本开始计算的字节偏移量)
 *  在分析一个需要多个词法规则进行处理的 token 时,
 *  在第一次应用规则时就应该完成这个动作, 否则的话 yylloc 会指向到 token 的中间位置.
 */
#define SET_YYLLOC() (*(yylloc) = yytext - yyextra- scanbuf)
 * Advance yylloc by the given number of bytes.
 *  通过给定的字节数调整 yylloc 的位置
 */
#define ADVANCE_YYLLOC(delta) ( *(yylloc) += (delta) )
#define startlit() ( yyextra- literallen = 0 )
static void addlit(char *ytext, int yleng, core_yyscan_t yyscanner);
static void addlitchar(unsigned char ychar, core_yyscan_t yyscanner);
static char *litbufdup(core_yyscan_t yyscanner);
static char *litbuf_udeescape(unsigned char escape, core_yyscan_t yyscanner);
static unsigned char unescape_single_char(unsigned char c, core_yyscan_t yyscanner);
static int process_integer_literal(const char *token, YYSTYPE *lval);
static bool is_utf16_surrogate_first(pg_wchar c);
static bool is_utf16_surrogate_second(pg_wchar c);
static pg_wchar surrogate_pair_to_codepoint(pg_wchar first, pg_wchar second);
static void addunicode(pg_wchar c, yyscan_t yyscanner);
static bool check_uescapechar(unsigned char escape);
#define yyerror(msg) scanner_yyerror(msg, yyscanner)
#define lexer_errposition() scanner_errposition(*(yylloc), yyscanner)
static void check_string_escape_warning(unsigned char ychar, core_yyscan_t yyscanner);
static void check_escape_warning(core_yyscan_t yyscanner);
 * Work around a bug in flex 2.5.35: it emits a couple of functions that
 * it forgets to emit declarations for. Since we use -Wmissing-prototypes,
 * this would cause warnings. Providing our own declarations should be
 * harmless even when the bug gets fixed.
 * Flex 2.5.35 存在一个 bug: 忽略了函数但没有忽略函数声明.
 *  因为使用了 -Wmissing-prototypes 选项, 这会导致警告出现.
 *  就算 bug 修复, 提供 PG 的声明也可能会存在问题.
 */
extern int core_yyget_column(yyscan_t yyscanner);
extern void core_yyset_column(int column_no, yyscan_t yyscanner);
%}

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

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-24发表,共计5850字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)