问题
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++.
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.
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:
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
I have a few questions:
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?
//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).
回答1:
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?
来源:https://stackoverflow.com/questions/5329066/integrating-flex-bison-with-external-program