bison & flex

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

来自: http://hi.baidu.com/bihailan/blog/item/f12e78c82b2ae21c7e3e6ff8.html

bison 是替代yacc的语法分析程序生成器. yacc是 Yet Another Compiler Compiler的缩写. bison又是什么呐 是一个生成可以分析文本文件结构的程序的程序. 用户不用直接编写程序而只用确定好如何分析这些文本文件的规则就可以了. 这种文本结构应用的例子举不胜举, 其中一个就是计算器(calculator).

Introduction to Bison

Bison is a general-purpose parser generator that converts an annotated context-free grammar into an LALR(1) or GLR parser for that grammar. Once you are proficient with Bison, you can use it to develop a wide range of language parsers, from those used in simple desk calculators to complex programming languages.

Bison is upward compatible with Yacc: all properly-written Yacc grammars ought to work with Bison with no change. Anyone familiar with Yacc should be able to use Bison with little trouble. You need to be fluent in C or C++ programming in order to use Bison.

Bison

安装指导参见: the 节 called 安装 Bison-1.875 在 第 6 章.

Bison的内容

Bison 是替代yacc的语法解析器. Bison能生成可以分析文本文件结构的程序.

安装下列程序: bison 和 yacc

安装下列库文件: liby.a

简短说明

bison 是替代yacc的语法分析程序生成器. yacc是 Yet Another Compiler Compiler(又一个编译器的编译器)的缩写.

yacc是bison的包装脚本,实际上是以-y的参数调用bison. 这个是为了和那些用yacc而不是bison的程序兼容.

liby.a 是 Yacc 库,包含了与Yacc兼容的 yyerror 和主要函数。通常这个库没什么用,但 POSIX 要求有它.

Bison 安装依赖关系

Bison 依赖于: Bash, Binutils, Coreutils, Diffutils, GCC, Gettext, Glibc, Grep, M4, Make, Sed.

Flex description

Flex is a Fast Lexical Analyzer.

Flex is a fast lexical analyzer generator. It is a tool for generating programs that perform pattern-matching on text. Flex is a non-GNU free implementation of the well known Lex program.

Flex is a tool for generating scanners: programs which recognized lexical patterns in text. flex reads the given input files, or its standard input if no file names are given, for a description of a scanner to generate. The description is in the form of pairs of regular expressions and C code, called rules. flex generates as output a C source file, `lex.yy.c', which defines a routine `yylex()'. This file is compiled and linked with the `-lfl' library to produce an executable. When the executable is run, it analyzes its input for occurrences of the regular expressions. Whenever it finds one, it executes the corresponding C code.

Some simple examples

First some simple examples to get the flavor of how one uses flex. The following flex input specifies a scanner which whenever it encounters the string "username" will replace it with the user's login name: 

%%
username printf( "%s", getlogin() );

By default, any text not matched by a flex scanner is copied to the output, so the net effect of this scanner is to copy its input file to its output with each occurrence of "username" expanded. In this input, there is just one rule. "username" is the pattern and the "printf" is the action. The "%%" marks the beginning of the rules. 

Here's another simple example: 

int num_lines = 0, num_chars = 0;

%%
n ++num_lines; ++num_chars;
. ++num_chars;

%%
main()
{
yylex();
printf( "# of lines = %d, # of chars = %dn",
num_lines, num_chars );
}

This scanner counts the number of characters and the number of lines in its input (it produces no output other than the final report on the counts). The first line declares two globals, "num_lines" and "num_chars", which are accessible both inside `yylex()' and in the `main()' routine declared after the second "%%". There are two rules, one which matches a newline ("n") and increments both the line count and the character count, and one which matches any character other than a newline (indicated by the "." regular expression). 

A somewhat more complicated example: 

/* scanner for a toy Pascal-like language */

%{
/* need this for the call to atof() below */
#include < math.h >
%}

DIGIT [0-9]
ID [a-z][a-z0-9]*

%%

{DIGIT}+ {
printf( "An integer: %s (%d)n", yytext,
atoi( yytext ) );
}

{DIGIT}+"."{DIGIT}* {
printf( "A float: %s (%g)n", yytext,
atof( yytext ) );
}

if|then|begin|end|procedure|function {
printf( "A keyword: %sn", yytext );
}

{ID} printf( "An identifier: %sn", yytext );

"+"|"-"|"*"|"/" printf( "An operator: %sn", yytext );

"{"[^}n]*"}" /* eat up one-line comments */

[ tn]+ /* eat up whitespace */

. printf( "Unrecognized character: %sn", yytext );

%%

main( argc, argv )
int argc;
char **argv;
{
++argv, --argc; /* skip over program name */
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;

yylex();
}

This is the beginnings of a simple scanner for a language like Pascal. It identifies different types of tokens and reports on what it has seen. 

The details of this example will be explained in the following sections.

Flex & Bison 开始 FlexBison 是为编译器和解释器的编程人员特别设计的工具:例如,如下代码片段: 词法分析把这段代码分解为这样一些记号:, , , , , 。接着语法分析确定了 是一个表达式,而这个表达式被赋给了 。不过后来它们在其他应用领域被证明也非常有效。任何应用程序,尤其文本处理,只要在其输入中寻找特定的模式,或者它使用命令语言作为输入,都适合使用 FlexBison。例如,SQL 分析:在编译器结构中,词法分析器、语法分析器是编译器前端的主要组成部分。大多数编译器组织成三个主要的阶段:前端、优化器 阅读详情

相关推荐

Flex & Bison

文件结构 FlexBison的源代码文件都是由定义,规则,用户自定义函数三部分组成的,结构如下 { definitions } %% { rules } %% { user subroutes } 联合使用FlexBison FlexBison各有一份自己的源代码,比如lexical.l和syntax.y,分别定义词法规则和语法规则,这两份源代码经过各自的处理后,分别生成一份词法分析器和语...

Wir müssen wissen, wir werden wissen. 1704

编译出错 yacc:未找到命令

通过搜索,知道可以通过安装Bison-GNU解析器来纠正此错误。发电机,但是即使安装了Bison之后,仍然遇到相同的错误。在终端上运行以下命令以安装bisonyacc可执行文件和配置。yaccbison一起提供。重新运行编译就解决问题了。

道亦无名 1653

利用FLEX & BISON 快速实现简单的C 语言编译器前端

Flex Flex是一个生成词法分析器的工具,它可以利用正则表达式来生成匹配相应字符串的C语言代码,其语法格式基本同Lex相同。 格式 LEX的源文件由三个部份组成,每个部分之间用顶行的 `%%’ 分割,其格式如下: 定义部份 %% 规则部份 %% 用户附加C语言部份 手册-&amp;amp;amp;amp;amp;gt;Flex, version 2.5 怎么用就不赘述了…. 主要就是用这个词法生...

unicxitoiv的博客 2万+

YaccBison

YaccBison

gfdhjf的博客 1248

flex

linux下的flex全称叫做 快速词汇分析程序生成器 1 linux ubuntu 12.04 安装flex sudo apt-get install flex 2 Terminal解释 wayne@ubuntu:~/Downloads/lxr-1.2.0$ flex --help Usage: flex [OPTIONS] [FILE]... Generates pr

xiaogou56a的专栏 719

YACCBISON)使用指南

YACCBISON)使用指南

火雨(Nick) 3万+

flex & bison

flex yytext是匹配的字符串,yyleng是该字符串的长度 scan的规则: If action code returns, scanning resumes on the next call to yylex(); if it doesn’t return, scanning resumes immediately. 统计代码行数,字符数,数字字符数 %{ #include &lt;...

tailuzhecom的博客 456

flex & bison 基础概述

编译器,编译原理,flex & bison

JiMoKuangXiangQu的专栏 5621

初识 flex & bison

flexbison经常结合使用,分别用于词法分析和语法分析。flex):flex用于生成词法分析器或者说是扫描器(scanner)。它将输入的文本分解为称为"tokens"的序列。每个 token 都有一个特定的意义,例如一个数字、一个变量名或一个操作符。bison):bison用于生成语法分析器或称为解析器(parser)。它的任务是根据由flex产生的 tokens 来确保输入遵循某种给定的语法,并从中生成一个通常的抽象语法树 (AST) 或其他中间表示。

青衫客36的博客 2500

实习日志(1)初识flex & bison (计算器的设计)

我怎么也没想到来公司了竟然是做编译原理相关的工作。  毕竟没有学过编译原理 ,看到正则表达式就晕了。T_T 刚刚拿到flex & bison 这本书的时候 感觉一头雾水,完全知道干什么的。第一天看了词法分析工具flex。直接用flex写出统计单词,行数,字符数的程序,比起直接用C++去模拟,简单的多了。顿时就感觉这个东西好叼。然后就看到了bison 这个这个东西, 后来才懂flex & biso

非确定有限状态自动机 2215

编译原理 · Flex & Bison 入门到实战教学

本文是一篇关于编译原理中FlexBison工具的实战教学文章,主要面向已学习过编译原理理论、希望动手实践的读者。文章分为五个部分: 整体概述:介绍编译器工作流程中词法分析(Flex实现)和语法分析(Bison实现)的作用及工具关系。 Flex词法分析器详解: 讲解Flex文件的三大结构(定义段、规则段、用户代码段) 核心正则表达式语法和内置变量 提供简单Token识别、关键字与标识符区分等实例 Bison语法分析器详解: 说明Bison文件结构 解释终结符/非终结符/产生式 包含四则运算和变量赋值等进阶实

2401_88644562的博客 510

flex & bison mark

this link: http://aquamentus.com/flex_bison.html and this link: https://blog.csdn.net/GW569453350game/article/details/47807123 Step1. 在bison中定义需要使用的token, 并且根据上下文无关文法BNF定义规则 Step2. 在 flex 中定义 token 的规则, yytext -&gt; yylval (union) Step3. 在 bison gramma 解析的

all for one,one for all 303

FLEX & BISON 联合使用

flex是词法分析器,bison是语法分析器,基本原理就是flex解析出token后,让bison来使用。 实际上,一般是先编写bison脚本,里面的token就是一个定义,没有实现,里面的yylex也是没有实现,只有定义,为什么先做bison呢?因为编写bison脚本并被bison编译后,除了产生一个parse的c文件,还会产生一个token头文件,里面详细定义了token,然后,我们再编写f...

HeroIsUseless的博客 1529

Flex & GNU Bison 简单四则混合运算

Flex 和 GNU Bison 实现的非常简单的计算器,支持 int 和 double 类型的 +-*/% 运算以及括号 ()main.l%{#include #include "main.tab.h"void yyerror(const char *);%}integer [0-9]+fraction "."{integer}expone

ShingRay的专栏 1299

从零构建四则运算编译器——基于 Flex & Bison

本文介绍如何利用FlexBison工具构建一个支持优先级的四则运算解析器。系统采用分层架构:Flex负责词法分析,通过正则表达式识别数字和运算符并生成Token;Bison负责语法分析,通过三层上下文无关文法(表达式、项、因子)实现运算优先级。

2301_80719975的博客 437

词法分析flex & 语法分析bison

flex&bison

errorr0 3882

flex & bison学习(一)

编译器是软件开发中的核心部件,其作用是其他任何软件所不能取代的。编译器在工作过程中,往往完成如下的任务: 读取源代码并且获得程序的结构描述 分析程序结构,并且生成相应的目标代码 在UNIX早期时代,编写一个编译器是一件非常耗时的工作。人们为了简化开发过程,开发了Lex和YACC程序来解决第一个任务,根据用户描述的语言,生成能够解决问题的C/C++语言代码,供开发者使用。 将源代...

weixin_34187822的博客 137
上一篇: u-boot顶层Makefile分析(转)
下一篇: u-boot的Makefile分析
我不会Debug
博客等级 码龄16年 47粉丝 3原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值