您好,登錄后才能下訂單哦!
本篇內容主要講解“PostgreSQL中的Declarations有什么作用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“PostgreSQL中的Declarations有什么作用”吧!
PG利用Bison對語法進行分析,Bison輸入文件由以下四部分組成:
%{ Declarations %} Definitions %% Productions %% User subroutines
Declarations與Flex類似,Bison會把這些代碼原樣拷貝到相應的c文件中(默認為y.tab.c,PG中是gram.c).
名詞解釋:
terminal symbols —> 終結符
non-terminals symbols —> 非終結符
reduce —> 折疊動作,輸入為符合集合(終結符/非終結符),輸出為匹配該pattern的非終結符
production —> 產生式,比如S -> S E,成為產生式
%{ /*#define YYDEBUG 1*/ /*------------------------------------------------------------------------- * * gram.y * POSTGRESQL BISON rules/actions * * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION * src/backend/parser/gram.y * * HISTORY * AUTHOR DATE MAJOR EVENT * Andrew Yu Sept, 1994 POSTQUEL to SQL conversion * Andrew Yu Oct, 1994 lispy code conversion * * NOTES * CAPITALS are used to represent terminal symbols. * non-capitals are used to represent non-terminals. * * In general, nothing in this file should initiate database accesses * nor depend on changeable state (such as SET variables). If you do * database accesses, your code will fail when we have aborted the * current transaction and are just parsing commands to find the next * ROLLBACK or COMMIT. If you make use of SET variables, then you * will do the wrong thing in multi-query strings like this: * SET constraint_exclusion TO off; SELECT * FROM foo; * because the entire string is parsed by gram.y before the SET gets * executed. Anything that depends on the database or changeable state * should be handled during parse analysis so that it happens at the * right time not the wrong time. * * WARNINGS * If you use a list, make sure the datum is a node so that the printing * routines work. * * Sometimes we assign constants to makeStrings. Make sure we don't free * those. * 注意 * 大寫字母用于表示終結符號. * 非大寫字母用于表示非終結符號. --> 文法中的總結符號和非終結符號 * * 通常來說,這個文件中的邏輯不應啟用數據庫訪問,也不應該依賴于可更改的狀態(比如SET變量). * 如果你確實需要數據庫訪問,業務代碼會在回滾當前事務后出錯,然后開始解析命令尋找下一個ROLLBACK/COMMIT. * 如果使用了SET變量,那么會在多個查詢串中出現錯誤,比如: * SET constraint_exclusion TO off; SELECT * FROM foo; * 因為整個字符串會在SET執行前被gram.y解析 * 所有依賴數據庫或可變狀態的事件應該在解析階段處理以便在正確而非錯誤的時間發生. * *------------------------------------------------------------------------- */ #include "postgres.h" #include <ctype.h> #include <limits.h> #include "catalog/index.h" #include "catalog/namespace.h" #include "catalog/pg_am.h" #include "catalog/pg_trigger.h" #include "commands/defrem.h" #include "commands/trigger.h" #include "nodes/makefuncs.h" #include "nodes/nodeFuncs.h" #include "parser/gramparse.h" #include "parser/parser.h" #include "parser/parse_expr.h" #include "storage/lmgr.h" #include "utils/date.h" #include "utils/datetime.h" #include "utils/numeric.h" #include "utils/xml.h" /* * Location tracking support --- simpler than bison's default, since we only * want to track the start position not the end position of each nonterminal. * 位置跟蹤支持 --- 比bison默認的處理要簡單,因為我們只需要跟蹤開始位置而非每個非終結符的結束位置. */ #define YYLLOC_DEFAULT(Current, Rhs, N) \ do { \ if ((N) > 0) \ (Current) = (Rhs)[1]; \ else \ (Current) = (-1); \ } while (0) /* * The above macro assigns -1 (unknown) as the parse location of any * nonterminal that was reduced from an empty rule, or whose leftmost * component was reduced from an empty rule. This is problematic * for nonterminals defined like * OptFooList: / * EMPTY * / { ... } | OptFooList Foo { ... } ; * because we'll set -1 as the location during the first reduction and then * copy it during each subsequent reduction, leaving us with -1 for the * location even when the list is not empty. To fix that, do this in the * action for the nonempty rule(s): * if (@$ < 0) @$ = @2; * (Although we have many nonterminals that follow this pattern, we only * bother with fixing @$ like this when the nonterminal's parse location * is actually referenced in some rule.) * 上面的宏將-1(未知數)指定為所有非終結符的解析位置, * 這些非終結符是從空規則折疊(規約)而來的,或者其最左邊的組件是從空規則折疊而來. * 對于下面的非終結符,存在問題: * OptFooList: / * EMPTY * / { ... } | OptFooList Foo { ... } ; * 因為在第一次折疊時將設置值為-1,然后在接下來的折疊中拷貝該值, * 這會讓就算鏈表不為空也會一直讓位置一直為-1. * 為了修正這一錯誤,對于非空規則,執行這一動作: * if (@$ < 0) @$ = @2; * * A cleaner answer would be to make YYLLOC_DEFAULT scan all the Rhs * locations until it's found one that's not -1. Then we'd get a correct * location for any nonterminal that isn't entirely empty. But this way * would add overhead to every rule reduction, and so far there's not been * a compelling reason to pay that overhead. * 更清晰的做法是讓YYLLOC_DEFAULT掃描所有的Rhs位置直至找到不為-1為止. * 然后我們就可以為完全不為空的非終結符獲取正確的位置. * 但這樣的做法會增加每個規則折疊的負載,到目前為止,還沒有一個令人信服的理由來增加開銷. */ /* * Bison doesn't allocate anything that needs to live across parser calls, * so we can easily have it use palloc instead of malloc. This prevents * memory leaks if we error out during parsing. Note this only works with * bison >= 2.0. However, in bison 1.875 the default is to use alloca() * if possible, so there's not really much problem anyhow, at least if * you're building with gcc. * Bison不會在解析器調用期間分配內存,因此我們可以很輕松的使用palloc而不是malloc. * 這可以防止在解析期間出錯而導致的內存泄漏.注意這個特性只在2.0+才會起效. * 無論如何,,在bison 1.875這個版本,默認使用alloca分配內存,在使用gcc構建時沒有太多問題. */ #define YYMALLOC palloc #define YYFREE pfree /* Private struct for the result of privilege_target production */ //privilege_target產生式結果的私有結構體 typedef struct PrivTarget { GrantTargetType targtype; ObjectType objtype; List *objs; } PrivTarget; /* Private struct for the result of import_qualification production */ //私有結構體 --> import_qualification產生式 typedef struct ImportQual { ImportForeignSchemaType type; List *table_names; } ImportQual; /* ConstraintAttributeSpec yields an integer bitmask of these flags: */ //ConstraintAttributeSpec產生這些標志的整數位掩碼 #define CAS_NOT_DEFERRABLE 0x01 #define CAS_DEFERRABLE 0x02 #define CAS_INITIALLY_IMMEDIATE 0x04 #define CAS_INITIALLY_DEFERRED 0x08 #define CAS_NOT_VALID 0x10 #define CAS_NO_INHERIT 0x20 #define parser_yyerror(msg) scanner_yyerror(msg, yyscanner) #define parser_errposition(pos) scanner_errposition(pos, yyscanner) static void base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner, const char *msg); static RawStmt *makeRawStmt(Node *stmt, int stmt_location); static void updateRawStmtEnd(RawStmt *rs, int end_location); static Node *makeColumnRef(char *colname, List *indirection, int location, core_yyscan_t yyscanner); static Node *makeTypeCast(Node *arg, TypeName *typename, int location); static Node *makeStringConst(char *str, int location); static Node *makeStringConstCast(char *str, int location, TypeName *typename); static Node *makeIntConst(int val, int location); static Node *makeFloatConst(char *str, int location); static Node *makeBitStringConst(char *str, int location); static Node *makeNullAConst(int location); static Node *makeAConst(Value *v, int location); static Node *makeBoolAConst(bool state, int location); static RoleSpec *makeRoleSpec(RoleSpecType type, int location); static void check_qualified_name(List *names, core_yyscan_t yyscanner); static List *check_func_name(List *names, core_yyscan_t yyscanner); static List *check_indirection(List *indirection, core_yyscan_t yyscanner); static List *extractArgTypes(List *parameters); static List *extractAggrArgTypes(List *aggrargs); static List *makeOrderedSetArgs(List *directargs, List *orderedargs, core_yyscan_t yyscanner); static void insertSelectOptions(SelectStmt *stmt, List *sortClause, List *lockingClause, Node *limitOffset, Node *limitCount, WithClause *withClause, core_yyscan_t yyscanner); static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg); static Node *doNegate(Node *n, int location); static void doNegateFloat(Value *v); static Node *makeAndExpr(Node *lexpr, Node *rexpr, int location); static Node *makeOrExpr(Node *lexpr, Node *rexpr, int location); static Node *makeNotExpr(Node *expr, int location); static Node *makeAArrayExpr(List *elements, int location); static Node *makeSQLValueFunction(SQLValueFunctionOp op, int32 typmod, int location); static Node *makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args, int location); static List *mergeTableFuncParameters(List *func_args, List *columns); static TypeName *TableFuncTypeName(List *columns); static RangeVar *makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner); static void SplitColQualList(List *qualList, List **constraintList, CollateClause **collClause, core_yyscan_t yyscanner); static void processCASbits(int cas_bits, int location, const char *constrType, bool *deferrable, bool *initdeferred, bool *not_valid, bool *no_inherit, core_yyscan_t yyscanner); static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); %}
到此,相信大家對“PostgreSQL中的Declarations有什么作用”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。