您将如何解析缩进(Python样式)? [英] How would you parse indentation (python style)?

查看:94
本文介绍了您将如何解析缩进(Python样式)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何定义解析器和词法分析器规则,以解析使用缩进定义范围的语言.

How would you define your parser and lexer rules to parse a language that uses indentation for defining scope.

我已经在Google上进行了搜索,并找到了一种聪明的方法来解析它,方法是在词法分析器中生成INDENT和DEDENT令牌.

I have already googled and found a clever approach for parsing it by generating INDENT and DEDENT tokens in the lexer.

如果我遇到有趣的事情,我会更深入地解决这个问题并发布答案,但是我希望看到解决该问题的其他方法.

I will go deeper on this problem and post an answer if I come to something interesting, but I would like to see other approaches to the problem.

正如查理(Charlie)指出的那样,已经有了另一个非常相似的线程,如果没有的话一样.应该删除我的帖子吗?

As Charlie pointed out, there is already another thread very similar if not the same. Should my post be deleted?

推荐答案

这是一种假设,因为这取决于您的词法分析器和解析器使用的技术,但是最简单的方法似乎是拥有BEGINBLOCK和ENDBLOCK令牌类似于C中的花括号.使用越位规则" ,您的词法分析器需要跟踪一系列的知识水平.当缩进级别增加时,为解析器发出BEGINBLOCK;否则,为解析器发出BEGINBLOCK.当压痕级别降低时,发出ENDBLOCK并弹出堆栈.

This is kind of hypothetical, as it would depend on what technology you have for your lexer and parser, but the easiest way would seem to be to have BEGINBLOCK and ENDBLOCK tokens analogous to braces in C. Using the "offsides rule" your lexer needs to keep track of a stack of indendtation levels. When the indent level increases, emit a BEGINBLOCK for the parser; when the indentation level decreases, emit ENDBLOCK and pop levels off the stack.

这是关于SO的另一种讨论 ,顺便说一句.

这篇关于您将如何解析缩进(Python样式)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆