英文:
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
%%
"(" { BEGIN(Paren2); yyless(0); return tLPAREN; }
<Paren2>"(" { BEGIN(INITIAL); return tLPAREN2; }
Note the call to yyless(0)
which "pushes back" the (
to be recognized again by the second rule.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论