lex和yacc(符号表生成)(lex and yacc (symbol table generation))

编程入门 行业动态 更新时间:2024-10-28 06:29:05
lex和yacc(符号表生成)(lex and yacc (symbol table generation))

我是lex和yacc以及编译器设计的新手。 我想知道在哪个阶段(词法,句法或任何其他阶段)以及如何生成符号表?

我是否可以简要描述y.output文件,该文件是通过给yacc提供-v选项生成的。我试图查看它但没有得到太多信息。

我能否知道除了编译器设计之外还使用lex和yacc的其他应用程序。

I am new to lex and yacc and compiler design. I would like to know at which phase(lexical, syntactical or any other phase) and how the symbol table is generated?

Can I have a brief description of y.output file which is generated by giving -v option to yacc.I tried to looking into it but didn't get much info.

Could I know the other applications where lex and yacc are used apart from compiler designs.

最满意答案

符号表是一种全局数据结构,可用于编译器的所有阶段/阶段/过程。 这意味着可以从lex和yacc生成的组件中使用/访问它。

通常,当词汇分析器找到将存储在表中的令牌(例如标识符)时,从词法分析器访问符号表条目,它可以找到该条目并使用仅对词法分析器可用的信息更新它,如行号和字符位置,它还可以存储lexeme值(如果它尚不存在)。 现在可以在令牌的lval中返回符号表指针。

有些人喜欢从词法分析器返回指向lexeme本身(作为lval )的指针,并在那里进行初始符号表访问。 这具有以下优点:符号表不必对词法分析器可见,但是具有如下缺点:如上所述的词法分析器信息可能不再可用于与符号一起存储。 它的缺点通常是使得yacc的解析器操作更加“繁忙”,因为它们可能涉及管理符号表以及解析树。

符号表条目将在编译器的后续阶段中进一步更新,例如解析树的语义步行,其可以用类型信息注释符号条目并标记未声明的对象等。 当可以存储或需要目标特定信息时,将在目标代码生成期间再次使用符号表,并且在优化期间,当可以检查或甚至优化变量使用时,将再次使用符号表。

符号表是编译器编写者为您自己创建的数据结构。 lex或yacc没有为你做的功能。 当您编写的任何代码创建它时,它会生成!

y.output文件与符号表无关。 它是yacc如何将上下文无关语法转换为解析表的记录。 当你有一个模糊的语法并想知道在调试你的语法时导致shift / reduce或reduce / reduce错误的规则时,它很有用。

问题的最后一部分,这些工具有什么用途? lex是一种为状态机生成代码的工具,可识别您指定的模式。 它不必用于编写编译器。 一个有趣的用途是处理可由状态机处理的网络协议,例如TCP / IP数据报等。 类似地,yacc用于匹配由上下文无关语法描述的序列。 这些不必是程序,但可以是符号,字段或数据项的其他复杂序列。 它们通常只是文本的一部分,这是该工具的正统使用。

你的问题的这些部分听起来像某人可能为那些参加过编程课程的学生写的考试问题!

A symbol table is a global data structure that can be used in all stages/phases/passes of a compiler. This means that it can be used/accessed from both the lex and yacc generated components.

It is conventional to access the symbol table entry from the lexical analyser when it finds a token that would be stored in the table, such as an identifier, it can find the entry and update it with information only available to the lexer like line number and character position and it can also store the lexeme value if it is not already there. The symbol table pointer can now be returned in the lval of the token.

Some people prefer to return a pointer to the lexeme itself (as the lval) from the lexer to the parser and do the initial symbol table access there. This has an advantage that the symbol table does not have to be visible to the lexer, but has the disadvantage that lexer information as described above may no longer be available to store with the symbol. It often has the disadvantage of making the parser actions from yacc a little more "busy" as they then may be involved in managing the symbol table as well as the parse tree.

The symbol table entry will be further updated in later phases of the compiler, such as a semantic walk of the parse tree which can annotate the symbol entries with type information and flag undeclared objects and the like. The symbol table will be used again during target code generation when target specific information may be stored or needed, and again during optimisation when variables usage may be examined or even optimised away.

The symbol table is a data structure that you the compiler writer create for yourself. There is no feature of lex or yacc that does it for you. It is generated as and when any code you write creates it!

The y.output file has nothing to do with symbol tables. It is a record of how yacc converted the context free grammar into a parse table. It is useful when you have an ambiguous grammar and want to know what rules are causing the shift/reduce or reduce/reduce errors when debugging your grammar.

The last part of the question, what uses do these tool have? lex is a tool that generates code for a state machine that recognises the patterns you specified. It does not have to be used in writing compilers. One interesting use is in handling networking protocols that can be processed by a state machine, such as TCP/IP datagrams and so forth. Similarly, yacc is used in matching sequences that are described by context free grammars. These do not have to be programs, but could be other complex sequences of symbols, fields or data items. They are just normally pieces of text, and that is the orthodox use of the tool.

These parts of your question really sound like the kind of exam question that someone might write for students who have attended a course in compilers!

更多推荐

本文发布于:2023-08-07 15:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464923.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:符号   yacc   lex   generation   table

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!