postgresql之词法分析简介

news/2024/7/9 20:24:58 标签: postgresql

一、词法文件结构

flex的词法输入文件.l 结构通过%%分成三部分

  • 声明

  • 规则

  • c 代码

声明中 %{ C代码声明 %} 中包裹的内容将直接原样拷贝到生成的C代码中,

最后的段也是直接原样拷贝到生成的C代码中,

中间的规则段,每个规则由 模式 + 动作 组成,模式一般都是正则表达式书写方式。
在这里插入图片描述

二、pg中的词法分析文件

2.1 声明段

src/backend/parser/scan.l


%{

/* LCOV_EXCL_START */

/* Avoid exit() on fatal scanner errors (a bit ugly -- see yy_fatal_error) */
#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.
 */
int			backslash_quote = BACKSLASH_QUOTE_SAFE_ENCODING;
bool		escape_string_warning = true;
bool		standard_conforming_strings = true;
...

%}

...
%option nodefault
%option noinput
%option nounput
%option noyywrap
%option noyyalloc
%option noyyrealloc
%option noyyfree
%option warn
%option prefix="core_yy"

...

%%

2.2 规则段

{whitespace}	{
					/* ignore */
				}

{xcstart}		{
					/* Set location in case of syntax error in comment */
					SET_YYLLOC();
					yyextra->xcdepth = 0;
					BEGIN(xc);
					/* Put back any characters past slash-star; see above */
					yyless(2);
				}

<xc>{
{xcstart}		{
					(yyextra->xcdepth)++;
					/* Put back any characters past slash-star; see above */
					yyless(2);
				}
...

%%

/* LCOV_EXCL_STOP */

2.3 C code 段

/*
 * Arrange access to yyextra for subroutines of the main yylex() function.
 * We expect each subroutine to have a yyscanner parameter.  Rather than
 * use the yyget_xxx functions, which might or might not get inlined by the
 * compiler, we cheat just a bit and cast yyscanner to the right type.
 */
#undef yyextra
#define yyextra  (((struct yyguts_t *) yyscanner)->yyextra_r)

/* Likewise for a couple of other things we need. */
#undef yylloc
#define yylloc	(((struct yyguts_t *) yyscanner)->yylloc_r)
#undef yyleng
#define yyleng	(((struct yyguts_t *) yyscanner)->yyleng_r)


/*
 * scanner_errposition
 *		Report a lexer or grammar error cursor position, if possible.
 *
 * This is expected to be used within an ereport() call, or via an error
 * callback such as setup_scanner_errposition_callback().  The return value
 * is a dummy (always 0, in fact).
 *
 * Note that this can only be used for messages emitted during raw parsing
 * (essentially, scan.l, parser.c, and gram.y), since it requires the
 * yyscanner struct to still be available.
 */
int
scanner_errposition(int location, core_yyscan_t yyscanner)
{
	int			pos;

	if (location < 0)
		return 0;				/* no-op if location is unknown */

	/* Convert byte offset to character number */
	pos = pg_mbstrlen_with_len(yyextra->scanbuf, location) + 1;
	/* And pass it to the ereport mechanism */
	return errposition(pos);
}

...

2.4 最终生成的词法分析文件

src/backend/parser/scan.c
通过flex工具,对scan.l文件进行处理,最终将生成scan.c代码,用于后续对sql语言进行词法分析。


http://www.niftyadmin.cn/n/1830024.html

相关文章

Easyui 生成layout

Easyui 生成layout var $tabs;var $body;var $south;function appendLayout(title, href) {if (!$body)$body $(document.body);if (!$body.hasClass(layout)) {var $centerHtml $("<div>", {"id": "center","style": "…

(转)Java web 项目中文件路径

为什么80%的码农都做不了架构师&#xff1f;>>> 源链接;http://blog.sina.com.cn/s/blog_6b695d5c01015dkp.html 前言 Java的路径问题&#xff0c;非常难搞。最近的工作涉及到创建和读取文件的工作&#xff0c;这里我就给大家彻底得解决Java路径问题。 我编写了一个…

MYSQL主从同步故障一例及解决过程

公司里有两个mysql服务器做主从同步&#xff0c;某天Nagios发来报警短信&#xff0c;mysqla is down...赶紧联系机房&#xff0c;机房的人反馈来的信息是 HARDWARE ERROR 后面信息省略&#xff0c;让机房记下错误信息后让他们帮忙重启下看是不是能正常起来&#xff0c;结果竟然…

使用 hexdump dump 文件内容

名词解释 【dump】 dump 是指把文件的内容&#xff0c;每个字节用2位十六进制数来表示的方式。 缘由 最近看矢泽久雄的《How Program Works》&#xff0c;了解到 dump “exe文件”&#xff08;恕我不知该如何将 dump 汉化&#xff09;。然后在系统&#xff08;Windows 7&#x…

JavaWeb学习笔记之JSP(一)

1. JSP: 1.1. 为什么需要 JSP ? 如果使用Servlet程序来输出只有局部内容需要动态改变的网页&#xff0c;但是其中的静态网页内容也需要程序员使用Java语言来进行输出&#xff0c;这就造成了大量代码的冗余&#xff0c;使整个Servlet维护起来将会变的困难。 1.2. Java Server P…

XmlParser和HtmlParser

经常要用的Xml和Html解决&#xff0c;实际上这个领域也有非常好的解决方案。 相对来说现在各种开源的Xml解析功能比较丰富&#xff0c;机制也比较灵活&#xff0c;但是由于他功能比较完善&#xff0c;干的事情比较多&#xff0c;所以性能方面也慢一点&#xff1b;另外&#xf…

PHP保存本地日志文件

*** 写文件* param string $file 文件路径* param string $str 写入内容* param char $mode 写入模式*/function writeFile($file,$str,$modew){ $oldmask umask(0); $fp fopen($file,$mode); flock($fp, 3); if(!$fp) { Return …

AC日记——逃出克隆岛 (bfs)

2059 逃出克隆岛 时间限制: 1 s空间限制: 128000 KB题目等级 : 黄金 Gold题解题目描述 Descriptionoi小组的yh酷爱玩魔兽rpg&#xff0c;每天都会在u9搜索最新的rpg地图。 今天&#xff0c;他找到一张名为《逃出克隆岛》的地图&#xff0c;在这张地图中&#xff0c;有一个n行m列…