0

I have a yacc parser in which commands like "abc=123" (VAR=VAR), abc=[1 2 3] (VAR=value_series/string) and abc=[[123]] can be parsed.

I need to parse abc=[xyz=[1 2 3] jkl=[3 4 5]].This grammar is failing due to ambiguity between rule 2 (I guess, it couldn't differentiate between value_series and the new rule.

I have tried a case: VAR_NAME EQUAL quote_or_brace model EQUAL quote_or_brace value_series quote_or_brace net EQUAL quote_or_brace value_series quote_or_brace quote_or_brace

It didn't work.

series:
   | PLUS series
   {
   }
   | series VAR_NAME EQUAL VAR_NAME
   {
     delete [] $2;
     delete [] $4;
   }
   | series VAR_NAME EQUAL quote_or_brace value_series quote_or_brace
   {
     delete [] $2;
   }
   | series VAR_NAME EQUAL quote_or_brace quote_or_brace value_series quote_or_brace quote_or_brace
   {
     delete [] $2;
   }
   | error
   {
     setErrorMsg(string("error"));
     YYABORT;
   };
3
  • Your question title is a little misleading I think, as what you seem to want is a grammar without any ambiguity. As for one way to remove ambiguity is to try to make your rules simpler. For example, instead of three rules beginning with series VAR_NAME EQUAL why not make a single rule as series var_assignment (or similar). Then var_assignment: VAR_NAME EQUAL assignment_rhs_expression. And so on. Commented Sep 9, 2019 at 10:29
  • why cant I add another rule in the same series? why is it unable to parse the new rule? Commented Sep 9, 2019 at 11:41
  • what I am looking for is a way to parse - abc=[xyz=[1 2 3] jkl=[3 4 5]]. What rule should I write to make it parse this? Commented Sep 9, 2019 at 17:00

1 Answer 1

0

If it was me, I would probably write a set of rules similar to this:

assignment_list
  : assignment
  | assignment assignment_list
  ;

assignment
  : VAR_NAME '=' assignment_rhs
  ;

assignment_rhs
  : expression
  | '[' opt_expression_list ']'  /* allows var1 = [ ... ] */
  ;

opt_expression_list
  : /* empty */
  | expression_list
  ;

expression_list
  : expression
  | expression expression_list
  ;

expression
  : VAR_NAME   /* a variable name, as in var1 = var2 */
  | NUMBER     /* some kind of number, as in var1 = 123 */
  | STRING     /* quoted string, as in var1 = "foo" */
  | assignment /* allows nesting of assignments, as in var1 = [var2 = 4] */
               /* also allows things like var1 = var2 = 123 */
  ;

Note that this isn't tested in anyway, and that I'm not fully sure about the recursion from expression to assigment.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.