Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

Writing your first Domain Specific Language, Part 2 of 2

Rate me:
Please Sign up or sign in to vote.
5.00/5 (26 votes)
3 Sep 2008CPOL12 min read 85.6K   1.1K   75  
A guide to writing a compiler in .NET for beginners, using Irony.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Irony.Compiler;
using FreightLanguageCompiler.Nodes;

namespace FreightLanguageCompiler
{
	public class FLGrammar : Grammar
	{

		public FLGrammar()
		{

			#region Initial setup of the grammar

			// turn off case sensitivity
			this.CaseSensitive = false;

			// define all the non-terminals
			var program = new NonTerminal("program", typeof(ProgramNode));
			var statementList = new NonTerminal("statementList", typeof(StatementListNode));
			var freightDeclaration = new NonTerminal("freightDeclaration", typeof(FreightDeclarationNode));
			var statement = new NonTerminal("statement", typeof(StatementNode));
			var setVariable = new NonTerminal("setVariable", typeof(SetVariableNode));
			var ifStatement = new NonTerminal("ifStatement", typeof(IfStatementNode));
			var orderLoop = new NonTerminal("orderLoop", typeof(OrderLoopNode));
			var expression = new NonTerminal("expression", typeof(ExpressionNode));
			var binaryOperator = new NonTerminal("binaryOperator", typeof(BinaryOperatorNode));

			// define all the terminals
			var variable = new IdentifierTerminal("variable");
			variable.AddKeywords("set", "to", "if", "freight", "cost", "is", "loop", "through", "order");
			var number = new NumberLiteral("number");
			var stringLiteral = new StringLiteral("string", "\"", ScanFlags.None);

			// remove uninteresting nodes from the AST (note: in current version of Irony,
			// keywords added to the variable cannot be registered as punctuation).
			this.RegisterPunctuation(";", "[", "]", "(", ")");

			// specify the non-terminal which is the root of the AST
			this.Root = program;

			#endregion

			#region Define the grammar

			//<Program> ::= <StatementList> <FreightDeclaration>
			program.Rule = statementList + freightDeclaration;

			//<StatementList> ::= <Statement>*
			statementList.Rule = MakeStarRule(statementList, null, statement);

			//<Statement> ::= <SetVariable> ";" | <IfStatement> | <OrderLoop> | <Expression> ";"
			statement.Rule = setVariable + ";" | ifStatement | orderLoop | expression + ";";

			//<SetVariable> ::= "set" <Variable> "to" <Expression>
			setVariable.Rule = Symbol("set") + variable + "to" + expression;

			//<IfStatement> ::= "if" <Expression> "[" <StatementList> "]"
			ifStatement.Rule = Symbol("if") + expression + "[" + statementList + "]";

			//<OrderLoop> ::= "loop" "through" "order" "[" <StatementList> "]"
			orderLoop.Rule = Symbol("loop") + "through" + "order" + "[" + statementList + "]";

			// <FreightDeclaration> ::= "freight" "cost" "is" <Expression> ";"
			freightDeclaration.Rule = Symbol("freight") + "cost" + "is" + expression + ";";

			//<Expression> ::= <number> | <variable> | <string>
			//  | <Expression> <BinaryOperator> <Expression>
			//  | "(" <Expression> ")"
			expression.Rule = number | variable | stringLiteral
				| expression + binaryOperator + expression
				| "(" + expression + ")";

			//<BinaryOperator> ::= "+" | "-" | "*" | "/" | "<" | ">" | "<=" | ">=" | "is"
			binaryOperator.Rule = Symbol("+") | "-" | "*" | "/" | "<" | ">" | "<=" | ">=" | "is";

			#endregion

		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
China China
Daniel has a Bachelor of Science with First Class Honours from the University of Auckland, and has designed and developed software in companies large and small.

Comments and Discussions