Keyword Identifier

Aug 29, 2012 at 7:58 PM

Hi all,

I wanna know if is possible to allow the following:

1- In the grammar the word "delete" is considered as a keyword.

2- In some cases i need to allow that an object's method is named using that keyword, for example:

obj.delete = function(){screenManager.alert('hello');};

But in this case, the grammar throws a parsing error because after the "." its expecting an Identifier as defined in the rules and not a Keyword.

Thanks,

Developer
Aug 30, 2012 at 4:21 PM

Hi aquintana,

could you please post your grammar?
I tried to reproduce your case, but my sample grammar works well:

// csc.exe test.cs /r:Irony.dll 

using System;
using Irony;
using Irony.Parsing;

class Program
{
	// sample code to parse
	const string snippet = "program.main; obj.delete; delete obj;";

	static void Main()
	{
		var parser = new Parser(new SampleGrammar());
		var tree = parser.Parse(snippet);

		Console.WriteLine("Parsing: {0}", snippet);
		if (tree.ParserMessages.Count == 0)
		{
			Console.WriteLine("No errors.");
		}
	}
}

class SampleGrammar : Grammar
{
	public SampleGrammar() : base(true)
	{
		// terminals
		var Identifier = new IdentifierTerminal("Identifier", "_-", "");

		// non-terminals
		var Program = new NonTerminal("Program");
		var Statement = new NonTerminal("Statement");
		var Expression = new NonTerminal("Expression");
		var DeleteStatement = new NonTerminal("DeleteStatement");

		// rules
		Program.Rule = MakePlusRule(Program, Statement);
		Statement.Rule = DeleteStatement | Expression;
		Expression.Rule = Identifier + "." + Identifier + ";";
		DeleteStatement.Rule = ToTerm("delete") + Identifier + ";";
		Root = Program;
	}
}