我花了一天时间尝试一个简单的编译器程序,但仍然难以理解这个函数。
let rec expression tokens =
let (trm, tokens') = term tokens in
这里 so (trm, tokens') 是一个元组, term 是一个函数还是一个变量?元组如何=两个变量或一个函数
expTail trm tokens'
and
expTail trm tokens =
match tokens with
Tokenizer.PlusTok::tokens' ->
let (trm', tokens'') = term tokens' in expTail (Plus(trm, trm')) tokens''
| Tokenizer.MinusTok::tokens' ->
let (trm', tokens'') = term tokens' in expTail (Minus(trm, trm')) tokens''
| _ -> (trm, tokens)
这条线是什么意思?它似乎没有调用任何东西。为什么现在是 (trm, tokens) 而不是 (trm', tokens'')
and
term tokens = let (trm, tokens') = factor tokens in termTail trm tokens'
也不确定这条线是什么意思。
and
termTail trm tokens =
match tokens with
Tokenizer.TimesTok::tokens' ->
let (trm', tokens'') = factor tokens' in termTail (Times(trm, trm')) tokens''
| Tokenizer.DivTok::tokens' ->
let (trm', tokens'') = factor tokens' in termTail (Div(trm, trm')) tokens''
| Tokenizer.ModTok::tokens' ->
let (trm', tokens'') = factor tokens' in termTail (Mod(trm, trm')) tokens''
| _ -> (trm, tokens)
and
factor tokens =
match tokens with
Tokenizer.LParenTok::tokens' ->
let (expr, tokens'') = expression tokens' in
(match tokens'' with
Tokenizer.RParenTok::tokens''' -> (expr, tokens''')
| _ -> raise (Syntax "Bad factor, failed to find )\n"))
| (Tokenizer.IntTok i)::tokens' -> (Bits i, tokens')
| _ -> raise (Syntax ("Bad factor."))
多谢你们!