2

I'm using the Irony DSL framework to parse a language. That language allows strings to be written without quotes ("), even if they contain special characters like dot (.) or minus (-).

If I editthe text files I get, so the strings have quotes around them, they can be parsed just fine. However, I want to read the files as they are. How can I make Irony parse a string without setting a StartEndSymbol?

I'm using the current version of Irony (2012_03_15).

4

3 回答 3

2

查看示例 SQL 语法。它允许标识符包含'.'诸如SELECT a.x FROM table. 在这种情况下,“Id”将是一个带有分隔符的列表。

var dot = ToTerm(".");
var dash = ToTerm("-");
var Id_simple = TerminalFactory.CreateSqlExtIdentifier(this, "id_simple"); //covers normal identifiers (abc) and quoted id's ([abc d], "abc d")
Id.Rule = MakePlusRule(Id, dot, Id_simple) | MakePlusRule(Id, dash, Id_simple)

 //Covers simple identifiers like abcd, and also quoted versions: [abc d], "abc d".
    public static IdentifierTerminal CreateSqlExtIdentifier(Grammar grammar, string name) {
      var id = new IdentifierTerminal(name);
      StringLiteral term = new StringLiteral(name + "_qouted");
      term.AddStartEnd("[", "]", StringOptions.NoEscapes);
      term.AddStartEnd("\"", StringOptions.NoEscapes);
      term.SetOutputTerminal(grammar, id); //term will be added to NonGrammarTerminals automatically 
      return id;
    }

如果它适用于 SQL,它应该适用于您。

于 2012-07-25T11:11:13.203 回答
1

让我试着澄清一下。CreateSqlExtIdentifier 类中“id”变量的声明:“//涵盖简单的标识符,如 abcd,以及引用的版本:[abc d]、“abc d”和下划线和数字,但不是更多。事实上,SQL 解析器包含点像“id”的部分一样 - 是以下规则的结果: **"Id.Rule = MakePlusRule(Id, dot, Id_simple);"** 作为一种解决方案,您可以声明特殊字符(如果没有很多)并制作您的自定义规则,这将在代码中包含可能的出现。
例如:
specialSymbols.Rule = ToTerm(".") | "-" | ...etc.;
myId.Rule = id_simple | specialSymbols;
myIds.Rule = MakePlusRule(myIds<,Delimiter if nedeed>,myId);

最好的问候,迪奥尼斯。

于 2016-03-18T07:47:15.590 回答
0

我也想看看这样做的正确方法。目前我只是通过在其构造函数中设置必要的额外字符来使用 IdentifierTerminal。

于 2012-07-24T15:37:30.343 回答