I had a working parser and it essentially revolved around carefully building a Regex (or more precisely a wrapper around regex which keeps track of group names) out of the Uri grammar in small steps and then just matching that.
But this means that I hardcoded the grammar as a Regex, so I took a step back and implemented classes inheriting from an interface
Expression, meant to model regular expressions as an algebraic data type, as done in the lecture and a visitor to build the Regex for me if given an expression.
So now instead of hardcoding the grammar as a Regex I have it hardcoded as an
Expression. The next logical step would be to invent a file format that stores a grammar like it is done in the doc string of
Uri which each rule being a definition of a regular expression and then to build a lexer that produces such an expression programmatically from the lines of the file.
If I did that I would have build a lexer/parser for regular expressions which reads a regular grammar and produces a regex out of it which I then give to Java’s Regex engine to match the
I believe that this is not the intended way to solve the problem and besides I would have still hardcoded something, namely the grammar of regular expressions. So:
- What was I actually meant to do? (Something like making the rules of the URI grammar the expressions? Then I hardcode the grammar again and I also have to write a lexer to create expressions out of the input string)
- Is there anyway to remove hardcoded grammars out of a lexer/parser/compiler/whatever?
Bonus question: My regex is build statically in a class consisting only of constants with a private constructor. I do this because:
- There is no main method, so I have to get the regex either statically or I would have to build it every time I call the
UriParserFactory(= waste of time and not the place it should happen anyway).
- This is essentially the place where a lexer would go, but that is missing. So this class is a band aid.
Are there secret tests which deduct points for such bad style?