解析while循环野牛 [英] Parsing a While Loop in Bison

查看:135
本文介绍了解析while循环野牛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是实现一个简单的while循环野牛的最好方法?如果它的确与众不同,我使用C,但我可以用C ++为好。

What is the best way to implement a simple while loop in Bison? If it makes a difference, I am using C, but I can use C++ as well.

推荐答案

什么你问并没有真正融入一个堆栈溢出的答案。这是pretty细致,很多书都写过了。

What you're asking doesn't really fit into a Stack Overflow answer. It's pretty detailed, and many books have been written about it.

最基本的答案是,你不要,而在野牛循环实现。野牛不是在这个意义上的语言。野牛建立从语法和行动是联想与该语法规则的分析器。

The basic answer is that you don't implement while loops in Bison. Bison is not a language in that sense. Bison builds a parser from a grammar and actions that are associate with rules in that grammar.

一个解析器是下推自动机,这是一个状态机附带一个堆栈。它需要令牌的线性序列,当在语法规则得到满足检测(或者,如果有一个错误)。当规则已经满足,解析器将执行附加到该规则的动作。在这里,令牌(典型值)的整数值对应的关键字,标识符和文字的语言。与野牛书面解析器通常依赖于一个单独的例程,称为词法扫描器,输入文本标记翻译。

A parser is a pushdown automaton, which is a state machine with a stack attached. It takes a linear sequence of tokens and detects when rules in the grammar have been satisfied (or if there's an error). When a rule has been satisfied, the parser will execute the action attached to that rule. Here, tokens are (typically) integer values that correspond to keywords, identifiers, and literals in the language. Parsers written with Bison usually rely on a separate routine, called a lexical scanner, to translate input text into tokens.

在此机器会直接让你实现一个while循环没有。取而代之的是,操作用于构建可以被进一步处理的输入的内部重新presentation。为文法是足够复杂有while循环,该再presentation通常需要一个树的形式,并且通常被称为抽象语法树(AST)。为了更具体,给定了输入文本:

Nothing in this machinery will directly allow you to implement a while loop. Instead, the actions are used to build an internal representation of the input that can be further processed. For grammars that are complicated enough to have while loops, this representation usually takes the form of a tree, and is commonly called an abstract syntax tree (AST). To be more concrete, given the input text:

while (i < n) { ... }

相应的AST可能是这样的:

a corresponding AST might look like this:

                        [while node]
                   _____/          \_____
             _____/                      \____
            /                                 \
       [operator <]                     [block subtree]
       /          \
      /            \
  [ID: i]        [ID: n]

本而节点需要两个子树:对应的继续状态的前pression子树( I&LT; N ),和块子树对应块( {...} )。

The while node expects two subtrees: an expression subtree corresponding to the continue condition (i < n), and a block subtree corresponding to the block ({ ... }).

鉴于while循环正确的AST,这是相当简单的用一些机械处理标识符和变量值处理AST的节点,结合来实现循环。

Given a proper AST for the while loop, it's reasonably straightforward to implement the loop by processing the nodes of the AST, in combination with some machinery for handling identifiers and variable values.

如果你给一个野牛正确的语法(即:适用于LALR(1)分析)和行动建立一个AST,你会得到一个程序,将标记流转换为AST。从AST执行循环是野牛的范围之外。

If you give Bison a proper grammar (ie: suitable for LALR(1) parsing) and actions that build an AST, you'll get a routine that will convert a stream of tokens into an AST. Executing the loop from that AST is outside the scope of Bison.

这篇关于解析while循环野牛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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