문제

Just to help give some background, I am trying to program a parser that will take in a long file line-by-line.

The program will set different states according to the information it is receiving. Think of it as reaching a certain "checkpoint" in the file and setting a flag -> true based on it. There are plenty of these "checkpoints" i am trying to keep track of, but I know that using flags and if-statements for this will end up making my life difficult in the long run.

example situation:

**State 1 (requires the following variables to be set to true):**

foo -> true, bar -> true, red -> false

**State 2:**

requires state 1 to be fulfilled, as well as, blue -> true, green-> false, yellow -> true

**State 3:**

requires states 1/2 to be fulfilled, along with added variables, so on and so forth.

As you can see, these flags/states will continuously build up due to the dependency between one another.

Hopefully this all made sense. What are some tips/strategies I can use to program all this cleanly?

도움이 되었습니까?

해결책

My guess is that you don't have a context-free grammar?

If you have a context-free grammar, you can use a context-free parser generator; there are lots of tools for creating a parser for context-free grammar. The key word to search for: parser generator.

Generally, the easiest way to parse a context-sensitive grammar is to build a parser with a parser generator that can parse a context-free superset of the actual grammar, and then do a second parsing stage to do the contexts-sensitive checks.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top