lex

词法分析器生成工具flex

不打扰是莪最后的温柔 提交于 2020-01-05 01:24:58
1.FLEX简介 单词的描述称为模式(Lexical Pattern),模式一般用正规表达式进行精确描述。FLEX通过读取一个有规定格式的文本文件,输出一个如下所示的C语言源程序。 +------------+ +------------+ +----------------+ | 输入文件*.l |------>|flex工具 |------>|输出文件lex.yy.c | +------------+ +------------+ +----------------+ FLEX的输入文件称为LEX源文件,它内含正规表达式和对相应模式处理的C语言代码。LEX源文件的扩展名习惯上用.l表示。FLEX通过对源文件的 扫描自动生成相应的词法分析函数 int yylex(),并将之输出到名规定为lex.yy.c的文件中。实用时,可将其改名为lexyy.c。该文件即为LEX的输出文件或输出的词法分析器。 也可将 int yylex()加入自已的工程文件中使用。 2. LEX源文件的格式 LEX对源文件的格式要求非常严格,比如若将要求顶行书写的语句变成非顶行书写就会产生致命错误。而LEX本身的查错能力很弱,所以书写时一定要注意。 LEX的源文件由三个部份组成,每个部分之间用顶行的“%%”分割,其格式如下: 定义部份 % % 规则部份  % % 用户附加C语言部份

Flex词法分析器使用

眉间皱痕 提交于 2020-01-05 01:23:24
感谢: http://blog.csdn.net/litchh/archive/2004/07/14/40983.aspx 在构造编译器方面,lex和yacc可谓是有很大的天赋,lex主要用于词法的分析,而yacc则用于语法语义等分析。 那么究竟如何使用lex和yacc呢?如果你是一个老鸟,当然这篇文章你可以跳过不看,如果你是一个新手,那么看了下面这个例子我想你会对flex做词法分析了解很多。 下面我通过一个例子来详细说明如何使用flex 根据所学的词法分析内容 ,利用 flex 构造 PL/0 语言的词法分析器。 既然是构造PL/0的词法分析器,那么我们有必要看一下pl0语言的简介和相应文法: 2 PL/0 语言 Ⅰ .PL/0 语言概述 . PL/0 语言是 PASCAL 语言的子集,它具备一般高级程序设计语言的典型特点。 PL/0 语言编译程序结构比较清晰,可读性强,充分体现了一个高级语言编译程序实现的基本组织、技术和步骤,是一个非常合适的小型编译程序的教学模型。 Ⅱ .PL/0 语言文法描述 . 由于只是做 PL/0 的词法分析,因此这里只列出词法有关的内容,其余略过。 下面用扩充的 EBNF 来进行表示: 元符号说明: ‘ < > ’:用左右尖括号括起来的中文字表示语法构造成分,或称语法单位,是 PL/0 的非终结符 ‘ ::= ’:该符号的左部由右部定义。 ‘ | ’

How to determine line/column numbers from boost::spirit::lex tokens?

浪尽此生 提交于 2020-01-04 05:30:31
问题 In its answer to this question, hkaiser said he would write an example of a token type which carries code position information. I really cannot find anything about this. Can anyone point me to such an example ? 回答1: He was probably referring to position_token , which did in fact get released, although there's little documentation. This SO question has good coverage of its usage. 来源: https://stackoverflow.com/questions/32073454/how-to-determine-line-column-numbers-from-boostspiritlex-tokens

Objective-C ParseKit return value

半世苍凉 提交于 2020-01-03 03:09:06
问题 In flex/lex/bison/yacc (all of which I just started reading about), you can set "$$" to be equal to some value ($1,$2,$3) and that's the value that gets returned. At least I think that's how it works. In ParseKit, you are given a stack so I imagine that the ($1,$2,$3) would be the first three values on the stack for example. But then I think what you would want to do is pop those values off the stack and put your return value on the stack. I see that the stack comes with a push method. Do you

Objective-C ParseKit return value

▼魔方 西西 提交于 2020-01-03 03:09:03
问题 In flex/lex/bison/yacc (all of which I just started reading about), you can set "$$" to be equal to some value ($1,$2,$3) and that's the value that gets returned. At least I think that's how it works. In ParseKit, you are given a stack so I imagine that the ($1,$2,$3) would be the first three values on the stack for example. But then I think what you would want to do is pop those values off the stack and put your return value on the stack. I see that the stack comes with a push method. Do you

What are the standard grammar parsers for iOS?

江枫思渺然 提交于 2020-01-02 20:17:21
问题 Does iOS for iPad and iPhone have support for a parser? In particular, is lex/yacc or flex/bison available for iOS development? Does xcode4 have a natively supported library? (I could generate my grammar via lex/yacc and then take the *.c files and put them in my project, but I was hoping for more tightly supported libraries.) Any ideas? 回答1: lex, yacc, flex, and bison all come with the Mac/iOS developer tools. Xcode has built-in rules for processing .l files with flex and .y files with bison

Flex, continuous scanning stream (from socket). Did I miss something using yywrap()?

不羁的心 提交于 2020-01-01 03:49:25
问题 Working on a socketbased scanner (continuous stream) using Flex for pattern recognition. Flex doesn't find a match that overlaps 'array bounderies'. So I implemented yywrap() to setup new array content as soon yylex() detects <> (it will call yywrap). No success so far. Basically (for pin-pointing my problem) this is my code: %{ #include <stdio.h> #include <string.h> #include <stdlib.h> #define BUFFERSIZE 26 /* 0123456789012345678901234 */ char cbuf1[BUFFERSIZE] = "Hello everybody, lex is su"

YAML parsing - lex or hand-rolled?

本秂侑毒 提交于 2020-01-01 03:16:10
问题 I am trying to write a simple YAML parser, I read the spec from yaml.org, before I start, I was wondering if it is better to write a hand-rolled parser, or use lex ( flex/bison ). I looked at the libyaml (C library) - doesn't seem to use lex/yacc . YAML (excluding the flow styles ), seems to be more line-oriented, so, is it easier to write a hand-rolled parser, or use flex/bison Thanks. 回答1: This answer is basically an answer to the question: "Should I roll my own parser or use parser

Calling flex from a makefile

爱⌒轻易说出口 提交于 2019-12-31 07:24:08
问题 I would like to call flex to build a .l file, then call gcc to build everything. I tryed: comp: lex scanner.l \ gcc -o a.out main.c hash.c -I. error: lex scanner.l \ gcc -o a.out main.c hash.c -I. lex: impossible to opne gcc /usr/bin/m4:stdin:2994: ERROR: end of file in string and lex scanner.l <tab> gcc -o a.out main.c hash.c -I. error: missing separator. The lex.yy.c generate by lex has is being included in the main file. Thanks in advance, Pedro 回答1: all: a.out lex.yy.c: scanner.l lex

Have problem with lex

佐手、 提交于 2019-12-31 04:15:11
问题 My lex as follows: LNUM [0-9]+ DNUM([0-9]*"."[0-9]+)|([0-9]+"."[0-9]*) %% {LNUM} { printf("\t");ECHO;printf("\r\n"); } {DNUM} { printf("\t");ECHO;printf("\r\n"); } But it turns out that it can only match numbers like 4.12 .2 ,not 42 , 45. etc.(those indented are matched) Output: 1. 1. .1 .1 12 12 My target is to match both integers and float numbers. Can anyone here tell me what's wrong above? 回答1: Late answer to your question... but for what it's worth, I tried replacing the * you had in the