Postgresql源码(96)操作符的语法解析细节

2022-12-31 17:07:13 浏览数 (1)

单字符Operators

1 加法

代码语言:javascript复制
select 1 2;
3

解析过程

steps

lex

yacc规则左值

yacc规则右值

gram.y规则代码

1

SELECT 623

2

ICONST 266

3

Iconst

ICONST

$$ = $1;

4

AexprConst

Iconst

$$ = makeIntConst($1, @1)

5

c_expr

AexprConst

$$ = $1;

6

a_expr

c_expr

$$ = $1;

7

43

8

ICONST 266

9

Iconst

ICONST

$$ = $1;

10

AexprConst

Iconst

$$ = makeIntConst($1, @1)

11

c_expr

AexprConst

$$ = $1;

12

a_expr

c_expr

$$ = $1;

13

; 59

14

a_expr

a_expr ‘ ’ a_expr

$$ = (Node *) makeSimpleA_Expr(AEXPR_OP, “ ”, $1, $3, @2);

15

target_el

a_expr

$$ = makeNode(ResTarget); …

16

target_list

target_el

$$ = list_make1($1)

2 绝对值

代码语言:javascript复制
select @ -5.0;
5

解析过程

steps

lex

yacc规则左值

yacc规则右值

gram.y规则代码

1

SELECT 623

2

Op 265

3

qual_Op

Op

$$ = list_make1(makeString($1))

4

- 45

5

FCONST 260

6

AexprConst

FCONST

$$ = makeFloatConst($1, @1)

7

c_expr

AexprConst

$$ = $1

8

a_expr

c_expr

$$ = $1

9

; 59

10

a_expr

‘-’ a_expr

$$ = doNegate($2, @1);

11

a_expr

qual_Op a_expr

$$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1)

12

target_el

a_expr

$$ = makeNode(ResTarget)

13

target_list

target_el

$$ = list_make1($1)

token来源

token

lex规则

lex规则代码

SELECT

{identifier}←{ident_start}{ident_cont}*←[A-Za-z200-377_]←[A-Za-z200-377_0-9$]

{…return yyextra->keyword_tokens[kwnum];}

Op

{operator}←{op_chars}←[~!@#^&|`? -*/%<>=]

{…return Op;}

FCONST

{numeric}←`(({decinteger}.{decinteger}?)

(.{decinteger}))`←decinteger

3 取反

代码语言:javascript复制
select -(-5.0);
5

解析过程

steps

lex

yacc规则左值

yacc规则右值

gram.y规则代码

1

SELECT 623

2

- 45

3

( 40

4

- 45

5

FCONST 260

6

AexprConst

FCONST

$$ = makeFloatConst($1, @1)

7

c_expr

AexprConst

$$ = $1

8

a_expr

c_expr

$$ = $1

9

) 41

10

a_expr

‘-’ a_expr

$$ = doNegate($2, @1)

11

opt_indirection

EMPTY

12

; 59

13

c_expr

‘(’ a_expr ‘)’ opt_indirection

$$ = $2

14

a_expr

c_expr

$$ = $1

15

a_expr

‘-’ a_expr

$$ = doNegate($2, @1)

16

target_el

a_expr

$$ = makeNode(ResTarget)

17

target_list

target_el

$$ = list_make1($1)

多字符Operators

1 位运算

可以不加空格

代码语言:javascript复制
select 1<<2;
select 1<< 2;
select 1 <<2;
4

解析过程(PG16)

steps

lex

yacc规则左值

yacc规则右值

gram.y规则代码

1

SELECT 623

2

ICONST 266

3

Iconst

ICONST

$$ = $1

4

AexprConst

Iconst

$$ = makeIntConst($1, @1)

5

c_expr

AexprConst

$$ = $1

6

a_expr

c_expr

$$ = $1

7

Op 265

8

ICONST 266

9

qual_Op

Op

$$ = list_make1(makeString($1))

10

Iconst

ICONST

$$ = $1

11

AexprConst

Iconst

$$ = makeIntConst($1, @1)

12

c_expr

AexprConst

$$ = $1

13

a_expr

c_expr

$$ = $1

14

; 59

15

a_expr

a_expr qual_Op a_expr

$$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2)

16

target_el

a_expr

$$ = makeNode(ResTarget)

17

target_list

target_el

$$ = list_make1($1)

token来源(PG16)

token

lex规则

lex规则代码

SELECT

{identifier}←{ident_start}{ident_cont}*←[A-Za-z200-377_]←[A-Za-z200-377_0-9$]

{…return yyextra->keyword_tokens[kwnum];}

Op

{operator}←{op_chars}←[~!@#^&|`? -*/%<>=]

{…return Op;}

2 逻辑

空格不能少

代码语言:javascript复制
select true and false;
f

解析过程

steps

lex

yacc规则左值

yacc规则右值

gram.y规则代码

1

SELECT 623

2

TRUE_P 679

3

AexprConst

TRUE_P

$$ = makeBoolAConst(true, @1)

4

c_expr

AexprConst

$$ = $1

5

a_expr

c_expr

$$ = $1

6

AND 289

7

FALSE_P 414

8

AexprConst

FALSE_P

$$ = makeBoolAConst(false, @1)

9

c_expr

AexprConst

$$ = $1

10

a_expr

c_expr

$$ = $1

11

; 59

12

a_expr

a_expr AND a_expr

$$ = makeAndExpr($1, $3, @2)

13

target_el

a_expr

$$ = makeNode(ResTarget)

14

target_list

target_el

$$ = list_make1($1)

token来源

token

lex规则

lex规则代码

SELECT

{identifier}←{ident_start}{ident_cont}*←[A-Za-z200-377_]←[A-Za-z200-377_0-9$]

{…return yyextra->keyword_tokens[kwnum];}

TRUE_P

{identifier}←{ident_start}{ident_cont}*←[A-Za-z200-377_]←[A-Za-z200-377_0-9$]

{…return yyextra->keyword_tokens[kwnum];}

AND

{identifier}←{ident_start}{ident_cont}*←[A-Za-z200-377_]←[A-Za-z200-377_0-9$]

{…return yyextra->keyword_tokens[kwnum];}

FALSE_P

{identifier}←{ident_start}{ident_cont}*←[A-Za-z200-377_]←[A-Za-z200-377_0-9$]

{…return yyextra->keyword_tokens[kwnum];}

3 大于等于

解析过程

steps

lex

yacc规则左值

yacc规则右值

gram.y规则代码

1

SELECT 623

2

ICONST 266

3

Iconst

ICONST

$$ = $1

4

AexprConst

Iconst

$$ = makeIntConst($1, @1)

5

c_expr

AexprConst

$$ = $1

6

a_expr

c_expr

$$ = $1

7

GREATER_EQUALS 273

8

ICONST 266

9

Iconst

ICONST

$$ = $1

10

AexprConst

Iconst

$$ = makeIntConst($1, @1)

11

c_expr

AexprConst

$$ = $1

12

a_expr

c_expr

$$ = $1

13

; 59

14

a_expr

a_expr GREATER_EQUALS a_expr

$$ = (Node *) makeSimpleA_Expr(AEXPR_OP, “>=”, $1, $3, @2)

15

target_el

a_expr

$$ = makeNode(ResTarget)

16

target_list

target_el

$$ = list_make1($1)

token来源

token

lex规则

lex规则代码

SELECT

{identifier}←{ident_start}{ident_cont}*←[A-Za-z200-377_]←[A-Za-z200-377_0-9$]

{…return yyextra->keyword_tokens[kwnum];}

GREATER_EQUALS

{greater_equals}←greater_equals “>=”

{return GREATER_EQUALS;}

0 人点赞