如何手动编写(shell)词法分析器
我正在开发一个 shell,一个类似 bash 的小型 shell,没有脚本(如果 while ...) 我必须手工制作词法分析器/解析器(LL)。
因此词法分析器会将命令 (char *cmd) 转换为链接列表 (t_list *list)。 LL 解析器会将链表 (t_list *list) 转换为带有 语法
所以,我知道如何制作 LL 解析器,但我不知道如何标记我的命令。
例如:ps | grep ls>>文件 ;制作&& ./a.out
=> <代码>'ps''|' 'grep' 'ls' '>>' '文件' ';' ''制作'&&' './a.out'
谢谢。
(我不想使用任何发电机)
I'm working on a shell, a small bash-like shell, without scripting (if while ...)
I have to make the lexer/parser (LL) by hand.
So the lexer will transform the command (char *cmd) to a linked list (t_list *list).
And the LL parser will transform the linked list (t_list *list) to an AST (binary tree t_btree *root) with a grammar
So, I know how to make the LL parser but I don't know how to tokenize my command.
For example: ps | grep ls >> file ; make && ./a.out
=> 'ps' '|' 'grep' 'ls' '>>' 'file' ';' ''make '&&' './a.out'
Thanks.
(I don't wanna use any generator)
(这解释了Spudd86暗示的想法)。
您需要实现一个有限状态机。有以下状态:
&&
标记||
标记内部对于每个状态和下一个输入字符,您必须决定下一个状态是什么,是否输出令牌。例如:
制定所有规则是一件非常无聊的工作(当您必须调试生成的代码时,乐趣就开始了),因此大多数人使用代码生成器来做到这一点。
编辑:一些代码(抱歉,如果语法混乱;我通常用 C++ 编程)
(This explains the idea hinted by Spudd86).
You need to implement a finite state machine. There are the following states:
&&
token||
tokenFor each state and next input character, you have to decide what is the next state, and whether to output a token. For example:
It's much boring work to work out all the rules (the fun starts when you must debug the resulting code), so most people use code generators to do that.
Edit: some code (sorry if the syntax is messed-up; i usually program in C++)