Simple Irony Parser not recognizing more than 1 instance of a particular matched item

934 Views Asked by At

I am attempting to create a simple Irony parser. The grammar is supposed to be

able to recognize one or more of these

setlink("string");

It seems to successfully recognize 1 single keyword() call but two consecutive ones blow up. I get an error status. When I inspect parseTree variable in the main, I can see the the last token that it identifies before it stops the parsing is the letter "c", the first letter in "connect" token. Can anyone tell me what I am missing here?

Contents of 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;
 }
1

There are 1 best solutions below

1
DWright On

I think the fix is to make the program consist of a list of commands, not just one command.

So, with the following modifications, it appears to parse correctly. Note that I made ToTerm("connect") look just like setlink in the keyword.Rule and that I moved the definition of program.Rule to after the definition of commandList.Rule and that I changed program.Rule to refer to commandList, not 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;