📄 t-gen-testsuite.st
字号:
)
tokenSpec: '
<space> : [\s\t\r]+ {ignoreDelimiter} ;
'
grammarSpec: '
E : T Ec {liftRightChild} ;
Ec : ''+'' E {Plus} ;
Ec : {nil} ;
T : P Tc {liftRightChild} ;
Tc : ''*'' T {Times} ;
Tc : {nil} ;
P : ''a'' {A} ;
P : ''b'' {B} ;
P : ''c'' {C} ;
'
ifFail: failBlock.
Transcript cr; show: 'STOP: ' , testName!
basicLRParserGenerationTest1
"TranslatorGenerator basicLRParserGenerationTest1"
| tgen failBlock testName |
testName := 'T-gen LR Parser Generation Test 1'.
tgen := self new setGrammarModeToLR.
failBlock := [self error: testName, ' failed.'].
Transcript cr; show: 'START: ' , testName; cr.
tgen
parseInputs: #(
''
'aabb'
)
compareDTTo: (Array
with: '#S\ . ''<epsilon>''\' withCRs
with: '#S\ . ''a''\ . #S\ . . ''a''\ . . #S\ . . . ''<epsilon>''\ . . ''b''\ . ''b''\' withCRs
)
compareSASTTo: (Array
with: '#Core\' withCRs
with: '#Pair\ . #Pair\ . . #Core\' withCRs
)
tokenSpec: ''
grammarSpec: '
S : ''a'' S ''b'' {Pair} ;
S : {Core} ;
'
ifFail: failBlock.
Transcript cr; show: 'STOP: ' , testName!
basicLRParserGenerationTest2
"TranslatorGenerator basicLRParserGenerationTest2"
| tgen failBlock testName |
testName := 'T-gen LR Parser Generation Test 2'.
tgen := self new setGrammarModeToLR.
failBlock := [self error: testName, ' failed.'].
Transcript cr; show: 'START: ' , testName; cr.
tgen
parseInputs: (Array
with: ' program\ var a b c : integer\ begin\ a := 3;\ b := a * 4 * 5;\ begin\ c := a + b * 19;\ b := b * c + 1;\ a := (a + 1) * a;\ end\ c := b * (c + 5);\ end\' withCRs
)
compareDTTo: (Array
with: '#Z\ . ''program''\ . #Decls\ . . ''var''\ . . #IdList\ . . . #Name\ . . . . ''<id>''\ . . . #IdList\ . . . . #Name\ . . . . . ''<id>''\ . . . . #IdList\ . . . . . #Name\ . . . . . . ''<id>''\ . . '':''\ . . ''integer''\ . #Stmts\ . . ''begin''\ . . #SL\ . . . #S\ . . . . #Name\ . . . . . ''<id>''\ . . . . '':=''\ . . . . #E\ . . . . . #T\ . . . . . . #P\ . . . . . . . ''<number>''\ . . . . '';''\ . . . #SL\ . . . . #S\ . . . . . #Name\ . . . . . . ''<id>''\ . . . . . '':=''\ . . . . . #E\ . . . . . . #T\ . . . . . . . #P\ . . . . . . . . #Name\ . . . . . . . . . ''<id>''\ . . . . . . . ''*''\ . . . . . . . #T\ . . . . . . . . #P\ . . . . . . . . . ''<number>''\ . . . . . . . . ''*''\ . . . . . . . . #T\ . . . . . . . . . #P\ . . . . . . . . . . ''<number>''\ . . . . . '';''\ . . . . #SL\ . . . . . #Stmts\ . . . . . . ''begin''\ . . . . . . #SL\ . . . . . . . #S\ . . . . . . . . #Name\ . . . . . . . . . ''<id>''\ . . . . . . . . '':=''\ . . . . . . . . #E\ . . . . . . . . . #E\ . . . . . . . . . . #T\ . . . . . . . . . . . #P\ . . . . . . . . . . . . #Name\ . . . . . . . . . . . . . ''<id>''\ . . . . . . . . . ''+''\ . . . . . . . . . #T\ . . . . . . . . . . #P\ . . . . . . . . . . . #Name\ . . . . . . . . . . . . ''<id>''\ . . . . . . . . . . ''*''\ . . . . . . . . . . #T\ . . . . . . . . . . . #P\ . . . . . . . . . . . . ''<number>''\ . . . . . . . . '';''\ . . . . . . . #SL\ . . . . . . . . #S\ . . . . . . . . . #Name\ . . . . . . . . . . ''<id>''\ . . . . . . . . . '':=''\ . . . . . . . . . #E\ . . . . . . . . . . #E\ . . . . . . . . . . . #T\ . . . . . . . . . . . . #P\ . . . . . . . . . . . . . #Name\ . . . . . . . . . . . . . . ''<id>''\ . . . . . . . . . . . . ''*''\ . . . . . . . . . . . . #T\ . . . . . . . . . . . . . #P\ . . . . . . . . . . . . . . #Name\ . . . . . . . . . . . . . . . ''<id>''\ . . . . . . . . . . ''+''\ . . . . . . . . . . #T\ . . . . . . . . . . . #P\ . . . . . . . . . . . . ''<number>''\ . . . . . . . . . '';''\ . . . . . . . . #SL\ . . . . . . . . . #S\ . . . . . . . . . . #Name\ . . . . . . . . . . . ''<id>''\ . . . . . . . . . . '':=''\ . . . . . . . . . . #E\ . . . . . . . . . . . #T\ . . . . . . . . . . . . #P\ . . . . . . . . . . . . . ''(''\ . . . . . . . . . . . . . #E\ . . . . . . . . . . . . . . #E\ . . . . . . . . . . . . . . . #T\ . . . . . . . . . . . . . . . . #P\ . . . . . . . . . . . . . . . . . #Name\ . . . . . . . . . . . . . . . . . . ''<id>''\ . . . . . . . . . . . . . . ''+''\ . . . . . . . . . . . . . . #T\ . . . . . . . . . . . . . . . #P\ . . . . . . . . . . . . . . . . ''<number>''\ . . . . . . . . . . . . . '')''\ . . . . . . . . . . . . ''*''\ . . . . . . . . . . . . #T\ . . . . . . . . . . . . . #P\ . . . . . . . . . . . . . . #Name\ . . . . . . . . . . . . . . . ''<id>''\ . . . . . . . . . . '';''\ . . . . . . ''end''\ . . . . . #SL\ . . . . . . #S\ . . . . . . . #Name\ . . . . . . . . ''<id>''\ . . . . . . . '':=''\ . . . . . . . #E\ . . . . . . . . #T\ . . . . . . . . . #P\ . . . . . . . . . . #Name\ . . . . . . . . . . . ''<id>''\ . . . . . . . . . ''*''\ . . . . . . . . . #T\ . . . . . . . . . . #P\ . . . . . . . . . . . ''(''\ . . . . . . . . . . . #E\ . . . . . . . . . . . . #E\ . . . . . . . . . . . . . #T\ . . . . . . . . . . . . . . #P\ . . . . . . . . . . . . . . . #Name\ . . . . . . . . . . . . . . . . ''<id>''\ . . . . . . . . . . . . ''+''\ . . . . . . . . . . . . #T\ . . . . . . . . . . . . . #P\ . . . . . . . . . . . . . . ''<number>''\ . . . . . . . . . . . '')''\ . . . . . . . '';''\ . . ''end''\' withCRs
)
compareSASTTo: (Array
with: '#Program\ . #Decls\ . . #IdList\ . . . ''a''\ . . . ''b''\ . . . ''c''\ . #Stmts\ . . #StmtList\ . . . #Assign\ . . . . ''a''\ . . . . ''3''\ . . . #Assign\ . . . . ''b''\ . . . . #Times\ . . . . . ''a''\ . . . . . #Times\ . . . . . . ''4''\ . . . . . . ''5''\ . . . #Stmts\ . . . . #StmtList\ . . . . . #Assign\ . . . . . . ''c''\ . . . . . . #Plus\ . . . . . . . ''a''\ . . . . . . . #Times\ . . . . . . . . ''b''\ . . . . . . . . ''19''\ . . . . . #Assign\ . . . . . . ''b''\ . . . . . . #Plus\ . . . . . . . #Times\ . . . . . . . . ''b''\ . . . . . . . . ''c''\ . . . . . . . ''1''\ . . . . . #Assign\ . . . . . . ''a''\ . . . . . . #Times\ . . . . . . . #Plus\ . . . . . . . . ''a''\ . . . . . . . . ''1''\ . . . . . . . ''a''\ . . . #Assign\ . . . . ''c''\ . . . . #Times\ . . . . . ''b''\ . . . . . #Plus\ . . . . . . ''c''\ . . . . . . ''5''\' withCRs
)
tokenSpec: '
<id> : [a-z]+ ;
<number> : [0-9]+ ;
<space> : [\s\t\r]+ {ignoreDelimiter} ;
'
grammarSpec: '
Z : ''program'' Decls Stmts {Program} ;
Decls : ''var'' IdList '':'' ''integer'' {Decls} ;
IdList : Name IdList {liftRightChild} ;
IdList : Name {IdList} ;
Stmts : ''begin'' SL ''end'' {Stmts} ;
SL : S SL {liftRightChild} ;
SL : Stmts SL {liftRightChild} ;
SL : S {StmtList} ;
SL : Stmts {StmtList} ;
S : Name '':='' E '';'' {Assign} ;
E : E ''+'' T {Plus} ;
E : T ;
T : P ''*'' T {Times} ;
T : P;
P : ''('' E '')'' ;
P : Name ;
P : <number> {Number} ;
Name : <id> {Id} ;
'
ifFail: failBlock.
Transcript cr; show: 'STOP: ' , testName!
basicLRParserGenerationTest3
"TranslatorGenerator basicLRParserGenerationTest3"
| tgen failBlock testName |
testName := 'T-gen LR Parser Generation Test 3'.
tgen := self new setGrammarModeToLR.
failBlock := [self error: testName, ' failed.'].
Transcript cr; show: 'START: ' , testName; cr.
tgen
parseInputs: #(
'a'
'a + b * c + a'
)
compareDTTo: (Array
with: '#E\ . #T\ . . #P\ . . . ''a''\ . . #Tc\ . . . ''<epsilon>''\ . #Ec\ . . ''<epsilon>''\' withCRs
with: '#E\ . #T\ . . #P\ . . . ''a''\ . . #Tc\ . . . ''<epsilon>''\ . #Ec\ . . ''+''\ . . #E\ . . . #T\ . . . . #P\ . . . . . ''b''\ . . . . #Tc\ . . . . . ''*''\ . . . . . #T\ . . . . . . #P\ . . . . . . . ''c''\ . . . . . . #Tc\ . . . . . . . ''<epsilon>''\ . . . #Ec\ . . . . ''+''\ . . . . #E\ . . . . . #T\ . . . . . . #P\ . . . . . . . ''a''\ . . . . . . #Tc\ . . . . . . . ''<epsilon>''\ . . . . . #Ec\ . . . . . . ''<epsilon>''\' withCRs
)
compareSASTTo: (Array
with: '#A\' withCRs
with: '#Plus\ . #A\ . #Plus\ . . #Times\ . . . #B\ . . . #C\ . . #A\' withCRs
)
tokenSpec: '
<space> : [\s\t\r]+ {ignoreDelimiter} ;
'
grammarSpec: '
E : T Ec {liftRightChild} ;
Ec : ''+'' E {Plus} ;
Ec : {nil} ;
T : P Tc {liftRightChild} ;
Tc : ''*'' T {Times} ;
Tc : {nil} ;
P : ''a'' {A} ;
P : ''b'' {B} ;
P : ''c'' {C} ;
'
ifFail: failBlock.
Transcript cr; show: 'STOP: ' , testName!
basicLRParserGenerationTest4
"TranslatorGenerator basicLRParserGenerationTest4"
| tgen failBlock testName |
testName := 'T-gen LR Parser Generation Test 4'.
tgen := self new setGrammarModeToLR.
failBlock := [self error: testName, ' failed.'].
Transcript cr; show: 'START: ' , testName; cr.
tgen
parseInputs: #(
'(a ; a)'
'(a , a)'
)
compareDTTo: (Array
with: '#E\ . #P\ . . ''(''\ . . #A\ . . . ''a''\ . . '';''\ . . #A\ . . . ''a''\ . . '')''\' withCRs
with: '#E\ . #P\ . . ''(''\ . . #V\ . . . ''a''\ . . '',''\ . . #V\ . . . ''a''\ . . '')''\' withCRs
)
tokenSpec: '
<space> : [\s\t\r]+ {ignoreDelimiter} ;
'
grammarSpec: '
E : P ;
P : ''a'' ;
P : ''('' A '';'' A '')'' ;
P : ''('' V '','' V '')'' ;
V : ''a'' ;
A : ''a'' ;
'
ifFail: failBlock.
Transcript cr; show: 'STOP: ' , testName!
basicLRParserGenerationTest5
"TranslatorGenerator basicLRParserGenerationTest5"
| tgen failBlock testName |
testName := 'T-gen LR Parser Generation Test 5'.
tgen := self new setGrammarModeToLR.
failBlock := [self error: testName, ' failed.'].
Transcript cr; show: 'START: ' , testName; cr.
tgen
parseInputs: #(
'aab'
'aaa'
'baa'
'bab'
)
compareDTTo: (Array
with: '#S\ . ''a''\ . #B\ . . #A\ . . . ''a''\ . ''b''\' withCRs
with: '#S\ . ''a''\ . #D\ . . ''a''\ . ''a''\' withCRs
with: '#S\ . ''b''\ . #B\ . . #A\ . . . ''a''\ . ''a''\' withCRs
with: '#S\ . ''b''\ . #D\ . . ''a''\ . ''b''\' withCRs
)
tokenSpec: ''
grammarSpec: '
S : ''a'' B ''b'' ;
S : ''a'' D ''a'' ;
S : ''b'' B ''a'' ;
S : ''b'' D ''b'' ;
B : A ;
A : ''a'' ;
D : ''a'' ;
'
ifFail: failBlock.
Transcript cr; show: 'STOP: ' , testName!
basicLRParserGenerationTest6
"TranslatorGenerator basicLRParserGenerationTest6"
| tgen failBlock testName |
testName := 'T-gen LR Parser Generation Test 6'.
tgen := self new setGrammarModeToLR.
failBlock := [self error: testName, ' failed.'].
Transcript cr; show: 'START: ' , testName; cr.
tgen
parseInputs: #()
compareDTTo: #()
tokenSpec: ''
grammarSpec: ' S : E ; E : E ''+'' E | E ''*'' E | ''a'' ;'
ifFail: failBlock.
(tgen isLL1 | tgen isSLR1 | tgen isLALR1 | tgen isLR1)
ifTrue: failBlock
ifFalse: [Transcript show: '*'].
Transcript cr; show: 'STOP: ' , testName!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -