首页 > 数据库 >flex and bison usage in PostgreSQL

flex and bison usage in PostgreSQL

时间:2023-07-15 22:23:11浏览次数:39  
标签:flex PostgreSQL yyscan parser base bison usage core yylex

flex/bison usage in pgsql

In regular bison usage, we call yyparse() to get an AST. So, I searched for yyparse in PostgreSQL source code, which eventually led me to the base_yyparse() function. What is that?

In gram.y:

%name-prefix="base_yy"
%parse-param {core_yyscan_t yyscanner}
%lex-param   {core_yyscan_t yyscanner}

This tells me that PostgreSQL has changed the conventional name yyparse to base_yyparse, which has one parameter core_yyscan_t yyscanner. What is core_yyscan_t?

In regular flex code, there is a scanner object of type yyscan_t. But in PostgreSQL, specifically in scan.l:

%option prefix="core_yy"

It renames yyscan_t to core_yyscan_t which maps to the opaque void * type.

Okay, that's fine. The next step is to know from where base_yyparse() is called.

It is called from raw_parser() function in parser.c file.

parser.c::raw_parser()
|___ scan.l::scanner_init()
|___ gram.y::parser_init()
|___ base_yyparse()
|___ scan.l::scanner_finish()

From the article [[overview of flex and bison]], we know yyparse() calls yylex() internally.
In PostgreSQL, we use base_yylex() instead. This version of yylex() has three arguments:

extern int base_yylex(YYSTYPE *lvalp, YYLTYPE *llocp, core_yyscan_t yyscanner);

Where do they come from?
core_yyscan_t yyscanner is generated by the option line:

%lex-param   {core_yyscan_t yyscanner}

YYSTYPE* lvalp is generated by the option line:

%pure-parser

YYLTYPE* llocp is generated by the option line:

%locations

Refer to the info bison section 4.3.6 Calling Conventions for Pure Parsers for more details.

Inside the base_yylex() function, the lvalp and llocp values must be set to tell the bison generated parser the semantic value and token location. PostgreSQL dispatches that work to the unerlying core_yylex() method.Through this mechanism, base_yylex() is called a 'filter' in the PostgreSQL source code.

In normal cases, base_yylex() only calls core_yylex() once. In some particular cases, one lookahead token is not enough to make a shift/reduce decision. For those cases, we need to call core_yylex() more times to retrieve more consecutive tokens.

In PostgreSQL, the WITH_LA token is the special case relevant to this article. According to PostgreSQL source code, WITH_LA token is a multiword token.

image

When does the lexical analyzer return the WITH_LA token?

image

Here is how to use with ordinality:

image

As PostgreSQL allows with ordinality being used in CTE, the parser needs to differentiate the two cases. Therefore, the WITH_LA token was introduced to enable the parser to look ahead when it encounters the with keyword.

Now, it is time to talk about with and ordinality keywords in PostgreSQL lexical scanner.

All keywords are kept in src/include/parser/kwlist.h.
image

By the way, use vim sort command to sort these keywords.
The kwlist.h is then included in scan.l file.
image

When the scanner reads an identifer, it uses some utility functions to find and return the keyword.

image

Let's use a picture to end this article.

image

标签:flex,PostgreSQL,yyscan,parser,base,bison,usage,core,yylex
From: https://www.cnblogs.com/lddcool/p/17557089.html

相关文章

  • display:flex弹性布局
     设置方式:给父元素设置display:flex,子元素可以自动挤压或拉伸flex弹性容器里的弹性盒子可以设置宽高,比如a标签设置宽高生效,不用转块描述属性值创建flex容器(父级)display:flex主轴对齐方式 justify-contentflex-start起点开始排列(默认)flex-e......
  • M1 安装redis 报错问题 {assert {[r memory usage key] < 42000}} proc ::test)
    macm1安装redis基本步骤官方下载安装包https://redis.io/download/然后解压 进入目录输入测试命令: sudomaketest没问题进行安装:sudomakeinstall安装成功redis-server启动redis服务redis-cli进行连接,接着按照key,value进行设置测试是否能正常set、get报错......
  • 8. Q_ 如下代码中文本_Sausage_的颜色是_
    Q:如下代码中文本“Sausage”的颜色是?<ulclass="shopping-list"id="awesome"><li><span>Milk</span></li><liclass="favorite"id="must-buy"><spanclass="highlight">Saus......
  • 13. Q_ 如下代码中文本_Sausage_的颜色是_
    Q:如下代码中文本“Sausage”的颜色是?<ulclass="shopping-list"id="awesome"><li><span>Milk</span></li><liclass="favorite"id="must-buy"><spanclass="highlight">Saus......
  • 11. Q_ 如下代码中文本_Sausage_的颜色是_
    Q:如下代码中文本“Sausage”的颜色是?<ulclass="shopping-list"id="awesome"><li><span>Milk</span></li><liclass="favorite"id="must-buy"><spanclass="highlight">Saus......
  • 12. Q_ 如下代码中文本_Sausage_的颜色是_
    Q:如下代码中文本“Sausage”的颜色是?<ulclass="shopping-list"id="awesome"><li><span>Milk</span></li><liclass="favorite"id="must-buy"><spanclass="highlight">Saus......
  • Flex布局
    引言:关于前端布局,我学习了Pink老师的Flex。以下是我自己做的一些简单笔记。一个小套路凡使用Flex布局,我们都会在其父元素设置display:flex;那么就有人问了,这句代码的意义何在?别急,看下面GPT怎么回答!display:flex是一种CSS属性,用于定义一个容器元素以及其子元素的布局方式。它引......
  • Flex布局常用属性详解
    1.Flex布局与响应式布局1.1为什么需要响应式布局?在电脑PC端,使用浮动,定位同时使用像素px单位就可以完成大部分布局,而且布局完之后不会有大问题,但是到了移动端,移动设备的屏幕尺寸多种多样,从小屏幕的智能手机到大屏幕的平板电脑,甚至是可穿戴设备,简单地运用和PC端一样的方式就会出......
  • flex垂直居中当界面空间不足时显示不全
    代码示例如下<divid="a"style="display:flex;justify-content:center;align-items:center;width:200px;height:100%;background:skyblue"><divid="b"style="height:400px;background:pink;width:100px">&l......
  • CSS|Flex布局
    演示动图来源:GitHub一.什么是flex布局是一种专门的CSS一维(水平/垂直)布局方案位置(定性)大小(定量)怎么研究位置和大小借助坐标系(平面直角坐标系)水平轴:主轴垂直轴:交叉轴二.区分flex容器和flex项目1)什么是flex容器启用flex布局方案的元素2)如何......