2

Is it possible to fallback to other rules after an exception is raised in a semantic action? Like the following (contrived) scenario:

var = /[a-zA-Z]+/;
keyword = 'for' | 'in';
a = var:var | keyword:keyword;

def a(ast):
    if (ast.var not in symbolTable):
        raise Exception()

and when the exception is raised, parsing continues with the 'keyword' rule. I am aware of the @@keyword feature, but I want to declare keywords at runtime (my parser is for a programming language with user-defined operators).

Sid Mani
  • 369
  • 3
  • 9
  • Can you provide some more context for this? – AMC Jan 08 '20 at 16:50
  • 1
    I am writing an interpreter for a math-oriented programming language and am using Tatsu to parse the syntax. One issue I'm facing is that since implicit multiplication is supported, the expression f(3) could be parsed as "function f evaluated at 3" or "f * 3". I want to use my symbol table to perform the disambiguation while parsing, by (for example) throwing an exception in the semantic action to mark a rule as failed even after parsing succeeds, and then have the parser continue to the next rule. – Sid Mani Jan 09 '20 at 02:18

1 Answers1

2

If the semantics code raises tatsu.exceptions.FailedSemantics, the exception will be treated exactly as a ParseException, so the normal parsing control-flow will be resumed.

Apalala
  • 9,017
  • 3
  • 30
  • 48