我正在尝试为Allen Bradly SLC语言/文件格式编写解析器。我已经成功地解析了寄存器参考。即N5:4/3。但是,当我尝试进入下一个级别时,它解析由空格分隔的寄存器引用列表,则会引发以下错误
输入:
N30:3/8 B20:3/3
错误:
((L1,C9)语法错误,应为::
这是我的代码,可以构建该文件并将dll加载到Irony Grammar Explorer中
using System;
using Irony.Parsing;
namespace Irony.Samples.SLC
{
[Language("SLC", "1.0", "RS Logix 500 SLC")]
public class SLCGrammar : Grammar
{
public SLCGrammar()
{
//Terminals
var SLCFilePrefix = new FixedLengthLiteral("Type", 1, TypeCode.String);
var SLCRegisterWord = new NumberLiteral("Word");
var SLCRegisterBit = new NumberLiteral("Bit");
var SLCRegisterFileNumber = new NumberLiteral("FileNumber");
//Nonterminals
var SLCInstructionRegisterList = new NonTerminal("InstructionRegisterList");
var SLCRegisterReference = new NonTerminal("RegisterReference");
var SLCRegisterReferenceWordOrBit = new NonTerminal("RegisterReferenceWordOrBit");
var SLCRegisterReferenceWordWithBit = new NonTerminal("RegisterReferenceWordWithBit");
//Rules
SLCRegisterReferenceWordWithBit.Rule = SLCRegisterWord + "/" + SLCRegisterBit;
SLCRegisterReferenceWordOrBit.Rule = SLCRegisterReferenceWordWithBit | SLCRegisterWord;
SLCRegisterReference.Rule = SLCFilePrefix + SLCRegisterFileNumber + ":" + SLCRegisterReferenceWordOrBit;
SLCInstructionRegisterList.Rule = MakePlusRule(SLCInstructionRegisterList, SLCRegisterReference);
//Set grammar root
this.Root = SLCInstructionRegisterList;
//MarkPunctuation(" ");
}//constructor
}//class
}//namespace
如果我更改以下行
SLCInstructionRegisterList.Rule = MakePlusRule(SLCInstructionRegisterList, SLCRegisterReference);
收件人
SLCInstructionRegisterList.Rule = MakePlusRule(SLCInstructionRegisterList, ToTerm(" "), SLCRegisterReference);
我知道
错误:(L1,C9)语法错误,预期:
我认为这意味着它期待一个空格字符
任何帮助将不胜感激。我刚刚开始学习讽刺意味,并且没有大量的文档。
注:以后我希望能够解析采用这种格式T8:5/DN
的寄存器,这意味着在正斜杠之后是字符串而不是数字。终止的是空格
使用ValidateTokenMethod,并拒绝所有不是A-Z的东西解决了此问题。
问题是,/被解释为新的寄存器参考的开始
var SLCFilePrefix = new FixedLengthLiteral("Type", 1, TypeCode.String);
SLCFilePrefix.ValidateToken += (e, s) =>
{
if (char.IsUpper(s.Token.ValueString[0]) == false)
{
s.RejectToken();
}
};
感谢Roman Ivantsov(原始创建者)通过电子邮件做出回复并帮助我。