Skip to content
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

Parser fails in the presence of brace in comment #19

Open
braised-babbage opened this issue May 18, 2023 · 1 comment
Open

Parser fails in the presence of brace in comment #19

braised-babbage opened this issue May 18, 2023 · 1 comment

Comments

@braised-babbage
Copy link
Collaborator

The following OpenPulse program fails to parse

cal {
  // while(true) {
  int x;
}

The error I see is

In [10]: parse("""cal {
    ...: // while(true) {
    ...: int x;
    ...: }
    ...: """)
---------------------------------------------------------------------------
ParseCancellationException                Traceback (most recent call last)
File ~/source/interpres/.venv/lib/python3.10/site-packages/openqasm3/parser.py:87, in parse(input_, permissive)
     86 try:
---> 87     tree = parser.program()
     88 except (RecognitionException, ParseCancellationException) as exc:

File ~/source/interpres/.venv/lib/python3.10/site-packages/openqasm3/_antlr/_4_11/qasm3Parser.py:640, in qasm3Parser.program(self)
    639 self.state = 131
--> 640 self.statement()
    641 self.state = 136

File ~/source/interpres/.venv/lib/python3.10/site-packages/openqasm3/_antlr/_4_11/qasm3Parser.py:919, in qasm3Parser.statement(self)
    918 self.state = 155
--> 919 self.calStatement()
    920 pass

File ~/source/interpres/.venv/lib/python3.10/site-packages/openqasm3/_antlr/_4_11/qasm3Parser.py:3352, in qasm3Parser.calStatement(self)
   3351     self.state = 423
-> 3352     self.match(qasm3Parser.RBRACE)
   3353 except RecognitionException as re:

File ~/source/interpres/.venv/lib/python3.10/site-packages/antlr4/Parser.py:126, in Parser.match(self, ttype)
    125 else:
--> 126     t = self._errHandler.recoverInline(self)
    127     if self.buildParseTrees and t.tokenIndex==-1:
    128         # we must have conjured up a new token during single token insertion
    129         # if it's not the current symbol

File ~/source/interpres/.venv/lib/python3.10/site-packages/antlr4/error/ErrorStrategy.py:703, in BailErrorStrategy.recoverInline(self, recognizer)
    702 def recoverInline(self, recognizer:Parser):
--> 703     self.recover(recognizer, InputMismatchException(recognizer))

File ~/source/interpres/.venv/lib/python3.10/site-packages/antlr4/error/ErrorStrategy.py:697, in BailErrorStrategy.recover(self, recognizer, e)
    696     context = context.parentCtx
--> 697 raise ParseCancellationException(e)

ParseCancellationException: None

Note that the OpenQASM parser doesn't have a problem with the following

while (true) {
  // while(true) {
  int x;
}
@PhilReinhold
Copy link

I think this is sort of an inherent problem with the "allow arbitrary text in cal blocks, and parse those cal blocks separately" approach. The only way to determine where the end of the cal block occurs is by assuming the block contains balanced curly braces. It seems like it would be better for the cal blocks should use a more unique syntax for start/end something like

cal {:
    ...
:}

or

cal {{{
   ...
}}}

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

No branches or pull requests

2 participants