我正在尝试创建一个简单的反讽解析器。语法应该是
能够识别其中一个或多个
setlink( “串”);
它似乎成功识别了1个单一的keyword()调用,但是连续两个关键字爆炸了。我收到错误状态。当我检查main中的parseTree变量时,我可以看到它在停止解析之前识别的最后一个标记是字母“c”,即“connect”标记中的第一个字母。谁能告诉我这里缺少什么?
code.txt的内容
setlink("stuffs");
connect("things");
LilGrammar.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Irony.Parsing;
namespace IronyTest
{
public class LilGrammar : Grammar
{
public LilGrammar()
{
//Define terminals
var program = new NonTerminal("program");
var command = new NonTerminal("command");
var commandList = new NonTerminal("commandList");
var keyword = new NonTerminal("keyword");
var stringLiteral = new StringLiteral("string", "\"", StringOptions.None);
var LPAREN = ToTerm("(");
var RPAREN = ToTerm(")");
var SEMICO = ToTerm(";");
var NL = ToTerm("\n");
this.Root = program;
program.Rule = command + Eof;
keyword.Rule = ToTerm("setlink") | "connect";
command.Rule = keyword + LPAREN + stringLiteral + RPAREN + SEMICO;
commandList.Rule = MakePlusRule(commandList,null , command);
this.Root = program;
MarkPunctuation(";");
}
}
}
Main.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Irony.Parsing;
using Irony;
using System.IO;
namespace IronyTest
{
private static void Main(string[] args)
{
Grammar grammar = new LilGrammar();
var parser = new Parser(grammar);
string path = @"c:\code\code.txt";
// Open the file to read from.
string readText = File.ReadAllText(path);
//Returns the root to a parase tree
//var tree = parser.Parse(readText, path);
ParseTree parseTree = parser.Parse(readText,path);
ParseTreeNode root = parseTree.Root;
}
答案 0 :(得分:3)
我认为解决方法是让程序包含一个命令列表,而不仅仅是一个命令。
因此,通过以下修改,它似乎正确解析。请注意,我ToTerm("connect")
看起来就像keyword.Rule
中的setlink一样,我将program.Rule
的定义移到了commandList.Rule
的定义之后,我改变了program.Rule
}引用commandList
,而不是command
。
keyword.Rule = ToTerm("setlink") | ToTerm("connect");
command.Rule = keyword + LPAREN + stringLiteral + RPAREN + SEMICO;
commandList.Rule = MakePlusRule(commandList, command);
program.Rule = commandList + Eof;
this.Root = program;