当前位置:网站首页>flex&bison系列第一章:flex Hello World
flex&bison系列第一章:flex Hello World
2022-08-10 18:07:00 【飞翼剑仆】
系列文章目录
flex&bison系列第一章:Flex Hello World
前言
在此记录下基于flex写一个Hello World的过程,以备查阅。
flex是用来生成程序的工具,比如用来生成编译器程序(一般指编译器的前端部分)。它所生成的程序能够处理结构化的输入。在这里,为简单起见,我们可以把“结构化的输入”理解为有一定组织结构的文本,我们用flex对文本进行处理时,应提前了解一下正则表达式和编译原理相关的知识(如词法分析器、自动机等)。
本章,我们就来利用flex写一个简单的Hello World程序。
一、开发环境
1. 操作系统
我用的操作系统是macOS 11.6.4:
System Version: macOS 12.5 (21G72)
Kernel Version: Darwin 21.6.0
Model Name: MacBook Pro
Model Identifier: MacBookPro15,1
Processor Name: 8-Core Intel Core i9
Processor Speed: 2.3 GHz
Number of Processors: 1
Total Number of Cores: 8
Memory: 16 GB
2. 安装工具
用到的工具有Clang (Xcode)、flex。
关于安装Xcode, 可参考这个简易教程:
How to download Xcode DMG or XIP file? - Stack Overflow
安装后确认信息如下(示例):
% clang -v
Apple clang version 14.0.0 (clang-1400.0.29.100)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
对于其它工具,可借助brew (https://brew.sh) 进行安装(示例):
# Install flex
brew install flex
# Upgrade flex
brew upgrade flex
macOS已经自带flex工具,所以无需自己安装了。
二、Hello World
这个Hello World程序要做的事很简单,具体如下:
- 当检查到“World”这个单词时,输出“Hello World”
- 否则,输出原来的单词即可
首先,我们要写一个简单的flex脚本文件(示例):
/* * hello-world.l */
%option noyywrap
%{
%}
%%
World printf("Hello World");
%%
int main(int argc, char** argv)
{
yylex();
}
一个flex文件一般分为三部分,用符合“%%”隔开:
<第一部分>
%%
<第二部分>
%%
<第三部分>
我们的例子中,比较重要的是第二部分:
World printf("Hello World");
这一行代码包含两个部分:
World
,它其实是一个匹配字符串,意思是用“World”去匹配输入的数据。printf("Hello World");
,这是匹配成功后的“行动”,意思是如果匹配成功,则打印“Hello World”。
三、编译
1. 生成C代码
运行如下命令即可生成C代码(示例):
flex hello-world.l
其生成的C代码文件名为“lex.yy.c”。
2. 编译
我们可以用clang对以上生成的C代码进行编译(示例):
# Set up C++ standard library and header path for clang
export SDKROOT=$(xcrun --sdk macosx --show-sdk-path)
# Build with clang
clang lex.yy.c -o hello-world
# Or, build with gcc
gcc lex.yy.c -o hello-world
以上命令会生成一个名为“hello-world”的可执行程序。
四、运行
可以用如下命令运行Hello World程序(示例):
./hello-world
接下来,程序会等待用户的输入。我们可以随意打一个字符,然后按回车键,可以看到如下输出(示例):
abc
abc
World
Hello World
123
123
注意到,当我们输入单词“World”时,程序会输出“Hello World”;而当输入其它单词时,则仅输出原来的单词而已。
总结
我们基于flex,用C++写了一个Hello World,并且编译运行成功。完整源码示例请参看:
https://github.com/wuzhanglin/flex-bison-examples
边栏推荐
- Intelligent bid strategy how to affect advertising effectiveness?
- 开发模式对测试的影响
- 兼具外观、性能、屏幕!华硕灵耀X 14火热抢购中
- 测试接口出现“data“: “Full authentication is required to access this resource“凭证已过期
- WebRTC源码分析 nack详解
- FFmpeg Huaping solution (modify source code, discard incomplete frames)
- php7中使用“??”运算符
- WebRTC source code analysis nack detailed explanation
- Allegro软件Shape菜单下的每个命令的含义
- Making Pre-trained Language Models Better Few-Shot Learners
猜你喜欢
运维如何学习、自我提升价值?
WebRTC source code analysis nack detailed explanation
Interface test advanced interface script using -apipost (pre/post execution script)
const的自己理解
H3C_堆叠(IRF)及链路聚合在项目中的综合应用
FFmpeg extract H264 nalu from the mp4
开发模式对测试的影响
LeetCode 198:打家劫舍
[Image dehazing] Image dehazing based on color attenuation prior with matlab code
Toronto Research Chemicals BTK甜味剂配方丨D-Abequose
随机推荐
FFmpeg 从mp4上提取H264的nalu
Making Pre-trained Language Models Better Few-Shot Learners
AVFrame相关api内存管理
企业如何通过北森HR SaaS 自动化管理员工账号生命周期
Keil5退出仿真调试卡死的解决办法
D-Wave成功上市!量子计算商业化正在加速
How to choose Fengjiawei PHY62xx series?PHY6222/PHY6212/PHY6252
欧洲核子研究中心首次在量子机器学习研究中取得实效
MySQL数据高级查询之连接查询、联合查询、子查询[通俗易懂]
FFmpeg Huaping solution (modify source code, discard incomplete frames)
【HMS core】【FAQ】AR Engine、Analytics Kit、Video Editor Kit、Image Kit、Map Kit典型问题合集2
【接入指南 之 直接接入】手把手教你快速上手接入HONOR Connect平台(下)
eager模式和graph模式 Tensorflow
Xilinx FPGA收发器参考时钟设计应用
机器人控制器编程实践指导书旧版-实践六 LCD液晶显示(点阵)
【HMS core】【FAQ】Analytics Kit、Push Kit典型问题合集3
Flexsim 发生器和暂存区设定临时实体流颜色和端口
Flexsim 发生器设置label和颜色
Wuling Hongguang MINI EV, the only drawback is safety
Live Review|How to build an enterprise-level cloud management platform in the multi-cloud era?(with the download of the construction guide)