如何在词法分析器中生成两个令牌

huangapple go评论56阅读模式
英文:

How to make two tokens in lexer

问题

在你的词法分析器中,你想要为一个字符使用两个标记。例如,在词法分析器中,你写了:

"(" { return tLPAREN; return tLPAREN2; }

但是标记 tLPAREN2 不起作用。

如何使两个标记能够处理一个字符。这将帮助我解决语法冲突。

英文:

I'm currently making a RUBY language parser. My question is that I want to use two tokens for one character. For example, in the lexer I write

"(" { return tLPAREN; return tLPAREN2; }
while the token tLPAREN2 DON,T WORK.

How to make two tokens can handle 1 character. It would help me get rid of conflicts in grammar.


Flex version 2.6.3 source code and win_bison based on Bison version 2.7

"(" { return tLPAREN; return tLPAREN2; }
while the token tLPAREN2 DON,T WORK.

答案1

得分: 0

你可以使用起始状态来实现类似的操作。你会识别到该标记,并让动作设置一个(独占的)起始状态,回退输入并返回第一个标记。然后在起始状态中,你将再次识别到该标记,然后返回到正常的起始状态并返回第二个标记。所以类似这样:

%x Paren2

%%

"("             { BEGIN(Paren2); yyless(0); return tLPAREN; }
<Paren2>"("     { BEGIN(INITIAL); return tLPAREN2; }

请注意调用 yyless(0) 的部分,它会将 ( “推回”以便被第二条规则再次识别。

英文:

You can do something like this with start states. You'll recognize the token and have the action set an (exclusive) start state, back up the input and return the first token. Then in the start state, you'll recognize the token again and go back to the normal start state and return the second token. So something like:

%x Paren2

%%

&quot;(&quot;             { BEGIN(Paren2); yyless(0); return tLPAREN; }
&lt;Paren2&gt;&quot;(&quot;     { BEGIN(INITIAL); return tLPAREN2; }

Note the call to yyless(0) which "pushes back" the ( to be recognized again by the second rule.

huangapple
  • 本文由 发表于 2023年6月12日 01:44:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定