Skip to content

Investigate lark as an alternative to antlr4-python3-runtime in openqasm3.parser #196

@TheGupta2012

Description

@TheGupta2012

Background

The current OpenQASM3 parser is a known performance bottleneck, especially for large or complex QASM files. Rather than a full migration, this issue proposes a focused experiment: fork the repo, implement a Lark-based parser in parser.py for a subset of the grammar, and evaluate feasibility, performance, and compatibility.

Goals

  • Evaluate if Lark can serve as a drop-in replacement for the current parser logic.
  • Maintain API and object compatibility with existing code.
  • Benchmark correctness and performance on OpenQASM3 files.

Proposed Steps

1. Fork and Prepare

2. Analyze parser.py

  • Identify key functions/classes responsible for:
    • Parsing input strings/files
    • Emitting AST or domain objects
    • Error handling and reporting

3. Develop Lark Grammar

  • Extract a minimal but representative subset of the OpenQASM3 grammar (e.g., statements, declarations, a few gate definitions).
  • Write the equivalent Lark EBNF grammar.

4. Implement Lark Parser

  • In parser.py, add a new parser class or function using Lark.
  • Use a Transformer to convert the Lark parse tree into the same AST or objects as the current parser.
  • Add a feature flag or environment variable to switch between the old and new parsers for testing.

5. Ensure API Compatibility

  • Match all public methods, return types, and error messages to the existing API.
  • Write or adapt unit tests to verify that the Lark-based parser produces identical results.

6. Benchmark and Report

  • Compare parsing speed and memory usage between the original and Lark-based parser.
  • Document any edge cases, limitations, or incompatibilities encountered.

7. Share Findings

  • Summarize results in a markdown file or GitHub issue.
  • If successful, outline next steps for incremental migration.
  • Eventually, we want this change to be merged upstream into openqasm3 if the maintainers are willing. If not, we can merge this as a parser component within pyqasm itself.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestoptimizationRelated to program optimization

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions