Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Linux Bison semantic types

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "how to use Linux Bison semantic types". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Linux Bison semantic types.

Brief introduction

As UNIX ®developers know, Flex and Linux Bison are very powerful and ideal for developing lexical and grammatical parsers, especially language compilers and interpreters.

This article introduces more advanced topics: features and techniques used to better implement error handling capabilities in compilers and interpreters. To demonstrate these techniques, I used a sample program, ccalc, which implements an enhanced calculator based on the computer in the Bison manual. We can download ccalc and related files from the download section later in this article.

Enhancements include the use of a number of variables. In ccalc, variables are defined when they are used in initialization, such as axi3. If the variable is used before initialization, a semantic error is generated, create the variable with a value of 0, and print a message.

Sample source file

The sample source code includes seven files:

Ccalc.c: main program, and some functions for input, output, and error handling ccalc.h: including the definition of all modules cmath.c: input grammar lex.l:Flex used by the mathematical function parse.y:Bison input makefile: simple makefiledefs.txt: sample input file this program receives two parameters:

-debug: generate debug output filename: input file name; default value is the setting used by defs.txtBison in order to handle variable names and actual values, the semantic type of Bison must be enhanced:

Listing 1. Better Linux Bison semantic types

/ * generateinclude-filewithsymbolsandtypes*/% defines / * amoreadvancedsemantictype*/% union {doublevalue; char*string;}

Some grammar rules can produce specific semantic types, which require the declaration of Bison as shown in listing 2. To get a more portable version of Bison grammar, we need to redefine the +-* / () symbol. The following example does not use the left parenthesis (, but uses the Terminator symbol LBRACE, which is provided by lexical analysis. In addition, the priority of operators must also be declared.

For Flex, the generated code usually depends on the code page (codepage) used by the platform. Although we can use other code pages, we have to convert the input. Therefore, unlike Bison code, Flex code is not yet portable.

At this point, I believe you have a deeper understanding of "how to use Linux Bison semantic types". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report