Skip to content

Add REx-generated parser - #2804

Open
GuntherRademacher wants to merge 12 commits into
BaseXdb:mainfrom
GuntherRademacher:rex-parser
Open

GuntherRademacher wants to merge 12 commits into
BaseXdb:mainfrom
GuntherRademacher:rex-parser

Conversation

@GuntherRademacher

Copy link
Copy Markdown
Member

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 shift strategy, 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, and RExQueryParser walks 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 option QUERYPARSER selects one: REX (the default) or MANUAL (the old one, kept as the reference).

Size: 64 files, +28418 / −1185 lines

What Lines Share
Generated or fetched code and data 17978 63%
New hand-written code in src/main ~4830 17%
New hand-written code in src/test (tools, tests, benchmarks, adapted tests) ~4480 16%
Moved from QueryParser to AQueryParser ~1090 4%
  • Generated or fetched (17978):
    • XQuerySyntaxParser.java (2094) and its .xml (5062), from REx.
    • Nodes.java (2708), NodeFactory.java (4630) and Sym.java (1444), from our generators.
    • The grammar .ebnf (1663), fetched, plus one rule.
    • fold-signatures.properties (377), recorded by a tool.
    • None of this is meant to be reviewed line by line; it is regenerated by GrammarUpdate.
    • The parser, nodes, factory and symbols are in src/main; the grammar, its XML form and the signatures are test resources.
  • New hand-written in main (~4830): RExQueryParser (3858), the syntax package and FoldPolicy (718), about 180 new lines in AQueryParser, and about 75 lines of small edits elsewhere.
  • New hand-written in test (~4480):
    • Generators and update driver: 2443.
    • Tests: 1205.
    • Benchmarks and the comparison tool: 710.
    • Adapted existing tests: 121.
  • Moved (~1090): the shared code, matched line by line against the old file. QueryParser lost 1097 lines, and AQueryParser gained 1268, of which about 1090 come from there.

The parsers compared

Code lines, without comments and blank lines:

MANUAL REX
Shared code in AQueryParser 695 695
Parser itself 3131 2629
Support classes (syntax, FoldPolicy) – 398
Hand-written total 3826 3722
Generated (parser, nodes, factory, symbols) – 9545

Before the split, QueryParser had 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.
    • GrammarUpdate is the entry point, with RExRemake, SymGenerator and NodeGenerator as steps.
    • ParseDiff compares both parsers on the QT4 suite.
    • The grammar and its XML form are test resources, next to the recorded fold signatures.

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.query pass 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.

Run MANUAL REX Difference
1 51.6 s 52.7 s +2%
2 48.9 s 51.8 s +6%

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

MANUAL REX Difference
Median of rounds 3–16 1687 ms 2160 ms +28%
Best round 1601 ms 2032 ms +27%
Per query (median) 34 µs 43 µs +9 µs

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 and else if chains; 16 rounds):

Input MANUAL median REX median
content run 1.6 ms 11.8 ms
attribute run 2.5 ms 10.6 ms
alternating content 19.4 ms 78.1 ms
arguments 5.1 ms 23.5 ms
nested arguments 1.1 ms 2.3 ms
operands 9.1 ms 57.2 ms
else if 31.1 ms 64.1 ms

On 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 MANUAL goes

Once MANUAL is 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 helper MarkdownTable (65). That is about 5300 lines in total. The QUERYPARSER option and the parser choice in the tests go with them. AQueryParser can then be merged into RExQueryParser, if we want a single class. The generated code and the tools that keep it in step with the grammar stay.

@ChristianGruen

Copy link
Copy Markdown
Member

Exciting… First observations:

  1. The parsing of function declarations seems to be quadratic (10,000 of them take 1.5 s). Maybe this can be done linearly?
  2. Could we get closer to the old messages, e.g. for retrun, let $x = 1, and possibly <a><b></a> (which is now XQST0118)?
  3. It seems that parts of the code could be cleaned up (XmlSerializer, TopDownTreeBuilder, ParseTreeBuilder).
  4. Nice to have, at most: could the first command-line parsing of a simple query be sped up?

@GuntherRademacher

Copy link
Copy Markdown
Member Author
  1. The parsing of function declarations seems to be quadratic (10,000 of them take 1.5 s). Maybe this can be done linearly?

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 declare. A single declare token is not sufficient to detect the end of the prolog, which was then assumed in a thread that immediately died when the next token came up. With a lookahead of 2, this is no longer necessary

  1. Nice to have, at most: could the first command-line parsing of a simple query be sped up?

The types of NodeFactory,createXxx methods were too specific, which required eager class loading of 245 classes. This becomes unnecessary after changing to Node, thus improving cold-start performance. There is still a slight disadvantage, though. My latest measurement for running 100 interleaved runs each of java -cp <jar> … 42 is this:

Configuration min median mean
BaseX 12.4 647 922 931
BaseX 13.0 658 880 904
rex-parser, MANUAL 639 893 906
rex-parser, REX 704 940 946

@GuntherRademacher

Copy link
Copy Markdown
Member Author
  1. Could we get closer to the old messages, e.g. for retrun, let $x = 1, and possibly <a><b></a> (which is now XQST0118)?

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:

before after
identical error code, position and message 4907 5248
different message 398 252
different position 404 209
different error code 5 5
all differing queries 830 489
  1. It seems that parts of the code could be cleaned up (XmlSerializer, TopDownTreeBuilder, ParseTreeBuilder).

These are generated classes, that are not needed here. I have filed a task for the parser generator to suppress them.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants