I am brand-new to Irony (I studied BNF at uni about 10 years ago), and am starting by creating a quasi-real coordinate parser.
Below is the grammar class (full code at https://github.com/spadger/notam-visualiser) that I believed should have been able to parse input
70 50' 44" N 1 13' 66" E
However, when I run it in the grammar explorer, I get a syntax error at 1:4 "expected: space"
Can you see what is wrong with my code?
Thanks,
Jon
[Language("Simple non-real coordinates", "0.1", "A grammar to parse an imaginary coordinate system")]
public class CoordinateGrammar : Grammar
{
//70 50' 44" N 1 13' 66" E
public CoordinateGrammar()
{
#region Terminals
var integer = new NumberLiteral("integer", NumberOptions.IntOnly);
var space = ToTerm(" ", "space");
var point = ToTerm(".", "dot");
var lat = ToTerm("N", "north") | ToTerm("S", "south");
var lon = ToTerm("E", "east") | ToTerm("W", "west");
var minuteMarker = ToTerm("'", "minute");
var secondMarker = ToTerm("\"", "second");
#endregion
#region Non-Terminals
var decimalAmount = new NonTerminal("decimalAmount", typeof(DecimalAmountNode));
var minute = new NonTerminal("minute", typeof(MinuteNode));
var second = new NonTerminal("second", typeof(SecondNode));
var imperialMagnitude = new NonTerminal("decimalMagnitude", typeof (ImperialMagnitudeNode));
var imperialLatitude = new NonTerminal("imperialLatitude", typeof (ImperialLatitudeNode));
var imperialLongitude = new NonTerminal("imperialLongitude", typeof (ImperialLongitudeNode));
var imperialCoordinate = new NonTerminal("imperialCoordinate", typeof(ImperialCoordinateNode));
#endregion
#region Rules
decimalAmount.Rule = integer | integer + point + integer;
minute.Rule = integer + minuteMarker;
second.Rule = integer + secondMarker;
imperialMagnitude.Rule = integer + space + minute + space + second;
imperialLatitude.Rule = imperialMagnitude + space + lat;
imperialLongitude.Rule = imperialMagnitude + space + lon;
imperialCoordinate.Rule = imperialLatitude + space + imperialLongitude;
#endregion
Root = imperialCoordinate;
}
}
Fixed it!
I was manually expecting spaces, but the grammar was already configured (by default) to treat the 'space' character as a white-space marker. Updating the non-termanal rules to below fixed my problem.
decimalAmount.Rule = integer | integer + point + integer;
minute.Rule = integer + minuteMarker;
second.Rule = integer + secondMarker;
imperialMagnitude.Rule = integer + minute + second;
imperialLatitude.Rule = imperialMagnitude + lat;
imperialLongitude.Rule = imperialMagnitude + lon;
imperialCoordinate.Rule = imperialLatitude + imperialLongitude;