如何在 Racket 中对此处字符串进行词法分析?

问题描述 投票:0回答:1

我正在尝试弄清楚如何使用 Racket 词法分析器工具对 HERE-STRING 等内容进行词法分析。例如:

string(END-MARKER)
any text goes here except the end marker
END-MARKER

所有这些都应该被词法化为具有值“的标记” 除了结束标记之外的任何文本都放在这里 “。我需要一种方法来捕获 END-MARKER。我认为有一些方法可以使用词法分析器宏来实现它,但我还没有找到任何关于如何使用它们的好的文档。

不幸的是,我现在没有看到任何方法,所以我无法在此处包含任何代码。应该是这样的:

#lang racket

(require parser-tools/lex
         (prefix-in : parser-tools/lex-sre)

(define-lex-abbrev here-string
  (:: "string(" <...>))
racket
1个回答
0
投票
[16:07]soegaard: Make two lexers lex1 and lex2 using lexer. 

One lexer handles "normal mode" and the other handles "here string" mode.

In lex1 make a cluase [trigger action-expr] where the trigger is the pattern for string(END-MARKER)
and in the action-expr use lexeme to extract the concrete END-MARKER. Store the end marker
in a global variable. 

(define lexer-mode #f)   ; #f = standard mode, some string = string mode
 
[16:09]soegaard: Then define a lex function that calls lex1 or lex2 according to the lexer mode.
And remember to set the lexer mode back in lex2.
© www.soinside.com 2019 - 2024. All rights reserved.