Difficulty parsing a text file with complex grammar #252
-
I am trying to parse some poker hands, which utilise very complex grammer it seems.
I've managed to parse the first two lines, but I'm stuck on the third. My progress is very slow, as I find it difficult to comprehend the parser's internal state and why it's failing. I'm using a custom Lexer and regex to match individual fields. My code is available here with tests that are failing. I'd appreciate some advice on how to build this lexer to make it simpler to write and debug. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
From a brief glance, one issue you're likely to have is with this type of grammar fragment: LineOne LineOne `"Poker Hand #" @@ "\n"` This is telling Participle to match the exact token {"Word", `[A-Za-z]+`, nil},
{"Whitespace", `[ \t]+`, nil},
{"Newline", `\n`, nil},
{"Punct", `[#]`, nil}, And your grammar fragment would become something like this: LineOne LineOne `"Poker" "Hand" "#" @@ "\n"` Another issue is with the use of a stateful lexer - you almost certainly don't need this, based on the example you've given. A stateful lexer is only necessary for recursive tokens such as recursive interpolated string fragments, eg. PS. The Trace option can help when debugging the parser. |
Beta Was this translation helpful? Give feedback.
-
Thank you, I will give that a go. One other problem I seem to stumble across regularly is when there is Punctuation and some value I want to extract but no word break.
Results in
|
Beta Was this translation helpful? Give feedback.
From a brief glance, one issue you're likely to have is with this type of grammar fragment:
This is telling Participle to match the exact token
Poker Hand #
, but your lexer doesn't appear to produce tokens like this. Generally the lexer should be as minimal as possible, for example in this case you might have:And your grammar fragment would become something like this:
Another issue is with the use of a stateful lexer - you almost certainly don't need this, based on the example you've given. A stat…