-
-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Precedence When Matching/Lexing #50
Comments
Right now there is no mechanism for terms precedence. |
After a brief analyze of the code I can tell that the term that is first on the terms(...) call in parser definition is actually matched first. |
I'm trying to write a parser for Java's MANIFEST.MF format (spec) and this behavior would be useful for me:
A MANIFEST.MF file looks like:
The language specification requires the value for
So I use the following
And the rule:
For other key-value pairs, I use a more general
My problem is that the If I instead list I don't have much experience with parsing, so I may be simply misunderstanding something. |
First of all, if your language doesn't do any specific whitespace processing (like indentation sensitive languages) you should not include them in the grammar at all, ctpg generated parsers skip whitespace by default. I would do it like you do, with version-number term defined first so it has precedence, and simply include the version-number term in a syntax rule for 'other key value pairs'. Can you show me the grammar as well? |
Thanks for your reply. Yes, I might be able to get by with only parsing newlines, but the grammar does not have an equivalent of semicolons, lines are terminated only with newlines.
I think I see what you're saying. In general though, this could be a lot of manual work. Correct me if I'm wrong, but you'd need to:
Sure, here is the parser I've got so far: https://gist.github.com/evan-goode/a5d97348f96328c956792c0660e414b0. And the grammar specification is here: https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#Manifest_Specification. |
Possible to use precedence in pattern matching step?
I have 3 terms on for digits ("[0-9]+"), "u" and one for identifiers "[a-zA-Z_][a-zA-Z_0-9]+"
I "u" to identify an unsigned integer and I can specify its bit arity with a number after it
e.g "100u8" is an an unsigned 8-bit integer with value 100.
100u works fine (unsigned integer with value 100 and unspecified bit-arity)
However 100 is pared as "Digits" <- "Base10Digits", (I do this because I can parse digits in different bases) then "u8" is parsed as an identifier, and there is no rule to match this.
Right now "u" has a higher precedence than "Identifier" but identifier gets matched first.
Would it be possible to:
A ) Try matching patterns with higher precedence first
B ) If there is no suitable rule for the matched term, fallback and see if there is a suitable term (case: Unexpected Identifier)?
The text was updated successfully, but these errors were encountered: