1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5 >The Parser Stage</TITLE
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
10 HREF="mailto:pgsql-docs@postgresql.org"><LINK
12 TITLE="PostgreSQL 7.4.1 Documentation"
13 HREF="index.html"><LINK
15 TITLE="Overview of PostgreSQL Internals"
16 HREF="overview.html"><LINK
18 TITLE="How Connections are Established"
19 HREF="connect-estab.html"><LINK
21 TITLE="The PostgreSQL Rule System"
22 HREF="rule-system.html"><LINK
25 HREF="stylesheet.css"><META
27 CONTENT="2003-12-22T03:48:47"></HEAD
33 SUMMARY="Header navigation table"
43 >PostgreSQL 7.4.1 Documentation</TH
51 HREF="connect-estab.html"
67 >Chapter 42. Overview of PostgreSQL Internals</TD
81 HREF="rule-system.html"
96 >42.3. The Parser Stage</A
102 > consists of two parts:
120 built using the Unix tools <SPAN
134 >transformation process</I
136 modifications and augmentations to the data structures returned by the parser.
151 > The parser has to check the query string (which arrives as plain
152 ASCII text) for valid syntax. If the syntax is correct a
156 > is built up and handed back;
157 otherwise an error is returned. The parser and lexer are
158 implemented using the well-known Unix tools <SPAN
171 > is defined in the file
184 every key word or identifier that is found, a <I
188 is generated and handed to the parser.
191 > The parser is defined in the file <TT
195 consists of a set of <I
202 > that are executed whenever a rule
203 is fired. The code of the actions (which is actually C code) is
204 used to build up the parse tree.
210 > is transformed to the C
229 >. After these transformations
230 have taken place a normal C compiler can be used to create the
231 parser. Never make any changes to the generated C files as they
232 will be overwritten the next time <SPAN
248 > The mentioned transformations and compilations are normally done
249 automatically using the <I
253 shipped with the <SPAN
264 > A detailed description of <SPAN
268 the grammar rules given in <TT
272 beyond the scope of this paper. There are many books and
273 documents dealing with <SPAN
280 >. You should be familiar with
284 > before you start to study the
288 > otherwise you won't
289 understand what happens there.
298 >42.3.2. Transformation Process</A
301 > The parser stage creates a parse tree using only fixed rules about
302 the syntactic structure of SQL. It does not make any lookups in the
303 system catalogs, so there is no possibility to understand the detailed
304 semantics of the requested operations. After the parser completes,
307 >transformation process</I
308 > takes the tree handed
309 back by the parser as input and does the semantic interpretation needed
310 to understand which tables, functions, and operators are referenced by
311 the query. The data structure that is built to represent this
312 information is called the <I
318 > The reason for separating raw parsing from semantic analysis is that
319 system catalog lookups can only be done within a transaction, and we
320 do not wish to start a transaction immediately upon receiving a query
321 string. The raw parsing stage is sufficient to identify the transaction
322 control commands (<TT
329 these can then be correctly executed without any further analysis.
330 Once we know that we are dealing with an actual query (such as
338 start a transaction if we're not already in one. Only then can the
339 transformation process be invoked.
342 > The query tree created by the transformation process is structurally
343 similar to the raw parse tree in most places, but it has many differences
344 in detail. For example, a <TT
348 parse tree represents something that looks syntactically like a function
349 call. This may be transformed to either a <TT
356 > node depending on whether the referenced
357 name turns out to be an ordinary function or an aggregate function.
358 Also, information about the actual data types of columns and expression
359 results is added to the query tree.
368 SUMMARY="Footer navigation table"
379 HREF="connect-estab.html"
397 HREF="rule-system.html"
407 >How Connections are Established</TD