How to correct grammar to recognize "dot" qualifier

Feb 7, 2014 at 7:55 PM
Hi,

I'm a novice user. I'm getting a syntax error after attempting to expand a grammar. The existing grammar accepts a list of identifiers like "A, B". The enhancement is to be able to intermix "qualified dot" identifier's such as "c.c". E.g. feed it a list like: "A, B, c.c"

A and B should be recognized as propStringId's and "c.c" should be an qualifiedField. The problem is that it tries to make the first token an Alias and then complains that there is no "dot."

Thanks,
Chris

Attempted to partially extract from the full grammar:

var propStringId = new IdentifierTerminal("propStringId", "_", "");
var field = new IdentifierTerminal("field", "_", "");
var alias = new IdentifierTerminal("alias", "_", "");
var dot = g.ToTerm(".");

var selectExprList = new NonTerminal("selectExprList");
var selectExprTerm = new NonTerminal("selectExprTerm");

selectExprList.Rule = MakePlusRule(selectExprList, comma, selectExprTerm);
selectExprTerm.Rule = propStringId | qualifiedField;

qualifiedField.Rule = alias + dot + field;
Coordinator
Feb 7, 2014 at 9:04 PM
you can't do that - define identical terminals (like alias, field, propStringId), unless you have very separate areas, surrounding context that can help parser determines which one to use for a string of characters. All three are essentially identifier, one and the same thing - the difference is in it's role inside the statement, which should be attributed after parsing.
Feb 7, 2014 at 9:32 PM
Edited Feb 7, 2014 at 9:34 PM
So if made them the same then I still seem to have the same problem because the leading prefix "alias plus dot" is optional for each term in the list but the parser keeps going down the scopeDot path expecting the ".".

var stringId = new IdentitiferTerminal("stringId", "_", "");
var dot = g.ToTerm(".");

var optionallyQualifiedStringId = new NonTerminal("optionallyQualifiedStringId");
var selectExprList = new NonTerminal("selectExprList");
var selectExprTerm = new NonTerminal("selectExprTerm");

selectExprList.Rule = MakePlusRule(selectExprList, comma, selectExprTerm);
selectExprTerm.Rule = optionallyQualifiedStringId;

optionallyQualifiedStringId.Rule = optScopeDot + stringId;
optScopeDot.Rule = Empty | scopeDot;
scopeDot.Rule = scope + dot;


Oh, wait, I think maybe I see, do I have to flip it and make the first part (scope) required and then the second part (dot plus id) optional (and unwind the meaning after parsing)?
Feb 7, 2014 at 9:50 PM
Yes that is working! Thanks for your comments.

var stringId = new IdentitiferTerminal("stringId", "_", "");
var dot = g.ToTerm(".");
var optionallyQualifiedStringId = new NonTerminal("optionallyQualifiedStringId");
var selectExprList = new NonTerminal("selectExprList");
var selectExprTerm = new NonTerminal("selectExprTerm");

selectExprList.Rule = MakePlusRule(selectExprList, comma, selectExprTerm);
selectExprTerm.Rule = optionallyQualifiedStringId;

optionallyQualifedStringId.Rule = stringId + optDotStringId; // put optional part at end rather than front
optDotStringId.Rule = Empty | dotStringId;
dotStringId.Rule = dot + stringId;
Marked as answer by crokusek on 2/7/2014 at 2:50 PM