使用smalltalk编写一个小解析器,如何使用创建的方法'标识符?
writing a petit parser using smalltalk, how to use created method 'identifier?
我目前有一个方法
pMain
| parser |
parser := 'proc' asParser, #space asParser, "<---- im trying to use the method identifier here - so i tried self identifier, instead of 'proc' asParser
#letter asParser plus, $( asParser,
'int' asParser,#space asParser,
#letter asParser plus, $) asParser,
#space asParser, 'corp' asParser.
^ parser
我也有这两种方法
1-关键词法
keywords
^ keywords ifNil: [
keywords := Set newFrom: #(
proc corp
if then else fi
do od
print as
while
for from to by
return
int string real array format bool
true false
mutable static
)
]
2-标识符法
identifier
^ ((#letter asParser , #word asParser star) flatten) >=> [ : ctxt : aBlock | | parse |
parse := aBlock value.
(self keywords includes: parse) ifTrue: [
PPFailure message: 'keyword matched' context: ctxt
] ifFalse: [
parse
]]
问题:pMain中的标识符解析器是如何使用的?
我喂它这条线
MyParser new pMain:= 'proc a( int a ) corp'
'proc' asParser
returns 接受字符串 'proc'
的解析器;这类似于 $p asParser
returns 一个接受字符 $p
.
的解析器
我猜你的问题是关于如何引用解析器产品。在 PPCompositeParser
的子类中,您可以通过创建一个 returns 其解析器的方法来做到这一点(您这样做了)。然后产品通过读取各自的同名实例变量来相互引用(你必须自己创建它们,除非你使用 PetitParser 工具)。
您可以在 documentation 中找到有关复合解析器的教程。
我目前有一个方法
pMain
| parser |
parser := 'proc' asParser, #space asParser, "<---- im trying to use the method identifier here - so i tried self identifier, instead of 'proc' asParser
#letter asParser plus, $( asParser,
'int' asParser,#space asParser,
#letter asParser plus, $) asParser,
#space asParser, 'corp' asParser.
^ parser
我也有这两种方法
1-关键词法
keywords
^ keywords ifNil: [
keywords := Set newFrom: #(
proc corp
if then else fi
do od
print as
while
for from to by
return
int string real array format bool
true false
mutable static
)
]
2-标识符法
identifier
^ ((#letter asParser , #word asParser star) flatten) >=> [ : ctxt : aBlock | | parse |
parse := aBlock value.
(self keywords includes: parse) ifTrue: [
PPFailure message: 'keyword matched' context: ctxt
] ifFalse: [
parse
]]
问题:pMain中的标识符解析器是如何使用的?
我喂它这条线
MyParser new pMain:= 'proc a( int a ) corp'
'proc' asParser
returns 接受字符串 'proc'
的解析器;这类似于 $p asParser
returns 一个接受字符 $p
.
我猜你的问题是关于如何引用解析器产品。在 PPCompositeParser
的子类中,您可以通过创建一个 returns 其解析器的方法来做到这一点(您这样做了)。然后产品通过读取各自的同名实例变量来相互引用(你必须自己创建它们,除非你使用 PetitParser 工具)。
您可以在 documentation 中找到有关复合解析器的教程。