Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
50.3. The Parser Stage
Prev UpChapter 50. Overview of PostgreSQL InternalsHome Next

50.3. The Parser Stage#

Theparser stage consists of two parts:

  • Theparser defined ingram.y andscan.l is built using the Unix toolsbison andflex.

  • Thetransformation process does modifications and augmentations to the data structures returned by the parser.

50.3.1. Parser#

The parser has to check the query string (which arrives as plain text) for valid syntax. If the syntax is correct aparse tree is built up and handed back; otherwise an error is returned. The parser and lexer are implemented using the well-known Unix toolsbison andflex.

Thelexer is defined in the filescan.l and is responsible for recognizingidentifiers, theSQL key words etc. For every key word or identifier that is found, atoken is generated and handed to the parser.

The parser is defined in the filegram.y and consists of a set ofgrammar rules andactions that are executed whenever a rule is fired. The code of the actions (which is actually C code) is used to build up the parse tree.

The filescan.l is transformed to the C source filescan.c using the programflex andgram.y is transformed togram.c usingbison. After these transformations have taken place a normal C compiler can be used to create the parser. Never make any changes to the generated C files as they will be overwritten the next timeflex orbison is called.

Note

The mentioned transformations and compilations are normally done automatically using themakefiles shipped with thePostgreSQL source distribution.

A detailed description ofbison or the grammar rules given ingram.y would be beyond the scope of this manual. There are many books and documents dealing withflex andbison. You should be familiar withbison before you start to study the grammar given ingram.y otherwise you won't understand what happens there.

50.3.2. Transformation Process#

The parser stage creates a parse tree using only fixed rules about the syntactic structure of SQL. It does not make any lookups in the system catalogs, so there is no possibility to understand the detailed semantics of the requested operations. After the parser completes, thetransformation process takes the tree handed back by the parser as input and does the semantic interpretation needed to understand which tables, functions, and operators are referenced by the query. The data structure that is built to represent this information is called thequery tree.

The reason for separating raw parsing from semantic analysis is that system catalog lookups can only be done within a transaction, and we do not wish to start a transaction immediately upon receiving a query string. The raw parsing stage is sufficient to identify the transaction control commands (BEGIN,ROLLBACK, etc.), and these can then be correctly executed without any further analysis. Once we know that we are dealing with an actual query (such asSELECT orUPDATE), it is okay to start a transaction if we're not already in one. Only then can the transformation process be invoked.

The query tree created by the transformation process is structurally similar to the raw parse tree in most places, but it has many differences in detail. For example, aFuncCall node in the parse tree represents something that looks syntactically like a function call. This might be transformed to either aFuncExpr orAggref node depending on whether the referenced name turns out to be an ordinary function or an aggregate function. Also, information about the actual data types of columns and expression results is added to the query tree.


Prev Up Next
50.2. How Connections Are Established Home 50.4. ThePostgreSQL Rule System
pdfepub
Go to PostgreSQL 17
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp