反语语法规范错误

时间:2013-03-09 18:13:27

标签: c# grammar bnf abstract-syntax-tree irony

我对Irony来说是全新的(大约10年前我在大学学习过BNF),并开始创建一个准真实的坐标解析器。

下面是我认为应该能够解析输入的语法类(https://github.com/spadger/notam-visualiser的完整代码)

70 50' 44" N 1 13' 66" E

然而,当我在语法资源管理器中运行它时,我在1:4“预期:空间”时出现语法错误

你能看到我的代码出了什么问题吗?

谢谢,

乔恩

[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;
    }
}

1 个答案:

答案 0 :(得分:1)

修正了它!

我手动期待空格,但语法已经配置(默认情况下)将“空格”字符视为空格标记。将非termanal规则更新到下面修复了我的问题。

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;