chapter 4.4 bottom-up parsing 自底向上的分析

64
Chapter 4.4 4.4 Bottom-Up Parsing 自自自自自自自 4.4.1 OVERVIEW OF BOTTOM-UP PARSING 4.4.1 OVERVIEW OF BOTTOM-UP PARSING 自自自自自自自自 自自自自自自自自 4.4.2 FINITE AUTOMATA OF LR(0) ITEMS 4.4.2 FINITE AUTOMATA OF LR(0) ITEMS AND LR(0) PARSING AND LR(0) PARSING LR(0) LR(0) 自自自自自自自自 自自自自自自自自 LR(0) LR(0) 自自 自自 4.4.3 SLR(1) Parsing SLR(1) 4.4.3 SLR(1) Parsing SLR(1) 自自 自自 4.4.4 General LR(1) and LALR(1) Pars 4.4.4 General LR(1) and LALR(1) Pars ing ing 自自 自自 LR(1) LR(1) LALR(1) LALR(1) 自自 自自

Upload: talasi

Post on 16-Mar-2016

95 views

Category:

Documents


2 download

DESCRIPTION

Chapter 4.4 Bottom-Up Parsing 自底向上的分析. 4.4.1 OVERVIEW OF BOTTOM-UP PARSING 自底向上分析概述 4.4.2 FINITE AUTOMATA OF LR(0) ITEMS AND LR(0) PARSING LR(0) 项的有穷自动机与 LR(0) 分析 4.4.3 SLR(1) Parsing SLR(1) 分析 4.4.4 General LR(1) and LALR(1) Parsing 一般的 LR(1) 和 LALR(1) 分析. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

Chapter 4.44.4 Bottom-Up Parsing 自底向上的分析

4.4.1 OVERVIEW OF BOTTOM-UP PARSING 4.4.1 OVERVIEW OF BOTTOM-UP PARSING 自底向上分析概述自底向上分析概述4.4.2 FINITE AUTOMATA OF LR(0) ITEMS AN4.4.2 FINITE AUTOMATA OF LR(0) ITEMS AN

D LR(0) PARSINGD LR(0) PARSING LR(0)LR(0) 项的有穷自动机与项的有穷自动机与 LR(0) LR(0) 分析分析4.4.3 SLR(1) Parsing SLR(1)4.4.3 SLR(1) Parsing SLR(1) 分析分析4.4.4 General LR(1) and LALR(1) Parsing 4.4.4 General LR(1) and LALR(1) Parsing 一般的一般的 LR(1)LR(1) 和和 LALR(1)LALR(1) 分析 分析

Page 2: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

4.4.1 OVERVIEW OF BOTTOM-UP PARSING 4.4.1 OVERVIEW OF BOTTOM-UP PARSING 自底向上分析概述自底向上分析概述

$ StartState$ StartState Inputstring$ Inputstring$ … … ……

…… …… $ StartSymbol $ StartSymbol $ $ accept accept

●● 自底向上分析的一般形式:自底向上分析的一般形式:

1.1. ShiftShift a terminal from the front of the input to the top o a terminal from the front of the input to the top of the stack.f the stack.

2.2. ReduceReduce a string α at the top of the stack to a nontermi a string α at the top of the stack to a nonterminal A, given the BNF choice A→α.nal A, given the BNF choice A→α.

●● A bottom-up parser has two possible actions (besides "accept")A bottom-up parser has two possible actions (besides "accept")

Page 3: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

1)1) 已知已知 G: S→(S) S ε,∣G: S→(S) S ε,∣ 请应用自底向上分析方法解请应用自底向上分析方法解 () () L(G)?L(G)?

●● exampleexample

6 $S 6 $S $ $ S’S’→→ S S 规约规约

1 $1 $ () $() $ 移进移进

5 $(S)S 5 $(S)S $ $ S S →→ (S)S (S)S 规约规约3 $(S3 $(S ) $ ) $ 移进移进4 $(S) 4 $(S) $ $ S→εS→ε 规约规约2 $(2 $( ) $ ) $ S→εS→ε 规约规约分析栈分析栈 输入输入 动作动作

S' → SS' → SS S →→ (S)S| (S)S|εε

解: 解: (1)(1) 拓广文法拓广文法

(2)(2) 分析过程分析过程

7 $S’ 7 $S’ $ $ 接受接受

S’=>SS’=>S [S' → S] [S' → S] =>(S)S [S→(S)S]=>(S)S [S→(S)S] =>(S) [S→ε]=>(S) [S→ε] =>( ) [S→ε]=>( ) [S→ε]

Page 4: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

2)2) 已知已知 G: EG: E→→E + n | n ,E + n | n , 请应用自底向上分析方法解请应用自底向上分析方法解 n+n n+n L(GL(G)?)?

●● exampleexample

6 $E 6 $E $ $ E’→EE’→E 规约规约

1 $1 $ n+n$n+n$ 移进移进

5 $E+n 5 $E+n $ $ E→E+nE→E+n 规约规约3 $E3 $E +n$ +n$ 移进移进4 $E+ 4 $E+ n$ n$ 移进移进2 $n2 $n +n$ +n$ E→nE→n 规约规约分析栈分析栈 输入输入 动作动作

E’→EE’→EEE→→E + n | n E + n | n

解: 解: (1)(1) 拓广文法拓广文法

(2)(2) 分析过程分析过程

7 $E’ 7 $E’ $ $ 接受接受

E’=>EE’=>E [E' → E] [E' → E] =>E+n [E→E+n]=>E+n [E→E+n] =>n+n [E→n]=>n+n [E→n]

Page 5: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

3)3) 已知文法已知文法 G[S],G[S], 请应用自底向上分析方法判断请应用自底向上分析方法判断 accd accd L(G)?L(G)? S S aA aA A A cA | d cA | d

●● exampleexample

S’→SS’→SS S aA aA A A cA | d cA | d

解: 解: (1)(1) 拓广文法拓广文法

(2)(2) 有穷自动机有穷自动机 AAc.Ac.AAA.cA.cAAA.d.d ss22

SSa.Aa.AAA.cA.cAAA.d.d ss11

S’→S S’→S SS.aA.aA ss00

startstart

AAcA.cA. ss44

SSaA.aA. ss55

AAd.d. ss33

AA

dd

dd

AA

cc

aa

cc

S’S’S.S. ss

SS

Page 6: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

6 $s6 $s00aass11ccss22ccss22AAss44 $$ reduce Areduce AcA cA

1 $s1 $s00 accd$ accd$ shiftshift

5 $s5 $s00aass11ccss22ccss22ddss33 $$ reduce Areduce Ad d

3 $ s3 $ s00aass11ccss2 2 cd$cd$ shiftshift4 $ s4 $ s00aass11ccss22ccss2 2 d$d$ shiftshift

2 $ s2 $ s00aass1 1 ccd$ccd$ shiftshift

分析栈分析栈 输入输入 动作动作(3)(3) 分析过程分析过程

7 $ s7 $ s00aass11ccss22AAss44 $$ reduce Areduce AcA cA

AAc.Ac.AAA.cA.cAAA.d.d ss22

SSa.Aa.AAA.cA.cAAA.d.d ss11

S’S’.S .S SS.aA.aA ss00

startstart

AAcA.cA. ss44

SSaA.aA. ss55

AAd.d. ss33

AAdd

ddAA

cc

aa

cc(2)(2) 有穷自动机有穷自动机

S’→SS’→SS S aA aA A A cA | d cA | d

解: 解: (1)(1) 拓广文法拓广文法

8 $ s8 $ s00aass11AAss55 $$ reduce Sreduce SaA aA 9 $ s9 $ s00SS $ $ reduce S’reduce S’S S 10 $ 10 $ S’S’ $ $ acceptaccept

S’S’S.S. ssSS

Page 7: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● 相关术语相关术语

S’S’=>=>SS => =>(S)S(S)S => =>(S)(S) => => ( )( )推导中的终结符和非终结符的每个推导中的终结符和非终结符的每个中间串中间串称为右句型。称为右句型。1) the right sentential form 1) the right sentential form 右句型右句型

当前栈和输入串之间发生了间隔,例当前栈和输入串之间发生了间隔,例 E || +n, E+ || n,…, E || +n, E+ || n,…, 在在每种情况下,每种情况下,分析栈分析栈的符号序列被称为右句型的可行前缀的符号序列被称为右句型的可行前缀。。∴∴E, E+, E+nE, E+, E+n 都是右句型都是右句型 E+nE+n 的可行前缀。的可行前缀。

2) 2) 右句型的可行前缀 右句型的可行前缀 viable prefixviable prefix (活前缀)(活前缀)

这个串,在右句子格式中发生的位置以及用来规约它的产这个串,在右句子格式中发生的位置以及用来规约它的产生式被称为右句型的句柄。生式被称为右句型的句柄。3) The handle of the right sentential form 3) The handle of the right sentential form 右句型的句柄右句型的句柄

Page 8: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

4.4.2 FINITE AUTOMATA OF LR(0) ITEMS AND LR(0) P4.4.2 FINITE AUTOMATA OF LR(0) ITEMS AND LR(0) PARSING LR(0)ARSING LR(0) 项的有穷自动机与项的有穷自动机与 LR(0) LR(0) 分析分析

4.4.2.1 LR(0) ITEMS LR(0)4.4.2.1 LR(0) ITEMS LR(0) 项项

a production choice with a distinguished position in its a production choice with a distinguished position in its right-hand side. right-hand side.

1) LR(0) ITEMS LR(0)1) LR(0) ITEMS LR(0) 项项

if A → αis a production choice, and if β and Y are any two if A → αis a production choice, and if β and Y are any two strings of symbols (including the empty string s) such that βγ strings of symbols (including the empty string s) such that βγ = α, then A= α, then A→→ β·γβ·γis an LR(0) item. is an LR(0) item.

ExampleExample

Page 9: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

1)1) 已知已知 GG ,求其项目。,求其项目。S' → SS' → SS → (S)S|S → (S)S|εε

●● exampleexample

This grammar has This grammar has three production choices three production choices and eight items:and eight items:S' → ·SS' → ·SS' → S·S' → S·S → ·(S)SS → ·(S)SS → (·S)SS → (·S)SS → (S·)SS → (S·)SS → (S)·SS → (S)·SS → (S)S·S → (S)S· S S →→ ··

解:解:

2)2) 已知已知 GG ,求其项目。,求其项目。 E’→EE’→E EE→→E + n | nE + n | n

This grammar has This grammar has three production choices three production choices and eight items:and eight items:E’→·EE’→·EE’→E·E’→E·E→·E + nE→·E + nE→E· + nE→E· + nE→E +· nE→E +· nE→E + n·E→E + n·E→·E E→·E E→E· E→E·

解:解:

Page 10: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● 相关术语相关术语

在文法产生式右部某个位置标有‘在文法产生式右部某个位置标有‘ .’ .’ 的产生式,称为文的产生式,称为文法的一个法的一个 LR(0)LR(0) 项目 。项目 。1) 1) 项目项目

形如 形如 AA . . 的项目称为的项目称为初始项目初始项目;;形如 形如 AA . . 的项目称为的项目称为归约项目归约项目((完整项目完整项目););形如 形如 AA . B . B 的项目称为的项目称为待约项目待约项目 (( 基本项目基本项目 ) B N∈) B N∈ ;;形如 形如 AA . a . a 的项目称为的项目称为移进项目移进项目 (( 基本项目基本项目 ) a T∈) a T∈ 。。

项目项目 AA11..22 对对活前缀活前缀 = = 1 1 是有效的是有效的(存在规范推导 (存在规范推导 S =>S =>**Aw => Aw => 1122ww )。)。2) 2) 有效项目有效项目

若项目 若项目 AA11.B.B2 2 对活前缀 对活前缀 = = 1 1 是有效的,且 是有效的,且 BB 是产生式,则项目 是产生式,则项目 B B . . 对活前缀 对活前缀 = = 1 1 也是有效的。也是有效的。

:伽马 :伽马 :艾塔:艾塔

Page 11: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● 相关术语相关术语

文法文法 GG 的某个活前缀的某个活前缀的所有有效项目组成的集合,称的所有有效项目组成的集合,称为活前缀为活前缀的的 LR(0)LR(0) 有效项目集有效项目集。。文法文法 GG 的所有有效项目集组成的集合,称为的所有有效项目集组成的集合,称为 GG 的的 LR(0)LR(0)项目集规范族项目集规范族。。

3)3) 有效项目集,项目集规范族有效项目集,项目集规范族

Page 12: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● 相关术语相关术语

设设 II 是文法是文法 GG 的一个的一个 LR(0)LR(0) 项目集合,项目集合, II 的项目闭包的项目闭包 cloclosure(I)sure(I) 定义如下:定义如下:(1) I (1) I closure(I) closure(I) 。。(2) (2) 若项目若项目 A A . B . B closure(I) closure(I) ,且 ,且 B B 是是 GG 的的产生式,则项目产生式,则项目 B B . . closure(I) closure(I) 。。(3) closure(I)(3) closure(I) 仅包含上述两条规则确定的仅包含上述两条规则确定的 LR(0)LR(0) 项目。项目。

4)4) 项目闭包项目闭包

若若 II 是文法是文法 GG 的一个的一个 LR(0)LR(0) 项目集,项目集, XX 是是 GG 中的文法符中的文法符号。号。go(I, X) = closure(J) go(I, X) = closure(J) 其中其中 J ={AJ ={AX . X . | A | A . X . XI }I }称函数称函数 go(I, X)go(I, X) 为转移函数。为转移函数。项目项目 A A X . X . 称为项目称为项目 A A . X . X 后继。后继。

5)5) 转移函数转移函数

Page 13: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● 相关术语相关术语

若文法若文法 G = ( VG = ( VTT, V, VNN, S, P), S, P) ,则识别,则识别 GG 的句柄的自动机为的句柄的自动机为DFA M = ( DFA M = ( = V = VTTVVNN ,, Q = GQ = G 的的 LR(0)LR(0) 项目集规范族项目集规范族 , , qq0 0 = = closureclosure( {S( {S.S} ).S} ) ,, F = F = 所有含归约项目的有效项目集组成的集合,所有含归约项目的有效项目集组成的集合, = = gogo(I,X) )(I,X) ) 。。

6) 6) 识别识别 GG 的句柄的自动机的句柄的自动机

若将所有状态均视为终态,则识别活前缀的自动机若将所有状态均视为终态,则识别活前缀的自动机 DFA DFA MM= ( = ( = V = VTTVVNN,, Q = GQ = G 的的 LR(0)LR(0) 项目集规范族项目集规范族 , , qq0 0 = = closureclosure({S({S.S}).S}) ,, F = QF = Q ,, = = gogo (I,X) ) (I,X) ) 。。

7) 7) 识别活前缀的自动机识别活前缀的自动机

Page 14: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● 相关术语相关术语

对于拓广文法对于拓广文法 GG 的每一个活前缀 的每一个活前缀 ,它的有效项目集恰,它的有效项目集恰好是从识别 好是从识别 GG 活前缀的自动机的初态出发,经过 活前缀的自动机的初态出发,经过 路 路径所到达的那个状态所代表的项目集合。径所到达的那个状态所代表的项目集合。

8)8) 定理定理

Page 15: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

4.4.2.2 Finite Automata of Items 4.4.2.2 Finite Automata of Items 项目的有穷自动机项目的有穷自动机● ● LR(0)LR(0) 项的项的 NFANFA 的转换的转换

A→α·XηA→α·Xη A→αX · ηA→αX · ηXX

A→α·XηA→α·Xη X→·βX→·βεεX→βX→β

Page 16: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

1)1) 已知已知 GG ,求其,求其 DFADFA 。。S' → SS' → SS → (S)S|S → (S)S|εε

●● exampleexample

S' → ·SS' → ·SS' → S·S' → S·S → ·(S)SS → ·(S)SS → (·S)SS → (·S)SS → (S·)SS → (S·)SS → (S)·SS → (S)·SS → (S)S·S → (S)S· S S →→ ··

解:解:

NFANFA

eight itemseight items

eight stateseight states

S'→·SS'→·S S'→S·S'→S·

S→·(S)SS→·(S)S SS→→ ··

SS

εεεε

S→(·S)SS→(·S)S S→(S·)SS→(S·)S S→(S)·S S→(S)·S

S→(S)S·S→(S)S·

εε SS

εε

))(( εε εε

SS

Page 17: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

DFADFA

S'→·SS'→·S S'→S·S'→S·

S→·(S)SS→·(S)S SS→→ ··

SS

εεεε

S→(·S)SS→(·S)S S→(S·)SS→(S·)S S→(S)·S S→(S)·S

S→(S)S·S→(S)S·

εε SS

εε

))(( εε εε

SS

NFANFA

S'→·SS'→·SS→·(S)SS→·(S)SSS→→ ··

S→(·S)S S→(·S)S S→·(S)SS→·(S)SSS→→ ··

S→(S)·SS→(S)·SS→·(S)SS→·(S)SSS→→ ··

S'→S·S'→S·

S→(S)S·S→(S)S·

SS

(( ))

SS((((

00

11

22

S→(S·)SS→(S·)SSS 33

4455

Page 18: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

●● exampleexample

解:解:

2)2) 已知已知 GG ,求其,求其 DFA DFA 。。 E’→EE’→E EE→→E + n | nE + n | n

E’→·EE’→·EE’→E·E’→E·E→·E + nE→·E + nE→E· + nE→E· + nE→E +· nE→E +· nE→E + n·E→E + n·E→·E E→·E E→E· E→E·

NFANFA

eight itemseight items

eight stateseight states nn++

E'→·EE'→·E E'→E·E'→E·

E→·E+nE→·E+n EE→n·→n·

EE

εεεε

E→E·+nE→E·+n E→E+·nE→E+·n E→E+n·E→E+n·

εε

EE εε

E→·nE→·n nn

DFADFA

E'→·EE'→·EE→·E+nE→·E+nE→·nE→·n

EE→n·→n·

E'→E·E'→E·E→E· +nE→E· +n

E→E+n·E→E+n·

EE

nn ++nn

00 11

22E→E+·nE→E+·n

33 44

Page 19: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

例:已知拓广文法例:已知拓广文法 GG[S[S]] ,求其,求其 LR(0)LR(0) 的分析表。的分析表。 SS S S S S aA | bB aA | bB A A cA | d cA | d B B cB | d cB | d

Sb.BB.cBB.d

解 : (1) 识别文法活前缀的识别文法活前缀的 DFADFA

Ad.

Ac.AA.cAA.d

Sa.AA.cAA.d

Bc.BB.cBB.d

S.SS.aAS.bB

start

SS.AcA.

SaA.

A

d

d

A

ca

b

S

SbB.

BcB.

Bd.

B

d

d

B

c

c

c

00

11

22

33

44

55

66

7788

99

1010

1111核心项核心项闭包项闭包项

所有的闭包项都是初始项所有的闭包项都是初始项

Page 20: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

状态状态 actionaction gotogoto

a b c d $a b c d $ S A BS A B

0011223344556677889910101111

s2 s3s2 s3 accacc s5 s6s5 s6 s8 s9s8 s9r1 r1 r1 r1 r1r1 r1 r1 r1 r1 s5 s6s5 s6r4 r4 r4 r4 r4r4 r4 r4 r4 r4r2 r2 r2 r2 r2r2 r2 r2 r2 r2 s8 s9s8 s9r6 r6 r6 r6 r6r6 r6 r6 r6 r6r3 r3 r3 r3 r3r3 r3 r3 r3 r3r5 r5 r5 r5 r5r5 r5 r5 r5 r5

1 1    4 4 77

1010

11 11

(2)LR(0)(2)LR(0) 分析表分析表

(1) 识别文法活前缀的识别文法活前缀的 DFADFA

Ad.

Ac.AA.cAA.d

Sa.AA.cAA.d

Sb.BB.cBB.d

Bc.BB.cBB.d

S.SS.aAS.bB

start

SS.

AcA.

SaA.

A

d

d

A

ca

b

S

SbB.

BcB.

Bd.

B

d

d

B

c

c

c

00

11

22

33

66

77

88

99

1010

1111

4455

0 S0 SSS1 S1 SaAaA2 S2 SbBbB3 A3 AcAcA4 A4 Ad d 5 B5 BcBcB6 B6 Bdd

Page 21: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

输入输入 aa11 ... a ... aii ... a ... ann $ $

LRLR驱动程序驱动程序

分析表分析表

输出输出栈栈 ssmm

XXmm

ssm-1m-1

XXm-1m-1

......ss00 action gotoaction goto

●● LRLR 分析器的结构和工作过程分析器的结构和工作过程

Page 22: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● The LR (0) parsing algorithm LRLR 分析算法分析算法Let s be the current state (at the top of the parsing Let s be the current state (at the top of the parsing stack).Then actions are defined as follows:stack).Then actions are defined as follows:

1. 1. If state s contains any item of the form A → α·Xβ, where X If state s contains any item of the form A → α·Xβ, where X is a is a terminalterminal. Then the action is to . Then the action is to shiftshift the current input the current input token on to the stack. token on to the stack. If this token is X. and state s contains item A → α·Xβ, then If this token is X. and state s contains item A → α·Xβ, then the new state to be the new state to be pushpushed on the stack is the state containing ed on the stack is the state containing the item A → α·Xβ. If this token is not X for some item in the item A → α·Xβ. If this token is not X for some item in state s of the form just described, an state s of the form just described, an errorerror is declared. is declared.

Page 23: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

2. 2. If state s contains any If state s contains any complete itemcomplete item (an item of the form A (an item of the form A→γ→γ··), then the action is to ), then the action is to reducereduce by the rule A by the rule A →→ γ·γ·. . A reduction by the rule S` A reduction by the rule S` →→ S, where s is the start state, is equ S, where s is the start state, is equivalent to ivalent to acceptanceacceptance, provided the input is empty, and , provided the input is empty, and errorerror if the if the input is not empty.input is not empty. In all other cases, for new state is computed as follows. In all other cases, for new state is computed as follows. Remove Remove the string the string γγand all of its corresponding states from the parsing staand all of its corresponding states from the parsing stack (the string Y must be at the top of the stack, according to the wck (the string Y must be at the top of the stack, according to the way the DFA is constructed). Correspondingly, ay the DFA is constructed). Correspondingly, back upback up in the DFA in the DFA to the state from which the construction of to the state from which the construction of γγbegan (this must be thbegan (this must be the state uncovered by the removal of e state uncovered by the removal of γγ). Again, by the construction ). Again, by the construction of the DFA, this state must contain an item of the form B of the DFA, this state must contain an item of the form B →→ α·α·AAββ.. Push Push A onto the stack, and A onto the stack, and push push (as the new state) the state con(as the new state) the state containing the item B taining the item B →→ ααAA·β·β. (Note that this corresponds to follow. (Note that this corresponds to following the transition on A in the DFA, which is indeed reasonable, sining the transition on A in the DFA, which is indeed reasonable, since we are pushing A onto the stack.) ce we are pushing A onto the stack.)

Page 24: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

输入:一个输入串输入:一个输入串 ww 和文法和文法 GG 的一张的一张 LRLR 分析表分析表 MM 。。输出:若输出:若 w w L(G),L(G), 输出输出 ww 的一个自底向上的分析;的一个自底向上的分析; 否则,输出一个出错表示。否则,输出一个出错表示。方法:分别置放方法:分别置放 ss00 到栈中和到栈中和 w$w$ 到输入缓冲器中到输入缓冲器中 ;; 置置 ipip 指向指向 w$w$ 的第一个符号;的第一个符号; repeat forever beginrepeat forever begin 令令 ss 是栈顶状态且是栈顶状态且 aa 是是 ipip 所指向的符号所指向的符号 if if action[s,a] = shift saction[s,a] = shift s then begin then begin 将将 aa 和和 ss 先后压入栈内;先后压入栈内; 使使 ipip 指向输入串中的下一个符号;指向输入串中的下一个符号; endend

● ● LRLR 分析算法分析算法

else if else if action[s,a] = reduce Aaction[s,a] = reduce A then begin then begin 从栈顶弹出从栈顶弹出 2*|2*||| 个符号;个符号; 令令 ss 是当前栈顶状态;是当前栈顶状态; 把把 AA 和和 goto[sgoto[s,A],A] 先后入栈;先后入栈; 输出产生式输出产生式 AA endend else if else if action[s,a] = acceptaction[s,a] = accept then then returnreturn else else error( )error( ) end end

Page 25: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● exampleexample

例:已知文法例:已知文法 G[A]G[A] ,求其,求其 LR(0)LR(0) 的分析表,并判断的分析表,并判断 ((a)) L(G)?∈((a)) L(G)?∈ A A ( A ) | a ( A ) | a

(3)LR(0)(3)LR(0) 分析表分析表(2) 识别文法活前缀的识别文法活前缀的 DFADFA

解解 : : (1)(1) 拓广文法拓广文法

(4)(4) 分析过程分析过程

Page 26: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

4.4.3 SLR(1) Parsing SLR(1)4.4.3 SLR(1) Parsing SLR(1) 分析分析

Let s be the current state (at the top of the parsing stack). TheLet s be the current state (at the top of the parsing stack). Then. actions are defined as follows: n. actions are defined as follows:

1. If state s contains any item of form A → α·Xβ,where X is a 1. If state s contains any item of form A → α·Xβ,where X is a tterminalerminal, and X is the next token in the input string, then the ac, and X is the next token in the input string, then the action is to tion is to shift shift the current input token onto the stack, and the nthe current input token onto the stack, and the new state to be ew state to be pushpushed on the stack is the state containing the iteed on the stack is the state containing the item A → α·Xβ.m A → α·Xβ.

● ● The SLR(1) parsing algorithmThe SLR(1) parsing algorithm

Page 27: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

2 If state s contains the 2 If state s contains the complete itemcomplete item A → γ·, and A → γ·, and the next tokethe next token in the inupt string is in Follow(A)n in the inupt string is in Follow(A), then the action is to , then the action is to reducereduce by the rule A → γ. by the rule A → γ. A A reductionreduction by the rule S' →S, where s is the start state, is eq by the rule S' →S, where s is the start state, is equivalent touivalent to acceptance acceptance; this will happen only if the next input tok; this will happen only if the next input token is $. In all other cases, the new state is computed as follows. en is $. In all other cases, the new state is computed as follows. RemoveRemove the siring Y and all of its corresponding states from t the siring Y and all of its corresponding states from the parsing stack. Correspondingly, he parsing stack. Correspondingly, back upback up in the DFA to the st in the DFA to the state from which the construction of γ began. By construction, this ate from which the construction of γ began. By construction, this state must contain an item of the form B → α·Aβ. state must contain an item of the form B → α·Aβ. PushPush A onto t A onto the stack, and he stack, and pushpush the state containing the item B → αA·β. the state containing the item B → αA·β.

3. If the next input token is such that neither of the above two ca3. If the next input token is such that neither of the above two cases applies, an ses applies, an errorerror is declared . is declared .

Page 28: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

A grammar is an SLR(l) grammar if the application of the above A grammar is an SLR(l) grammar if the application of the above SLR( 1 ) parsing rules results in no ambiguity. In particular, a SLR( 1 ) parsing rules results in no ambiguity. In particular, a grammar is SLR( 1) if and only if, for any state s, the following grammar is SLR( 1) if and only if, for any state s, the following two conditions are satisfied:two conditions are satisfied:

1. For any item A → α·Xβin s with X a 1. For any item A → α·Xβin s with X a terminalterminal, there is , there is nono complete item B → γ. in s with X in Follow(B).complete item B → γ. in s with X in Follow(B).

2. For any two complete items A → α· and B →β· in s, Follow(A) 2. For any two complete items A → α· and B →β· in s, Follow(A) ∩ Follow(B) is ∩ Follow(B) is emptyempty..

● ● ConditionsConditions

Page 29: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

若有效项目集中存在冲突动作若有效项目集中存在冲突动作 ::I = { X I = { X . b . b, , AA . , . , BB . } . }

将将 bb 移进栈移进栈将将归约为归约为 AA

将将归约为归约为 BB

设当前输入符号为设当前输入符号为 a,a,1. 1. 若若 a = b, a = b, 则移进则移进 ;;2. 2. 若若 aaFollow(A), Follow(A), 则用则用 AA 进行归约进行归约 ;;3. 3. 若若 aaFollow(B), Follow(B), 则用则用 BB 进行归约进行归约 ;;4. 4. 其余情况报错其余情况报错 ..

● ● SLR(1)SLR(1) 分析分析

Page 30: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● SLRSLR 分析算法分析算法输入:一个拓广文法输入:一个拓广文法 GG输出:对于输出:对于 GG 的分析表的的分析表的 action action 子表和子表和 gotogoto 子表子表方法:方法:

1. 1. 构造构造 GG 的的 LR(0)LR(0) 项目集规范族。项目集规范族。2. 2. 对于状态对于状态 IIii 的分析动作如下:的分析动作如下: (a) (a) 若若 AA . aB . aB I Iii 且 且 go (Igo (Ii i ,a)=,a)= IIjj

action[i,a] = shift jaction[i,a] = shift j (b) (b) 若若 AA . . I Iii, , 对于所有对于所有 a a Follow(A) Follow(A)

action[i,a] = reduce Aaction[i,a] = reduce A , A , A S S (c) (c) 若若 SSS. S. I Iii, action[i, $]= accept, action[i, $]= accept3. 3. 若若 go(Igo(Iii, A) = I, A) = Ijj, A, AVVNN , , 则 则 goto[i,A] = jgoto[i,A] = j4. 4. 分析表其余位置为分析表其余位置为 errorerror

Page 31: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

SLR(SLR(1))SLR(SLR(1)) 算法算法::如果文法如果文法 GG 按上述算法构造出的分析按上述算法构造出的分析表不存在冲突动作,则称表不存在冲突动作,则称 GG 为为 SLRSLR 文法。类似地,不难文法。类似地,不难定义定义 LR(0)LR(0) 文法。文法。

若将上述算法的若将上述算法的 2(b)2(b) 步中的步中的 aaFollow(A)Follow(A) 改为改为 aaVVTT{$}{$},则由此修改后的算法所定义的文法,称为,则由此修改后的算法所定义的文法,称为 LR(0)LR(0) 文法文法。。

问题问题 . . 如何定义如何定义 LR(0)LR(0) 文法?文法?

Page 32: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● exampleexample

解: 解: (1)(1) 拓广文法拓广文法GG 的拓广文法的拓广文法 GG[E[E]] ::(0) E(0) E E (4) T E (4) TF F (1) E(1) EE+T (5) FE+T (5) F (E) (E) (2) E(2) ET (6) FT (6) F id id(3) T(3) TT*F T*F

(2)(2) 识别文法的活前缀的 识别文法的活前缀的 DFADFA (3)SLR(1)(3)SLR(1) 分析表分析表 (4)(4) 分析过程分析过程

例:已知文法例:已知文法 G[E]G[E] ,并用,并用 SLR(1)SLR(1) 方法分析方法分析 id*id+id L(G[E]) ?∈id*id+id L(G[E]) ?∈EEE+T | T E+T | T TTT*F | F T*F | F FF (E) | id (E) | id

Page 33: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

E E ’’EEE E E+TE+TE E TTT T T*FT*FT T FFF F (E)(E)F F idid

EE

EE’’EE E E EE+T+T

TTE E TT

T T TT*F*F

((

F F ( ( E) E) E E E+TE+TE E TTT T T*FT*FT T FF F F (E)(E) F F idid

II00

II11

II22

II66

FFT T FF

II33

F F idididid II55

TTII22

FFII33

ididII55((

E E E+ E+ T TT T T*FT*FT T FF F F (E)(E) F F idid

++

** T T T* T* F F F F (E)(E) F F idid

II77

F F (E (E ) ) E E EE+T +T

EEII88

TT E E E+ T E+ T T T TT*F*F

II99

FF II33

ididII55

((

FFT T T* F T* F

II1010idid

II44

((II44

**

II77

))

F F (E) (E)

++II66

II1111

GG :: (0) E(0) E E (1) E E (1) EE+TE+T (2) E(2) ET (3) TT (3) TT*F (4) TT*F (4) TFF (5) F(5) F (E) (6) F (E) (6) F id id

(2)(2) 识别文法的活前缀的 识别文法的活前缀的 DFADFA

Page 34: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

(2)(2) 识别文法的活前缀的 识别文法的活前缀的 DFADFA

E E ’’EEE E E+TE+TE E TTT T T*FT*FT T FFF F (E)(E)F F idid

EE

EE’’EE E E EE+T+T

TT

E E TT T T TT*F*F

((

F F ( ( E) E) E E E+TE+TE E TTT T T*FT*FT T FF F F (E)(E) F F idid

II00

II11

II22

II66

FFT T FF

II33

F F idididid II55

TT II22

FF II33

ididII55((

E E E+ E+ T TT T T*FT*FT T FF F F (E)(E) F F idid

++

**T T T* T* F F F F (E)(E) F F idid

II77

F F (E (E ) ) E E EE+T +T

EEII88

TT E E E+ T E+ T T T TT*F*F

II99

FF II33

idid II55

((

FFT T T* F T* F

II1010idid

II44

((II44

**II77

))

F F (E) (E)

++ II66

II1111

Page 35: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

II11:E´:E´EE I I22: E : E T T I I99: E : E E+T E+T E E EE+T T +T T T T *F T *F T T T *F *F

I={X I={X bb , A , A , B , B }}

若若 {b}{b}FOLLOW(A) FOLLOW(A) FOLLOW(B)= FOLLOW(B)=

则,面对当前读入符号则,面对当前读入符号 aa ,状态,状态 II 的解决方法:的解决方法: 1. 1. 若若 a=b,a=b, 则移进。则移进。 2. 2. 若若 a≠b, a≠b, 且且 aa FOLLOW(A), FOLLOW(A), 则用则用 AA 进行归约。进行归约。 3. 3. 若若 a≠b, a≠b, 且且 aa FOLLOW(B), FOLLOW(B), 则用则用 BB 进行归约。进行归约。 4. 4. 此外,报错。此外,报错。这种解决方法是比较简单的,因此称作这种解决方法是比较简单的,因此称作 SLRSLR分析,由此构造的分析表,称作分析,由此构造的分析表,称作 SLRSLR 分析表分析表。。

Page 36: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

对于表达式文法的例子,对于表达式文法的例子, FOLLOWFOLLOW 集如下:集如下:

II11:{ E:{ E’’EE E EEE+T}+T}II22:{E:{ETT T T T T *F} *F}II99:{E :{E E+T E+T T T T T *F}*F}

GG ::(0) E(0) E E (4) T E (4) TF F (1) E(1) EE+T (5) FE+T (5) F (E) (E) (2) E(2) ET (6) FT (6) F id id(3) T(3) TT*F T*F

II11:FOLLOW(E:FOLLOW(E’)∩{’)∩{+}=Φ+}=ΦII22: FOLLOW(E: FOLLOW(E)∩{*)∩{*}=Φ}=ΦII99: FOLLOW(E: FOLLOW(E)∩{*)∩{*}=Φ}=Φ∴∴ 可用可用 SLR(1)SLR(1) 方法实现方法实现

FOLLOWFOLLOW 集集E‘E‘ $$EE $$ ,),,), ++TT $$ ,),,), ++ ,, **FF $$ ,),,), ++ ,, **

Page 37: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

(3)SLR(3)SLR 分析表分析表 Follow(E)={$, +,)}Follow(E)={$, +,)}

ACTIONACTION GOTOGOTO++ ** (( )) idid $$ EE TT FF

00 S4S4 S5S5 11 22 3311 S6S6 ACCACC22 R2R2 S7S7 R2R2 R2R233 R4R4 R4R4 R4R4 R4R444 S4S4 S5S5 88 22 3355 R6R6 R6R6 R6R6 R6R666 S4S4 S5S5 99 3377 S4S4 S5S5 101088 S6S6 S11S1199 R1R1 S7S7 R1R1 R1R1

1010 R3R3 R3R3 R3R3 R3R31111 R5R5 R5R5 R5R5 R5R5

Page 38: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

(4) id*id+id(4) id*id+id 的的 LRLR 分析过程分析过程分析栈 输入串 动作

(1) 0(2) 0id5(3) 0F3(4) 0T2(5) 0T2*7(6) 0T2*7id5(7) 0T2*7F10(8) 0T2(9) 0E1(10) 0E1+6(11) 0E1+6id5(12) 0E1+6F3(13) 0E1+6T9(14) 0E1

id*id+id$*id+id$*id+id$*id+id$id+id$

+id$+id$+id$+id$

id$$$$$

shift reduce by Fid reduce by TF shift shift reduce by Fid reduce by TT*F reduce by ET shift shift reduce by Fid reduce by TF reduce by EE+T accept

Page 39: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● exampleexample

例:已知文法例:已知文法 G[E]G[E] ,求其,求其 SLR(1)SLR(1) 的分析表,并判断的分析表,并判断 n+n L(G)?∈n+n L(G)?∈ E E→→E + n | nE + n | n

(3)SLR(1)(3)SLR(1) 分析表分析表(2) 识别文法活前缀的识别文法活前缀的 DFADFA

解解 : : (1)(1) 拓广文法拓广文法

(4)(4) 分析过程分析过程

Page 40: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● exampleexample

例:已知文法例:已知文法 G[S]G[S] ,求其,求其 SLR(1)SLR(1) 的分析表,并判断的分析表,并判断 ( )( ) L(G)?∈( )( ) L(G)?∈ S → (S)S| S → (S)S|εε

(3)SLR(1)(3)SLR(1) 分析表分析表(2) 识别文法活前缀的识别文法活前缀的 DFADFA

解解 : : (1)(1) 拓广文法拓广文法

(4)(4) 分析过程分析过程

Page 41: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

4.4.4 General LR(1) and LALR(1) Parsing 4.4.4 General LR(1) and LALR(1) Parsing 一般的一般的 LR(1)LR(1) 和和 LALR(1)LALR(1) 分析分析

(part 1)(part 1).Given an LR(1) item [A→α·Xγ,a], where X is any sy.Given an LR(1) item [A→α·Xγ,a], where X is any symbol (terminal or nonterminal ), there is a transition on X to mbol (terminal or nonterminal ), there is a transition on X to the item [A→ αX·γ,a]the item [A→ αX·γ,a]

(part 2)(part 2). Given an LR(1) item [A→α·Bγ,a], where B is a nonte. Given an LR(1) item [A→α·Bγ,a], where B is a nonterminal, there arerminal, there areεε-transitions to items [B→·β,b] for every pr-transitions to items [B→·β,b] for every production B →βand oduction B →βand for every token bfor every token b in First(γa). in First(γa).

● ● Definition of LR(1) transitionsDefinition of LR(1) transitions

Page 42: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● exampleexample

例:已知文法例:已知文法 G[S]G[S] ,求其,求其 LR(1)LR(1) 的分析表,并判断 的分析表,并判断 id:=id L∈id:=id L∈(G)?(G)?

S → id | V := ES → id | V := EV→ idV→ idE E →→ V | n V | n

(3)LR(1)(3)LR(1) 分析表分析表(2) 识别文法活前缀的识别文法活前缀的 DFADFA

解解 : : (1)(1) 拓广文法拓广文法

(4)(4) 分析过程分析过程

Page 43: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

解 :(1) 拓广文法

S’ → SS’ → SS → id S → id S → V := ES → V := EV→ idV→ idE E →→ V V E E →→ n n

(2) 识别文法活前缀的识别文法活前缀的 DFADFA

S→V. :=E,$S→V. :=E,$

S→V:=.E,$S→V:=.E,$E→.V ,$E→.V ,$E→.n ,$E→.n ,$V→ .id,$V→ .id,$

Sid. ,$ Vid. ,:=

S’ → .S ,$S’ → .S ,$S → .id ,$S → .id ,$S → .V := E ,$S → .V := E ,$V→ .id ,:=V→ .id ,:=

start

SS. ,$

E→V. ,$E→V. ,$V

:=

id

V

S S→V:=E.,$S→V:=E.,$

E→n. ,$E→n. ,$n

E

0011

22

3344

55

66

77

V→id. ,$V→id. ,$id 88

Page 44: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

(3) LR(1)LR(1) 分析表分析表id := n $ S V E

012345678

Action goto

S2 1 3ACC

R3 R1S4

S8 S7 S6 6 5R2R4R5R3

S→V. :=E,$S→V. :=E,$

S→V:=.E,$S→V:=.E,$E→.V ,$E→.V ,$E→.n ,$E→.n ,$V→ .id,$V→ .id,$

Sid. ,$ Vid. ,:=

S’ → .S ,$S’ → .S ,$S → .id ,$S → .id ,$S → .V := E ,$S → .V := E ,$V→ .id ,:=V→ .id ,:=

start

SS. ,$

E→V. ,$E→V. ,$V

:=

id

V

S S→V:=E.,$S→V:=E.,$

E→n. ,$E→n. ,$n

E

0011

22

3344

55

66

77

V→id. ,$V→id. ,$id 88

0 S’ → S0 S’ → S1 S → id 1 S → id 2 S → V := E2 S → V := E3 V→ id3 V→ id4 E 4 E →→ V V 5 E 5 E →→ n n

Page 45: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

(4) id:=id(4) id:=id 的的 LRLR 分析过程分析过程分析栈 输入串 动作

(1) 0(2) 0id2(3) 0V3(4) 0V3:=4(5) 0V3:=4id8(6) 0V3:=4V6(7) 0V3:=4E5(8) 0S1

id:=idid:=id $:=id:=id $

:=id:=id $idid $

$$$$

shiftreduce by V → id→ idshiftshiftreduce by V → id→ idreduce by E E →→ V V reduce by S → V := ES → V := Eaccept

id := n $ S V E012345678

Action goto

S2 1 3ACC

R3 R1S4

S8 S7 S6 6 5R2R4R5R3

0 S’ → S0 S’ → S1 S → id 1 S → id 2 S → V := E2 S → V := E3 V→ id3 V→ id4 E 4 E →→ V V 5 E 5 E →→ n n

Page 46: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● exampleexample

例:已知文法例:已知文法 G[A]G[A] ,求其,求其 LR(1)LR(1) 的分析表,并判断的分析表,并判断 ((a)) L(G)?∈((a)) L(G)?∈ A A ( A ) | a ( A ) | a

(3)LR(1)(3)LR(1) 分析表分析表(2) 识别文法活前缀的识别文法活前缀的 DFADFA

解解 : : (1)(1) 拓广文法拓广文法

(4)(4) 分析过程分析过程

Page 47: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

解 :(1) 拓广文法

A’A’ A AA A ( A ) | a ( A ) | a

(2) 识别文法活前缀的识别文法活前缀的 DFADFA

Aa. ,$ Aa. ,)

A(.A) ,) A.(A) ,)A.a ,)

A(.A),$ A.(A) ,)A.a ,)

A.A ,$A.(A),$A.a ,$

start

AA. ,$

A(A .),)

A(A .),$

A

aa

A

((

a

AA(A) .,$

A(A) .,)

)

)

00

11

22

33

44

55

66

77

88

99

Page 48: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

(3) LR(1)LR(1) 分析表分析表( ) a $ A

0123456789

Action goto

S2 S3 1ACC

S5 S6 4R2

S7S5 S6 8

R2R1

S9R1

Aa. ,$ Aa. ,)

A(.A) ,) A.(A) ,)A.a ,)

A(.A),$ A.(A) ,)A.a ,)

A.A ,$A.(A),$A.a ,$

start

AA. ,$

A(A .),)

A(A .),$

A

aa

A

((

a

AA(A) .,$

A(A) .,)

)

)

00

11

22

33

44

55

66

77

88

99(

0 AA1 A(A)2 Aa

Page 49: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

(4) ((a))(4) ((a)) 的的 LRLR 分析过程分析过程

分析栈 输入串 动作(1) 0(2) 0(2(3) 0(2(5(4) 0(2(5a6(5) 0(2(5A8(6) 0(2(5A8)9(7) 0(2A4(8) 0(2A4)7(9) 0A1

((a))((a)) $(a))(a)) $ a))a)) $

)))) $))$)$)$$$

shiftshiftshiftreduce by Aashiftreduce by A(A)shiftreduce by A(A)accept

0 AA1 A(A)2 Aa

( ) a $ A0123456789

Action goto

S2 S3 1ACC

S5 S6 4R2

S7S5 S6 8

R2R1

S9R1

Page 50: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● exampleexample

例:已知文法例:已知文法 G[S]G[S] ,求其,求其 LR(1)LR(1) 的分析表的分析表 S S C C C C C C c C | d c C | d

(3)LR(1)(3)LR(1) 分析表分析表(2) 识别文法活前缀的识别文法活前缀的 DFADFA

解解 : : (1)(1) 拓广文法拓广文法

(4)(4) 分析过程分析过程

Page 51: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

◆ ◆ FIRST PRINCIPLE OF LALR(1) PARSINGFIRST PRINCIPLE OF LALR(1) PARSINGThe core of a state of the DFA of The core of a state of the DFA of LR(l)LR(l) items is a state of the items is a state of the DFA of DFA of LR(0)LR(0)-items.-items.

◆ ◆ SECOND PRINCIPLE OF LALR(1) PARSINGSECOND PRINCIPLE OF LALR(1) PARSINGGiven two states sGiven two states s11 and s and s22 of the DFA of LR(l) items that have of the DFA of LR(l) items that have the same corethe same core, suppose there is a transition on the symbol X , suppose there is a transition on the symbol X from sfrom s11 to a state t to a state t11. Then there is also a transition on X from . Then there is also a transition on X from state sstate s22 to a state t to a state t22, and the states t, and the states t11 and t and t22 have the same have the same core.core.

● ● Definition of LALR(1) transitionsDefinition of LALR(1) transitions

Page 52: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

对于初始状态对于初始状态 II00 ,有效项目,有效项目 [S[S.S, $].S, $] 称为称为 II00 的核;而的核;而对于非初始状态,则其中 “点不在最左端”的有效项对于非初始状态,则其中 “点不在最左端”的有效项目称为它的核。目称为它的核。

● ● 相关术语相关术语1)1) 同心项同心项

如果两个如果两个 LR(1)LR(1) 项目集去掉搜索符之后是相同的,则称项目集去掉搜索符之后是相同的,则称这两个项目集具有相同的心。这两个项目集具有相同的心。2)2) 核核

在在 LR(1)LR(1) 项目集规范族中, 项目集规范族中, LALRLALR 分析法合并同分析法合并同心项以减少状态的数目;在存储心项以减少状态的数目;在存储 LR(1)LR(1) 有效项目集时,有效项目集时,仅存储其中的核。仅存储其中的核。 由于同心项的合并,使项目的搜索符与活前缀的对由于同心项的合并,使项目的搜索符与活前缀的对应关系不唯一,降低了分析器的识别能力。应关系不唯一,降低了分析器的识别能力。

Page 53: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● exampleexample

例:已知文法例:已知文法 G[S’]G[S’] ,求其,求其 LR(1)LR(1) 的分析表的分析表 SSSS S SaAd | bBd | aBe | bAeaAd | bBd | aBe | bAe A Acc BBcc

(3)LR(1)(3)LR(1) 分析表分析表(2) 识别文法活前缀的识别文法活前缀的 DFADFA

解解 : : (1)(1) 拓广文法拓广文法

(4)(4) 分析过程分析过程

Page 54: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

II00:: SS.S $.S $ SS.aAd $.aAd $ SS.bBd $.bBd $ SS.aBe $.aBe $ SS.bAe $.bAe $

II11:(I:(I00 S) S) SSS. $S. $

II22:(I:(I00 a) a) SSa.Ad $a.Ad $ SSa.Be $a.Be $ AA.c d.c d B B.c e.c e

II33:(I:(I00 b) b) SSb.Bd $b.Bd $ SSb.Ae $b.Ae $ AA.c e.c e B B.c d.c d

II44:(I:(I22 A) A) SSaA.d $aA.d $II55:(I:(I22 B) B) SSaB.e $aB.e $II66:(I:(I22 c) c) AAc. dc. d B Bc. ec. eII77:(I:(I33 B) B) SSbB.d $bB.d $II88:(I:(I33 A) A) SSbA.e $bA.e $

II99:(I:(I33 c) c) AAc. ec. e B Bc. dc. dII1010:(I:(I44 d) d) SSaAd. $aAd. $II1111:(I:(I44 e) e) SSaBe. $aBe. $II1212:(I:(I77 d) d) SSbBd. $bBd. $II1313:(I:(I88 e) e) SSbAe. $bAe. $

II6969: A: Ac. d/ec. d/e B Bc. d/ec. d/e

合并同心项合并同心项

解解 :: 识别文法活前缀的识别文法活前缀的 DFADFA

若将同心项若将同心项 II66 和和 II99 合并,则得到项目集合并,则得到项目集 II6969: A: Ac. d/ec. d/e B Bc. d/ec. d/e该项目集含“归约该项目集含“归约 -- 归约”冲突。归约”冲突。因此,文法因此,文法 GG 是是 LR(1)LR(1) 文法,但不是文法,但不是 LALRLALR 文法。文法。

Page 55: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● LALR(1)LALR(1) 分析表的原理性构造方法分析表的原理性构造方法 构造构造 LR(1)LR(1) 项目集族项目集族 , , 如果它不存在冲突如果它不存在冲突 , , 就把同就把同心集合并在一起。若心集合并在一起。若合并后不存在归约合并后不存在归约 -- 归约冲突归约冲突,则按,则按这个集族构造文法这个集族构造文法 LALR(1)LALR(1) 分析表。分析表。● ● 算法: 算法: LALRLALR 分析表的构造分析表的构造输入:拓广文法输入:拓广文法 G'G'输出:对于输出:对于 G'G' 的的 LALR(1)LALR(1) 分析表分析表方法:方法: 1. 1. 构造文法的构造文法的 LR(1)LR(1) 项目集族项目集族 C={IC={I00, I, I11, …, I, …, Inn}} 2.2. 合并合并 CC 中的同心集,得到中的同心集,得到 C'={JC'={J00, J, J11, …, J, …, Jmm}} 3. 3. 从从 C'C' 出发构造出发构造 actionaction 表表 ::

(a) (a) 若若 [A[A . a . a,b] ,b] J Jii 且 且 go (Jgo (Ji i ,a)=,a)= JJjj

置置 action[i,a] = shift jaction[i,a] = shift j (b) (b) 若若 [A[A. ,a] . ,a] J Jii,, 置置 action[i,a] = r Aaction[i,a] = r A , A , A SS (c) (c) 若若 [S[SS., $] S., $] J Jii,, 置置 action[i, $]= acceptaction[i, $]= accept

4. 4. 若若 go(Igo(Ikk, A) = J, A) = Jjj, A, AVVNN , , 则 则 goto[k,A] = jgoto[k,A] = j 5. 5. 分析表其余位置为分析表其余位置为 errorerror

Page 56: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● LALRLALR 分析表的有效构造方法分析表的有效构造方法 前算法在构造前算法在构造 LALRLALR 分析表时,耗费的存储空间与分析表时,耗费的存储空间与 LRLR(1)(1) 算法完全相同,代价还是太大。可以从两个方面改进:算法完全相同,代价还是太大。可以从两个方面改进:

11、存储有效项目集时,、存储有效项目集时,只存储它们的核只存储它们的核,每当需要完整,每当需要完整的有效项目集时,再根据核来计算。的有效项目集时,再根据核来计算。22、直接生成、直接生成 LALRLALR 项目集规范族的核,这是有效构造方项目集规范族的核,这是有效构造方法的关键。法的关键。

注意,注意, LALRLALR 项目的搜索符项目的搜索符一般是一般是与该项目相关的非终结与该项目相关的非终结符号的符号的 FollowFollow 集的子集集的子集,这正是,这正是 LALRLALR 分析法比分析法比 SLRSLR 分析分析法强的原因。法强的原因。

Page 57: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● 确定核项目的搜索符是确定核项目的搜索符是自生的自生的或者是或者是传播的传播的算法:算法:for for 有效项目集有效项目集 II 的核的核 KK 中的每一项目中的每一项目 BB.. do begin do begin J = closure({B J = closure({B.., $});, $}); for J for J 中的每一个项目中的每一个项目 [A[A.X.X, a] do, a] do 如果如果 a≠$a≠$ ,则有效项目集,则有效项目集 go(I, X)go(I, X) 中的核项目中的核项目 [A[AX.X., a], a] 中的搜索符是自生的,否则就是传播的。中的搜索符是自生的,否则就是传播的。

● ● 核项目的搜索符无非有两种产生途径:核项目的搜索符无非有两种产生途径:若搜索符从其父核传递下来,则称这样的搜索符为“若搜索符从其父核传递下来,则称这样的搜索符为“传播传播的的”;否则,称搜索符为“”;否则,称搜索符为“自生的自生的”。”。

● ● 直接生成直接生成 LALRLALR 项目集规范族的项目集规范族的核核的方法:的方法:(1)(1) 构造构造 LR(0)LR(0) 项目集规范族的核及其自动机。项目集规范族的核及其自动机。(2)(2) 为为 LR(0)LR(0) 项目集规范簇中的每个核项目配上适当的搜索符。项目集规范簇中的每个核项目配上适当的搜索符。

Page 58: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● exampleexample

例:已知文法例:已知文法 G[A]G[A] ,求其,求其 LALR(1)LALR(1) 的分析表,并判断的分析表,并判断 ((a)) L∈((a)) L∈(G)?(G)? A A ( A ) | a ( A ) | a

(3)LALR(1)(3)LALR(1) 分析表分析表(2) 识别文法活前缀的识别文法活前缀的 DFADFA

解解 : : (1)(1) 拓广文法拓广文法

(4)(4) 分析过程分析过程

Page 59: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

解 :(1) 拓广文法

A’A’ A AA A ( A ) | a ( A ) | a

(2) 识别文法活前缀的识别文法活前缀的 DFADFA

Aa. , $ )

A(.A),$ ) A.(A) ,)A.a ,)

A.A ,$A.(A),$A.a ,$

startAA. ,$

A(A .),$ )

a

A

(

(

a

A

A(A) .,$ ))

0011

22

33

44

55

(3)LALR(1)(3)LALR(1) 分析表分析表(4)(4) 分析过程分析过程

Page 60: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● exampleexample

例:已知文法例:已知文法 G[S]G[S] ,求其,求其 LALR(1)LALR(1) 的分析表的分析表 S S C C C C C C c C | d c C | d

(3)LALR(1)(3)LALR(1) 分析表分析表(2) 识别文法活前缀的识别文法活前缀的 DFADFA

解解 : : (1)(1) 拓广文法拓广文法

(4)(4) 分析过程分析过程

Page 61: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

Chapter 4.44.4 Bottom-Up Parsing 自底向上的分析4.4.1 OVERVIEW OF BOTTOM-UP PARSING 4.4.1 OVERVIEW OF BOTTOM-UP PARSING 自底向上分析概述自底向上分析概述4.4.2 FINIT AUTOMATA OF LR(0) ITEMS AND LR(0) 4.4.2 FINIT AUTOMATA OF LR(0) ITEMS AND LR(0)

PARSINGPARSING LR(0)LR(0) 项的有穷自动机与项的有穷自动机与 LR(0) LR(0) 分析分析4.4.3 SLR(1) Parsing SLR(1)4.4.3 SLR(1) Parsing SLR(1) 分析分析4.4.4 General LR(1) and LALR(1) Parsing 4.4.4 General LR(1) and LALR(1) Parsing 一般的一般的 LR(1)LR(1) 和和 LALR(1)LALR(1) 分析 分析

ENDEND

Page 62: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

Chapter 44 Syntax Analysis 语法分析

4.1  4.1  语法分析器的作用语法分析器的作用4.2  4.2  上下文无关文法 上下文无关文法 4.3 4.3  自顶向下语法分析自顶向下语法分析4.4 4.4  自底向上语法分析自底向上语法分析

ENDEND

Page 63: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

● ● 作业作业

请将作业压缩成一个文件发送!发送的文件名:班级名学号姓名章节名作业 .rar 例:计科 030105××× 第 5章作业 .rar发送地址: [email protected]

编译原理 : 4.1 4.2 4.3 编译原理与实践 : 4.8 4.9 4.33 4.34 4.35 4.37 4.39 4.40

编译原理与实践 : 3.3 4.8 4.9 5.5 5.12

Page 64: Chapter  4.4 Bottom-Up Parsing 自底向上的分析

程序程序 22语法语法分析分析程序程序

语法树或其他形式语法树或其他形式

源程序源程序input

output

● ● 上机程序上机程序要求

程序程序 11 LLLL或或 LRLR分析表分析表上下文上下文无关文法无关文法

input

output