野牛在同一循环中的转到标签 [英] goto label in the same loop in Bison

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

问题描述

我正在使用Bison和Flex进行解析器,我想创建一个转到标签"语句,但是我想检查标签是否存在于同一代码块中(括号{},循环之间).

I am making a parser with Bison and Flex and I want to create a "goto label" statement, but I want to check if the label exists in the same block of code (between brackets { }, loop, etc).

有没有检查这种事情的功能?

Is there a function that checks such things?

推荐答案

您的问题意味着您在构建语言翻译器/编译器时缺少一些背景知识,因此,也许只有一个小教程可以帮助您解决问题.我希望你不要介意.

Your question implies that you are missing some background context in building language translators/compilers, so perhaps a small tutorial will assist you in solving your problem. I hope you don't mind.

按惯例,计算机语言的处理分为一系列步骤(有时称为阶段或阶段).每个步骤处理整个任务的一个组成部分,每个步骤使后续步骤更易于实现.

The processing of computer languages is conventionally divided up into a sequence of steps (sometimes called phases or passes). Each step handles a component of the whole task, and each step makes the step that follows easier to implement.

步骤将是:

  1. 词法分析
  2. 语法分析(也称为解析)
  3. 语义分析
  4. 中间代码代码生成
  5. 优化
  6. 代码生成/发射

在执行所有这些步骤的同时,通常还维护一个全局数据结构,称为符号表.符号表的目的是跟踪正在编译的程序中使用的名称以及所有这些符号的属性. 转到中的标签就是这样的符号,它将记录在符号表中.

Concurrent with all those steps a global data structure is usually maintained, called the Symbol Table. The purpose of the symbol table is to keep track of the names used in the program being compiled and the attributes of all those symbols. A label in a goto is such a symbol that would be recorded in the symbol table.

第一步是词法分析,是在将不同的词素转换为语言标记后发现符号的.并非词法分析器找到的所有词素都将导致在符号表中进行输入.诸如语言中的关键字,注释之类的事物通常不具有符号表条目.标识符(例如 goto 中的标签)将导致在符号表中进行输入.第一次遇到这样的标识符时,将创建一个新的表条目.可能会在遇到该行的地方做一个记录.标识符的后续出现不会导致创建新的表条目,但是可以更新信息.

The first step, lexical analysis, is where the symbols are discovered as the different lexemes are converted into tokens of the language. Not all lexemes found by the lexer will cause entries to be made into the symbol table. Things such as keywords in the language, comments and such like do not usually have symbol table entries. Identifiers, such as the label in a goto, would cause entries to be made in the symbol table. The first time such an identifier is encountered a new table entry would be made; a note might be made of the line where it was encountered. Subsequent occurrences of the identifier would not cause new table entries to be made, however information may be updated.

在词法阶段,由于标签引用可以是正向或反向 :

At the lexical stage no errors regarding label ordering could be generated as label references could be either forward or back:

转到 转发
...
后退:
...
前进:转到 后退

goto forward;
...
back:
...
forward: goto back;

第一次或第二次出现标签标识符都不会表示标签未定义.

Neither the first or second occurrence of a label identifier would indicate the label is undefined.

在语法分析中,我们将遇到类似的问题.语法规则将确定何时遇到有效的标签定义,以及何时找到有效的 goto 语句.但是,仍然没有足够的信息来确定所使用的标签是否有效.当涉及标签范围时,尤其如此.解析信息是必须在解析完成后通过查询符号表来解决的.解析器所能做的就是记录有效的解析,通常以解析树的形式记录.

In syntax analysis we would have a similar problem. The syntax rules would determine when a valid label definition is encountered and also when a valid goto statements is found. However there is still not enough information to decide if the label used is a valid one. This is particularly true when scoping of the label is concerned. Scoping information is something that would have to be resolved by consulting the symbol table after parsing has completed. All the parser can do is record the valid parse, usually in the form of a parse tree.

这是语义分析阶段,它在语法分析树上运行,该语法树具有所有可用信息,可以确定哪些 goto 标签有效,哪些无效.它通过将更多信息添加到符号表中来实现此目的,例如,作用域,记录已声明的标签以及在何处.然后可以查看哪些 goto 引用了未定义的标签并发出了适当的消息.

It is the semantic analysis stage, which operates on the parse tree that has all the information available to determine which goto labels are valid and which are not. It does this by adding further information to the symbol table, such as scoping, recording which labels have been declared and where. It is then possible to see which gotos refer to an undefined label and issue an appropriate message.

flex/bison(或lex/yacc)工具集是 compiler-compiler的常用形式.用于将编译器规范构建到编译器中的工具.还有许多其他可供选择.

The flex/bison (or lex/yacc) tool sets are commonly used form of compiler-compiler. Tools used to build the specifications of compilers into the compiler. There are many others to choose from.

flex/bison工具不包含用于符号表(或解析树)的程序包,对于使用这些工具实现自己的工具的程序员而言,这是必需的.一旦实现了自己的符号表数据结构并提供了确定存储在其中的符号属性的功能,便可以查询 goto 标签是否未定义.

The flex/bison tools do not include a package for a symbol table (or a parse tree) and it is necessary for programmers that use these tools to implement their own. Once you have implemented your own symbol table data structure and provided functions for determining attributes of the symbols stored within it, it then becomes possible to enquire if a goto label is undefined.

总而言之,答案是否定的.没有任何内置的方法可以执行所需的操作,因为它超出了您正在使用的工具的范围 .

So, in summary, the answer is no; there is no built in way of doing what you wanted as it is outside the scope of the tools you are using.

此示例在Algol 60中.
@rici所说的是什么.

This example is in Algol 60.
Which is what @rici said.

这篇关于野牛在同一循环中的转到标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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