开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 0

Carl/flex-bison-cpp-example

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
master
分支 (1)
标签 (4)
master
flex-bison-cpp-example-0.1.4
flex-bison-cpp-example-0.1.3
flex-bison-cpp-example-0.1.2
flex-bison-cpp-example-0.1
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
贡献代码
同步代码
对比差异 通过 Pull Request 同步
同步更新到分支
通过 Pull Request 同步
将会在向当前分支创建一个 Pull
Request,合入后将完成同步
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
未知许可证
*** Flex Bison C++ Example ***
Author: Timo Bingmann (Mail: tb a-with-circle idlebox dot net)
Date: 2007年08月20日
*** Summary ***
This example shows how to use both Flex and Bison in C++ mode. This way both
lexer and parser code and data is encapsulated into classes. Thus the lexer and
parser are fully re-entrant, because all state variables are contained in the
class objects. Furthermore multiple different lexer-parser pairs can easily be
linked into one binary, because they have different class names and/or are
located in a different namespace.
*** Website / License ***
The current example package can be downloaded from
http://idlebox.net/2007/flex-bison-cpp-example/
The following just mean you can copy the example code into your program or use
it for whatever purpose without crediting me (though I would really like it if
you did):
The parts of the example code written by myself are released into the public
domain or, at your option, under the Do What The Fuck You Want To Public
License (WTFPL), which can be found in the file COPYING. There are some special
GPL license exceptions for the included source files from the Bison and Flex
distributions.
The idea and method of this example is based on code from
http://ioctl.org/jan/bison/
*** Why Use These Old Tools? ***
Well, they are here to stay and they work well. These days there are much more
sophisticated C++ parser generation frameworks around:
 * Most well-known is the Boost.Spirit parser framework
 * and the ANTLR parser generator.
 * Less well known is the Common Text Transformation Library.
All these libraries do good jobs when you need to generate parsers for more
difficult grammars.
However if you write a program with one of the frameworks above, then your
users need that parser framework installed to compile your program. But Flex
and Bison require no compile-time dependencies, because they generate fully
autonomous source code. (And Flex and Bison are installed almost everywhere.) 
So far I have not found any modern parser generator which outputs independent
code.
It is even possible to compile the generated source with Visual C++ on Windows
(worked with 8.0 aka 2005). Flex and Bison need not be installed on the windows
machine. The source package includes a VC++ solution and two project files.
*** Source and Generated Files ***
The src directory contains the following source files. Note that some of them
are automatically generated from others.
 * scanner.ll contains the Flex source for the C++ lexical scanner.
 * scanner.cc is generated from scanner.ll by Flex.
 * scanner.h defines the lexer class example::Scanner.
 * FlexLexer.h copied from Flex distribution. Defines the abstract lexer
 class.
 * parser.yy is the example Bison parser grammar.
 * parser.cc generated from parser.yy by Bison.
 * parser.h generated from parser.yy by Bison.
 * y.tab.h contains nothing. Just forwards to parser.h
 * location.hh installed by Bison. Contains something required by the
 parser class.
 * position.hh same.
 * stack.hh same.
 * driver.h defines the example::Driver class, which puts together lexer
 and parser.
 * driver.cc implementation for driver.h
 * expression.h defines the example's calculator node classes.
 * exprtest.cc contains a main function to run the example calculator.
 * readme.dox doxygen explanation text, which you are reading right now.
So if you wish to create a program using a C++ Flex lexer and Bison parser,
you need to copy the following files:
 * scanner.ll, scanner.h, FlexLexer.h
 * parser.yy, y.tab.h
 * location.hh, position.hh, stack.hh (are created by Bison when run on
 the grammar)
 * driver.h, driver.cc
--- Namespace and Library ---
The scanner, parser and driver classes are located within the example
namespace. When coding a larger program, I believe it is most convenient to put
all scanner/parser source files into a separate directory and build a static
library. Then all parts of the parser are located in a separate namespace and
directory.
*** Code Overview ***
This is a brief overview of the code's structure. Further detailed information
is contained in the doxygen documentation, comments in the source and
ultimately in the code itself.
--- Scanner ---
The input stream is first converted by the lexical scanner into tokens. The
scanner is defined by the list of regular expressions in scanner.ll . From this
file Flex generates the file scanner.cc, which mainly contains a function
called yylex(). This function returns the next token for the parser. For a C++
scanner the yylex() function is contained in a class, which is named
yyFlexLexer by default. It is declared in the FlexLexer.h and is derived from
the abstract FlexLexer class.
By defining the macro yyFlexLexer => ExampleFlexLexer in scanner.h, the default
name of the scanner class is changed. Furthermore to extend yylex()'s parameter
list, the class example::Scanner is derived from the ExampleFlexLexer class. It
is mainly a forwarding class. By defining the macro YY_DECL, the yylex()
function generated by Flex is renamed to example::Scanner::lex().
Another change to the default Flex code is that the token type is changed from
int to the enum example::Parser::token defined by parser.
--- Parser ---
Bison's support for C++ is much more sophisticated. In C++ mode it generates a
class named example::Parser, which is located in parser.cc and declared in
parser.h . The header file also defines the scanner tokens, which must be
returned by the Flex scanner's regular expression rules. Bison's C++ skeleton
also installs the three .hh files, which contain utility classes required by
the parser.
In the example calculator the Bison code constructs a calculation node
tree. The tree's nodes are derived from CalcNode and are evaluated to output
the parsed expression's result.
--- Driver ---
The example::Driver class brings the two components scanner and parser classes
together. It is the context parameter of the parser class. The hook between
scanner object and parser object is done by defining the yylex() macro to be
"driver.lexer->lex". This way the Bison parser requests the next token from the
scanner object contained within the driver.
The example::Driver object can be accessed by the Bison actions. Therefore it
will contain a reference to the data classes filled by the parser's rules. In
the example it contains a reference to the CalcContext. Thus a refernce to a
CalcContext must be given to the constructor of example::Driver. This
CalcContext object will be filled with the parsed data.
To initiate parsing the example::Driver class contains the three functions
example::Driver::parse_stream(), example::Driver::parse_file() and
example::Driver::parse_string().
*** Example Calculator ***
The example lexer and grammar is a simple floating point arithmetic
calculator. It follows the usual operator precedence rules (sometimes called
BODMAS or PEMDAS): Parentheses, Exponentation, Multiplication/Division,
Addition/Subtraction.
Besides these simple arithmetic operators, the program also supports
variables. These can be assigned a value and used in subsequent expressions.
It can be started interactively and will process expressions entered on the
console. The expression's parse tree is printed and then evaluated. Here some
examples:
--- snip ---
$ ./exprtest
Reading expressions from stdin
input: 4 * 1.5 + 3 * (2 ^ 4 - 4)
tree:
+ add
 * multiply
 4
 1.5
 * multiply
 3
 - subtract
 ^ power
 2
 4
 4
evaluated: 42
input: v = (2 ^ 4 - 4)
Setting variable v = 12
input: 3.5 * a
input:1.6: Unknown variable "a"
input: 3.5 * v
tree:
* multiply
 3.5
 12
evaluated: 42
input: 5 + * 6
input:1.4: syntax error, unexpected '*'
input: 5 + (4 * 4
input:1.10-9: syntax error, unexpected end of file, expecting ')'
--- snap ---
The exprtest can also be used to process text files containing
expressions. Within the file each line is parsed as an expression. Multiple
expressions can be put into one line by terminating them with a semicolon
';'. The exprtest outputs a parse tree for each parsed non-assignment line.
--- snip ---
v = (2 ^ 4 - 4); e = 2.71828
4 * 1.5 + 3 * v
6 * (2 * 2) ^ 2 / 2
--- snap ---
The above example file (included as exprtest.txt) can be processed by calling
./exprtest exprtest.txt. The program outputs the following evaluation:
--- snip ---
Setting variable v = 12
Setting variable e = 2.71828
Expressions:
[0]:
tree:
+ add
 * multiply
 4
 1.5
 * multiply
 3
 12
evaluated: 42
[1]:
tree:
/ divide
 * multiply
 6
 ^ power
 * multiply
 2
 2
 2
 2
evaluated: 48
--- snap ---
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2004 Sam Hocevar 14 rue de Plaisance, 75014 Paris, France Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. You just DO WHAT THE FUCK YOU WANT TO.
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

暂无描述
暂无标签
README
未知许可证
查看未知开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/handsomei/flex-bison-cpp-example.git
git@gitee.com:handsomei/flex-bison-cpp-example.git
handsomei
flex-bison-cpp-example
flex-bison-cpp-example
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /