Skip to content

Difficulty parsing a text file with complex grammar #252

Answered by alecthomas
patrobinson asked this question in Q&A
Discussion options

You must be logged in to vote

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 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:

{"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 stat…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by patrobinson
Comment options

You must be logged in to vote
2 replies
@alecthomas
Comment options

@patrobinson
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants