1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>41.3.The Parser Stage</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="overview.html" title="Chapter41.Overview of PostgreSQL Internals">
<link rel="prev" href="connect-estab.html" title="41.2.How Connections are Established">
<link rel="next" href="rule-system.html" title="41.4.The PostgreSQL Rule System">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="parser-stage"></a>41.3.The Parser Stage</h2></div></div></div>
<p> The <em class="firstterm">parser stage</em> consists of two parts:
</p>
<div class="itemizedlist"><ul type="disc">
<li><p> The <em class="firstterm">parser</em> defined in
<code class="filename">gram.y</code> and <code class="filename">scan.l</code> is
built using the Unix tools <span class="application">yacc</span>
and <span class="application">lex</span>.
</p></li>
<li><p> The <em class="firstterm">transformation process</em> does
modifications and augmentations to the data structures returned by the parser.
</p></li>
</ul></div>
<p>
</p>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="id815181"></a>41.3.1.Parser</h3></div></div></div>
<p> The parser has to check the query string (which arrives as plain
ASCII text) for valid syntax. If the syntax is correct a
<em class="firstterm">parse tree</em> is built up and handed back;
otherwise an error is returned. The parser and lexer are
implemented using the well-known Unix tools <span class="application">yacc</span>
and <span class="application">lex</span>.
</p>
<p> The <em class="firstterm">lexer</em> is defined in the file
<code class="filename">scan.l</code> and is responsible
for recognizing <em class="firstterm">identifiers</em>,
the <em class="firstterm">SQL key words</em> etc. For
every key word or identifier that is found, a <em class="firstterm">token</em>
is generated and handed to the parser.
</p>
<p> The parser is defined in the file <code class="filename">gram.y</code> and
consists of a set of <em class="firstterm">grammar rules</em> and
<em class="firstterm">actions</em> 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.
</p>
<p> The file <code class="filename">scan.l</code> is transformed to the C
source file <code class="filename">scan.c</code> using the program
<span class="application">lex</span> and <code class="filename">gram.y</code> is
transformed to <code class="filename">gram.c</code> using
<span class="application">yacc</span>. 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 time <span class="application">lex</span>
or <span class="application">yacc</span> is called.
</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p> The mentioned transformations and compilations are normally done
automatically using the <em class="firstterm">makefiles</em>
shipped with the <span class="productname">PostgreSQL</span>
source distribution.
</p>
</div>
<p>
</p>
<p> A detailed description of <span class="application">yacc</span> or
the grammar rules given in <code class="filename">gram.y</code> would be
beyond the scope of this paper. There are many books and
documents dealing with <span class="application">lex</span> and
<span class="application">yacc</span>. You should be familiar with
<span class="application">yacc</span> before you start to study the
grammar given in <code class="filename">gram.y</code> otherwise you won't
understand what happens there.
</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="id815385"></a>41.3.2.Transformation Process</h3></div></div></div>
<p> 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,
the <em class="firstterm">transformation process</em> 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 the <em class="firstterm">query tree</em>.
</p>
<p> 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 (<code class="command">BEGIN</code>, <code class="command">ROLLBACK</code>, etc), and
these can then be correctly executed without any further analysis.
Once we know that we are dealing with an actual query (such as
<code class="command">SELECT</code> or <code class="command">UPDATE</code>), it is okay to
start a transaction if we're not already in one. Only then can the
transformation process be invoked.
</p>
<p> 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, a <code class="structname">FuncCall</code> node in the
parse tree represents something that looks syntactically like a function
call. This may be transformed to either a <code class="structname">FuncExpr</code>
or <code class="structname">Aggref</code> 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.
</p>
</div>
</div></body>
</html>
|