我正在尝试使用fslex和fsyacc编写一个小型DSL解析器。 输入由两种不同语言的交织块组成,这两种语言需要不同的lexing规则。 如何编写我的fslex文件来支持它?
(我想类似的情况是如何为c语言定义一个fslex文件但是支持内联汇编,这需要不同的lexing规则?)
我目前的情况是这样的:
rule tokenize = parse | "core" { core lexbuf } ... and core = parse | ...问题是,一旦core解析器返回一个令牌,输入的下一部分就会传递给tokenize 。 但是我想留在core状态(原样)。 我怎么做?
谢谢!
I'm trying to write a small DSL parser using fslex and fsyacc. The input is composed of interleaving chunks of two different languages which require different lexing rules. How do I write my fslex file to support that?
(I guess a similar case would be how to define an fslex file for the c language but with support for inline assembly, which requires different lexing rules?)
What I have currently is something like this:
rule tokenize = parse | "core" { core lexbuf } ... and core = parse | ...The thing is, once a token gets returned by the core parser, the next part of the input gets passed to tokenize instead. However I want to stay (as it were) in the core state. How do I do that?
Thanks!
最满意答案
我实际上设法找到了自己的解决方案。 我定义了自己的tokenizer函数,它根据BufferLocalStore状态决定调用哪个tokenizer。
let mytokenizer (lexbuf : LexBuffer<char>) = if lexbuf.BufferLocalStore.["state"].Equals("core") then FCLexer.core lexbuf else FCLexer.tokenize lexbuf let aString (x : string) = let lexbuf = LexBuffer<_>.FromString x lexbuf.BufferLocalStore.["state"] <- "fc" let y = try (FCParser.PROG mytokenizer) lexbuf ...我稍微修改了我的fslex输入文件:
rule tokenize = parse | "core" { lexbuf.BufferLocalStore.["state"] <- "core"; core lexbuf } ...令人惊讶的是如何简单地提出问题可以引导您找到解决方案,我希望这可以帮助除了我以外的人:)
I actually managed to find a solution on my own. I defined my own tokenizer function which decides based on the BufferLocalStore state which tokenizer to call.
let mytokenizer (lexbuf : LexBuffer<char>) = if lexbuf.BufferLocalStore.["state"].Equals("core") then FCLexer.core lexbuf else FCLexer.tokenize lexbuf let aString (x : string) = let lexbuf = LexBuffer<_>.FromString x lexbuf.BufferLocalStore.["state"] <- "fc" let y = try (FCParser.PROG mytokenizer) lexbuf ...And I modified my fslex input file slightly:
rule tokenize = parse | "core" { lexbuf.BufferLocalStore.["state"] <- "core"; core lexbuf } ...Amazing how simply asking the question can lead you to the solution, and I hope this helps someone besides me :)
更多推荐
发布评论