不能直接理解為每個非終結符的follow集,否則,lemon得到的只是簡單的slr(1)分析器。注意到followset是定義成專案(config)的屬性,在這裡應該理解為向前搜尋符。驗證如下,注意,在程式lemon.c中要開啟test巨集定義:
語法檔案(test.y[1]):
%include
%token_type
%default_type
%type start
%type s
%type b
%type a
%type b
%syntax_error
start ::= s.
s ::= b b.
b ::= a b.
b ::= b.
執行>lemon –c test.y
得到test.out如下:
state 0:
start ::= * s
[$]to (state 1) start ::= s *
to (state 0) s ::= * b b
s ::= * b b
[$]to (state 2) s ::= b * b
b ::= * a b
[a b]
to (state 4) b ::= a * b
b ::= * b
[a b]
to (state 6) b ::= b *
a shift 4
b shift 6
start accept
s shift 1
b shift 2
state 1:
(0) start ::= s *
[$]from (state 0) start ::= * s
$ reduce 0
state 2:
s ::= b * b
[$]to (state 3) s ::= b b *
to (state 2) b ::= * a b
to (state 2) b ::= * b
from (state 0) s ::= * b b
b ::= * a b
[$]to (state 4) b ::= a * b
b ::= * b
[$]to (state 6) b ::= b *
a shift 4
b shift 6
b shift 3
state 3:
(1) s ::= b b *
[$]from (state 2) s ::= b * b
$ reduce 1
state 4:
b ::= * a b
[$ a b]
to (state 4) b ::= a * b
b ::= a * b
[$ a b]
to (state 5) b ::= a b *
to (state 4) b ::= * a b
to (state 4) b ::= * b
from (state 0) b ::= * a b
from (state 4) b ::= * a b
from (state 2) b ::= * a b
b ::= * b
[$ a b]
to (state 6) b ::= b *
a shift 4
b shift 6
b shift 5
state 5:
(2) b ::= a b *
[$ a b]
from (state 4) b ::= a * b
$ reduce 2
a reduce 2
b reduce 2
state 6:
(3) b ::= b *
[$ a b]
from (state 0) b ::= * b
from (state 2) b ::= * b
from (state 4) b ::= * b
$ reduce 3
a reduce 3
b reduce 3
文法的first集和follow集如下:
first
follow
s』a, b#s
a, b#b
a, b
#, a, b
顯然,生成的test.out是lalr(1)分析器。
[1] 陳火旺,劉春林等.程式語言編譯原理[m].第3版,北京:國防工業出版社,2023年1月:115.
怎樣寫乙個lemon的spj
所有的都從argv裡面讀入 argv 1 輸入檔案 argv 2 選手輸出檔案 argv 3 標準輸出檔案 argv 4 單個測試點分值 argv 5 輸出最終得分的檔案 argv 6 輸出錯誤報告的檔案 include using namespace std ifstream fin,fout,f...
Python中的descriptor中的一點疑問
在我的印象中,類中的函式是可以有兩種呼叫方式的,如下 class b def func self return 10 b b b.func b.func b 於是,在之前研究descriptor的時候,我就有了幾點困惑 我將 更換成了如下兩個版本 版本1 import time class lazy...
python中的dict Python中的dict
dict python內建了字典 dict的支援,dict全稱dictionary,在其他語言中也稱為map,使用鍵 值 key value 儲存,具有極快的查詢速度。d print dict get michael d michael add a element d adam 67 print d...