首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >学习: Lexer不前进

学习: Lexer不前进
EN

Stack Overflow用户
提问于 2014-08-29 13:51:50
回答 1查看 132关注 0票数 0

目前,我正在学习基于解析(一种简单的计算)的词法和解析(基于F#工具集),而且我无法使用整个字符串:

代码语言:javascript
复制
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'

由于在我的实际实现中没有这样做,我只需要阅读第一个数字,这就是我得到的全部

代码语言:javascript
复制
rule calculator = parse
  | digit+  { NUMBER (Convert.ToInt32(lexeme lexbuf)) }
  | '+'     { PLUS }
  | '-'     { MINUS }
  | '*'     { TIMES }
  | '/'     { DIV }
  | '('     { LPAREN }
  | ')'     { RPAREN }
  | eof     { EOF }

我看到了许多类似的例子。我错过了什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-03 12:39:22

问题是,你根本不能指望雷克萨斯自己前进。对我来说,把它看作一条小溪有助于理解正在发生的事情。

前进只与解析器结合使用。解析器将继续要求lexer返回令牌。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25569751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档