Simple parser but does not discern numbers #4645
Replies: 10 comments
-
Hmm okay so it is actually throwing |
Beta Was this translation helpful? Give feedback.
-
I thought perhaps the
|
Beta Was this translation helpful? Give feedback.
-
Ah 💡 I think I see, maybe it is working, and I just need to connect some dots in the listener now. In my unit tests I verify against the top level coordinates rule, instead of the inward coordinate rule. parser.AddParseListener(listener);
IParseTree tree = parser.coordinatesDecl(); I also tweaked the grammar above, I think I see an issue there.
|
Beta Was this translation helpful? Give feedback.
-
Also I dare say, that was good thinking allowing subscribers the opportunity to extend a base class into the partial listener classes, for instance. Especially if some common baseline infrastructure is required extending into the listener override contexts, i.e. // As furnished by the Antlr4 code generation
public partial class MyGrammarParserBaseListener : IMyGrammarParserListener
{
}
// ...
// Sliding infrastructure service base underneath the listener
public partial class MyGrammarParserBaseListener : MyGrammarServiceBase
// Service based infrastructure: ^^^^^^^^^^^^^^^^^^^^
{
// ...
} And extending into the actual listener with overrides... public class MyGrammarParserListener : MyGrammarParserBaseListener
{
public override void EnterMyRule([NotNull] MyGrammarParser.MyRuleContext context)
{
// ...
}
public override void ExitMyRule([NotNull] MyGrammarParser.MyRuleContext context)
{
// ...
}
} I do not know if that was by design, necessarily, or just a happy accident, but will take the win. 🍻 |
Beta Was this translation helpful? Give feedback.
-
Under some xunit testing, I have breakpoints on each of my rule enter/exit pairs, and I seemed to have it working, briefly, for a moment, rules were more or less reporting their enter/exit edges, but with this bit of adjustment, does not appear to be the case. I get a lines enter/exit pair, and that's it. Whatever might have been produced seems to be flattened to the lines rule, which does show my test case data, Definitely in the unit test, my By comparison, if I think in terms of a Boost Spirit, let's say, I would like for each of the pairs to be producing something, that which they should be producing, or at least offer an opportunity to roll things up into said production, if that makes any sense. grammar ScumParser;
// Lexer rules
DIGIT: [0-9];
X: [xX];
Y: [yY];
Z: [zZ];
DOT: '.';
SEP: ' ' | '\t';
DECIMAL: ( DIGIT+ DOT? ) | ( DIGIT* DOT DIGIT+ );
OP_EQ: '=';
// Carriage Return, Line Feed.
fragment CR: '\r';
fragment LF: '\n';
// Space.
fragment SP: ' ';
// Tab, or Horizontal Tab.
fragment HT: '\t';
// Form Feed, or Vertical Tab.
fragment VT: '\f';
fragment IdentPart: X | Y | Z;
fragment DecimalPart: DECIMAL;
// Whitespace
WS: ( SP | HT | VT )+ -> skip;
// NEWLINE : '\r'? '\n' { {System.out.println("Newlines so far: " + (++counter)); } };
NL: CR? LF -> skip;
// Grammar rules
linesDecl: lineDecl+;
// TODO: we may also want to consider simple comments, allowing event admins more flexibility conveying sets of coordinates
// Location is hundred percent optional in this instance i.e. lines may be blank
lineDecl: location? NL;
location: ( locationDecl | identLocationDecl );
locationDecl: coordinateDecl ( SEP coordinateDecl )*;
identLocationDecl: identCoordinateDecl ( SEP identCoordinateDecl )*;
// The case where coordinate may appear without identifier i.e. <decimal/>
coordinateDecl: DecimalPart;
// The case where coordinate may be axis labeled i.e. X|Y|Z = <decimal/>
identCoordinateDecl: IdentPart SEP OP_EQ SEP DecimalPart; |
Beta Was this translation helpful? Give feedback.
-
Ah I think I see what happened, had a snafu somewhere in one of the productions. I simplified and it flows cleanly now. 😄 |
Beta Was this translation helpful? Give feedback.
-
Curious though... why would Looking at the lexer parts... DECIMAL: ( DIGIT+ DOT? ) | ( DIGIT* DOT DIGIT+ );
Similarly Kleene |
Beta Was this translation helpful? Give feedback.
-
In a test case such as this: fragment X: [xX];
fragment Y: [yY];
fragment Z: [zZ];
AXIS: X | Y | Z; But it seems like to me it is tripping up over location: coordinate ( SEP+ coordinate )*;
axesLocation: axisCoordinate ( SEP+ axisCoordinate )*;
// The case where coordinate may appear without identifier i.e. <decimal/>
coordinate: DECIMAL_LITERAL;
// The case where coordinate may be axis labeled i.e. X|Y|Z = <decimal/>
axisCoordinate: AXIS SEP* OP_EQ SEP* DECIMAL_LITERAL; Along similar lines, cases such as |
Beta Was this translation helpful? Give feedback.
-
...decided to drop the horizontal tab from the definition. This was causing angst and not just for Antlr, but also xUnit, in ways I did not want to think about. Favored simple space separation, which seems to have cleared things up. Still not very happy that it seems to ignore "invalid" Axis use cases and land in the raw coordinate use cases. I'm not sure how to avert that, necessarily. I ended up throwing an internal exception when I discovered the corresponding |
Beta Was this translation helpful? Give feedback.
-
This thought occurred to me. Instead of fighting the alternate use case, what I really need, I feel, are two separate grammars. On feeding parser of the axis flavored coordinates. Another featuring the raw set of coordinates. Done and dusted. I think that's probably the best case scenario. No problem then parsing one, followed by the other, stitched up and self contained. Now if there is also a way to include common elements, we'll see if that is also a possibility. Cheers 🍻 |
Beta Was this translation helpful? Give feedback.
-
Goal is support a simple
Location DSL
, nothing too fancy, but is a smidge more complex than I want to fuss with getting regex correct, or cruder forms of tokenization, split strings, etc. However, it does not seem to discern numbers quite right, if I understand the listener rule enter/exit edges correctly. Couple of example use cases, the interpretation around which sometimes is positional, sometimes not, depending whether an identifier assignment is present.The grammar, tentatively:
My test case to start with is:
Just to see what I might get. The parser appears to succeed, but if I am reading the context correctly, I do not find any values, and context is indicating something like
"1 0 0 0 0 1 0 0 0 0"
, which at least it is scanning through the input correctly, so there's that. And with breakpoints set, definitely only hitting one set of enter exit coordinate edges there.But at some level, my goal is to see two coordinate edges, enter and exit, indicating
"1 0 0 0 0"
in each case.And forward from there.
What am I missing?
Beta Was this translation helpful? Give feedback.
All reactions