Parsing 在解析时,我需要添加什么才能将monadUserState与alex一起使用?
我试图写一个程序,将理解一种语言,其中嵌入的意见是允许的。例如:Parsing 在解析时,我需要添加什么才能将monadUserState与alex一起使用?,parsing,haskell,alex,Parsing,Haskell,Alex,我试图写一个程序,将理解一种语言,其中嵌入的意见是允许的。例如: /* Here's a comment /* This comment is further embedded */ second comment is closed Must close first comment */ 这应该被视为一条注释(因此,除非之前只看到一条注释,否则不会在第一次看到时停止*/它会看到) 这在C语言中是一个很容易解决的问题,我可以简单地使用一个计数器,当它看到注释打开时计数器递增,当它看到注释关闭
/* Here's a comment
/* This comment is further embedded */ second comment is closed
Must close first comment */
这应该被视为一条注释(因此,除非之前只看到一条注释,否则不会在第一次看到时停止*/它会看到)
这在C语言中是一个很容易解决的问题,我可以简单地使用一个计数器,当它看到注释打开时计数器递增,当它看到注释关闭时计数器递减。如果计数器为0,则我们处于“代码段”
然而,如果没有Haskell中的状态,它就更具挑战性了
我读过monadUserState,它应该允许跟踪这种精确解析类型的状态。然而,除了这本书,我找不到太多的阅读材料
当我试图编译时,它给出了错误
templates\wrappers.hs:213:16: Not in scope: `alexEOF`
应该注意的是,我直接从“基本”包装器更改为“monadUserState”,而没有更改代码(我不知道要使用它需要添加什么)。它说这必须在用户代码中初始化:
data AlexState = AlexState {
alex_pos :: !AlexPosn, -- position at current input location
alex_inp :: String, -- the current input
alex_chr :: !Char, -- the character before the input
alex_bytes :: [Byte], -- rest of the bytes for the current char
alex_scd :: !Int, -- the current startcode
alex_ust :: AlexUserState -- AlexUserState will be defined in the user program
}
我是一个有点不懂词法的人,我根本不确定我应该在这里添加什么,以使它至少可以编译。。。然后我就可以担心事情的逻辑了。更新:此处提供的工作示例: alex github repo中的文件“tiger.x”包含一个如何使用monadUserState包装跟踪嵌入注释的示例 嗯,不幸的是,这个例子没有编译,但是其中的想法应该是可行的 基本上,这些行执行嵌入式注释处理:
<0> "/*" { enterNewComment `andBegin` state_comment }
<state_comment> "/*" { embedComment }
<state_comment> "*/" { unembedComment }
<state_comment> . ;
<state_comment> \n { skip }
并将alexEOF
定义为:
alexEOF = return EOF
请参阅alex repo中的tests/tokens\u monadUserState\u bytestring.x文件以了解此示例。我忘了感谢您的回复。感谢您在回复中投入的时间和精力,这对我帮助很大。
alexEOF = return EOF