Repository navigation
Add REx-generated parser - #2804
GuntherRademacher wants to merge 12 commits into
Conversation
|
Exciting… First observations:
|
This could be achieved by extending the lookahead from 1 to 2 - the parser is now generated as GLALR(2). The reason was runtime GLR processing forking a separate thread with each
The types of
|
Done. On a syntax error, the candidate tokens are now tried by re-parsing the input up to the error, which restores more of the old messages. QT4 comparison, before and after:
These are generated classes, that are not needed here. I have filed a task for the parser generator to suppress them. |
BaseX gets a second XQuery parser next to the hand-written
QueryParser. It is driven by a parser generated by REx from the XQuery 4.0 grammar.The generated parser is LALR with GLR fallback, and a few remaining ambiguities are resolved by the
shiftstrategy, which handles them in the same way as the original parser did. The parser produces events, and these are turned into a syntax tree of Java records, andRExQueryParserwalks that tree to build the same expression trees as before.Both parsers share the parts that do not depend on the syntax, now in
AQueryParser. The optionQUERYPARSERselects one:REX(the default) orMANUAL(the old one, kept as the reference).Size: 64 files, +28418 / −1185 lines
src/mainsrc/test(tools, tests, benchmarks, adapted tests)QueryParsertoAQueryParserXQuerySyntaxParser.java(2094) and its.xml(5062), from REx.Nodes.java(2708),NodeFactory.java(4630) andSym.java(1444), from our generators..ebnf(1663), fetched, plus one rule.fold-signatures.properties(377), recorded by a tool.GrammarUpdate.src/main; the grammar, its XML form and the signatures are test resources.RExQueryParser(3858), thesyntaxpackage andFoldPolicy(718), about 180 new lines inAQueryParser, and about 75 lines of small edits elsewhere.QueryParserlost 1097 lines, andAQueryParsergained 1268, of which about 1090 come from there.The parsers compared
Code lines, without comments and blank lines:
AQueryParsersyntax,FoldPolicy)Before the split,
QueryParserhad 3748 code lines. So the hand-written part is about the same size, and the generated part comes on top. The generated part needs no maintenance, only regeneration.New packages
org.basex.query.parse.gen(main): the generated classes: the parser (XQuerySyntaxParser), the node records (Nodes), their factory (NodeFactory) and the symbol constants (Sym).org.basex.query.parse.syntax(main): turns the parser's events into a syntax tree (TreeBuilder,SyntaxTree,Node,Terminal,Marker,ListAcc,SyntaxParser).org.basex.query.parse(main):FoldPolicy, which decides which grammar productions are collapsed to their only child.org.basex.query.parse(test): the tools and tests that keep this in step with the grammar.GrammarUpdateis the entry point, withRExRemake,SymGeneratorandNodeGeneratoras steps.ParseDiffcompares both parsers on the QT4 suite.Where it stands
On the QT4 suite (50153 queries), both parsers give the same result for 49336 of them: 44447 parse successfully with the same plan, and 4889 fail with the same error. A further 812 differ only in syntax error wording or position, which we tolerate. 5 differ in grammar-level cases that are still under review. Query tests under
org.basex.querypass with both parsers, apart from the XSLT failures that depend on the environment.Performance
Three measurements, all MANUAL against REX:
QT4 test suite, complete runs (
QT4Test, parsing, compiling and running all queries): two runs each.Both parsers give the same result. In a full run, parsing is a small part of the time, so REX costs 2 to 6% overall.
Parsing only (
ParseBench: the 50153 queries of the suite, parsed and compiled but not run, 16 rounds in one JVM, the first two rounds as warm-up):So, in steady state, REX needs about 27% more time for the same parsing work. This is a difference of less than 10 µs per query.
Stress inputs (
StressBench: synthetic queries with long runs of content, attributes, arguments, operands andelse ifchains; 16 rounds):else ifOn such inputs REX is 2 to 7 times slower, but none of them takes more than 80 ms. Neither parser fails on deep nesting.
When
MANUALgoesOnce
MANUALis dropped, these files can be deleted in full:QueryParser(4508 lines), and the tools that compare the two parsers:ParseDiff(401),ParseBench(204),StressBench(99) and their helperMarkdownTable(65). That is about 5300 lines in total. TheQUERYPARSERoption and the parser choice in the tests go with them.AQueryParsercan then be merged intoRExQueryParser, if we want a single class. The generated code and the tools that keep it in step with the grammar stay.