将Flex / Bison与外部程序集成 [英] Integrating Flex/Bison with external program

查看:190
本文介绍了将Flex / Bison与外部程序集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个智能代理模型,它需要一个事件列表作为输入。事件来自另一个模型的输出,并且在(大)文本文件中。文本文件是所有事件的列表(包括我不在乎的不必要的事件),所以我使用flex编写了一个可以找到有用位的扫描器。智能代理模型的框架已经用C ++编写。

I'm working on an intelligent agent model that requires, as input, a list of events. The events come from the output of another model and are in a (large) text file. The text file is a list of all events (including unnecessary events that I don't care about), so I've written a scanner using flex that can find the useful bits. The framework for the intelligent agent model is already written in C++.

每个事件都有时间戳,并且包含大量关于事件的信息。输入文件的格式是常量,所以我真的没有必要检查语法。我不知道Bison是否会给我带来任何好处,因为语法其实很简单。没有真正的变化。

Each event is timestamped and contains a large amount of information about the event. The format of the input file is constant, so I really have no need to check the syntax. I don't know if Bison will actually offer me any advantages, because the grammar is actually pretty simple. There's no real variation.

我只需要一种方法将每个事件(和来自该事件的所有信息)放到一个堆栈上。智能代理按时间顺序对每个事件进行操作,因此我需要程序扫描整个输入文件,然后将每个事件以相反的顺序放入堆栈(输入文件中的第一个事件应该是最后一个事件推送到堆栈)。这将允许智能代理从堆栈弹出事件,并一次处理一个。

I just need a way to place each event (and all the information from that event) onto a stack. The intelligent agent acts on each event chronologically, so I need the program to scan the entire input file, then place each event onto a stack in reverse order (the first event in the input file should be the last event pushed onto the stack). That will allow the intelligent agent to pop the events off the stack and deal with them one at a time.

我的想法是,bison不会帮助我太多,因为语法只是顺序列出所有令牌的问题。它基本上是这样的:

My thought is that bison won't help me much, because the grammar is simply a matter of listing all of the tokens sequentially. It'd basically look like this:

eventlist: /* nothing */
| eventlist event EOL
;

event: token1 token2 token3 ... tokenN-1 tokenN

一个小片段的输入文件,所以你可以看到我的意思:

Here's a small snippet of the input file, so you can see what I mean:


场景事件时间:DAY 1 00:00:00 < br>
[DATA FUSION EVENT]已创建新曲目

摘要

实际目标:RF HQ

Fusion Center位置:CVN Enterprise_0

融合中心名称:DEFAULT FUSION

感知目标
感知识别:未知

感知分类:未知

操作中:土地

Scenario Event Time: DAY 1 00:00:00
[DATA FUSION EVENT] New Track Formed
Summary
Actual Target: RF HQ
Fusion Center Location: CVN Enterprise_0
Fusion Center Name: DEFAULT FUSION
Perceived Target Perceived Identification: Unknown
Perceived Classification: Unknown
Operating Medium: Land

我有几个问题:

扫描仪生成的flex与已经存在的更大的程序?

2)将bison提供任何优势,或者我最好只是写我自己的程序,把所有的令牌放在一个数据结构,并把它们放在堆栈?
2a)如果bison更好,那么解决问题1,但是如何从我的程序中调用bison,然后让bison返回一个指向堆栈的指针,以便程序可以使用它?

1) How do I integrate the scanner generated by flex with the larger program that already exists?
2) Will bison offer any advantages or am I best off simply writing my own program to put all the tokens on a data structure and put them on the stack? 2a) If bison is better, then that solves question 1, but then how do I call bison from within my program and then have bison return a pointer to the stack so the program can use it?

//编辑:我想出了如何从外部C ++程序调用flex。我已经不能,到目前为止,创建一个bison程序,将做我想要的,虽然(即,返回一个指针到一堆事件)。

//Edited: I've figured out how to call flex from an external C++ program. I have been unable, so far, to create a bison program that will do what I want, though (namely, return a pointer to a stack of events).

推荐答案

如果你的语法是那么简单,那么使用解析器似乎过分。

If your grammar is that simple then using a parser seems overkill. Just process each line and scan the tokens into a collection.

为什么你认为你需要一个解析器?

Why do you think you need a parser?

这篇关于将Flex / Bison与外部程序集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆