目前,我正在学习基于解析(一种简单的计算)的词法和解析(基于F#工具集),而且我无法使用整个字符串:
let lexeme = LexBuffer<_>.LexemeString
// ...
rule test = parse
| digit+ { Console.WriteLine("1_" + (lexeme lexbuf)); test lexbuf; }
| '+' { Console.WriteLine("2_" + (lexeme lexbuf)); test lexbuf; }
| '-' { Console.WriteLine("3_" + (lexeme lexbuf)); test lexbuf; }
| '*' { Console.WriteLine("4_" + (lexeme lexbuf)); test lexbuf; }
| '/' { Console.WriteLine("5_" + (lexeme lexbuf)); test lexbuf; }
| '(' { Console.WriteLine("6_" + (lexeme lexbuf)); test lexbuf; }
| ')' { Console.WriteLine("7_" + (lexeme lexbuf)); test lexbuf; }
| eof { () }注:例如,为了确保我提供的整个字符串被消耗,我必须编写最后的'test lexbuf'
由于在我的实际实现中没有这样做,我只需要阅读第一个数字,这就是我得到的全部。
rule calculator = parse
| digit+ { NUMBER (Convert.ToInt32(lexeme lexbuf)) }
| '+' { PLUS }
| '-' { MINUS }
| '*' { TIMES }
| '/' { DIV }
| '(' { LPAREN }
| ')' { RPAREN }
| eof { EOF }我看到了许多类似的例子。我错过了什么。
发布于 2014-09-03 12:39:22
问题是,你根本不能指望雷克萨斯自己前进。对我来说,把它看作一条小溪有助于理解正在发生的事情。
前进只与解析器结合使用。解析器将继续要求lexer返回令牌。
https://stackoverflow.com/questions/25569751
复制相似问题