Irony - Language Implementation Kit/Grammar

From Wikibooks, open books for an open world
< Irony - Language Implementation Kit
Jump to: navigation, search

From Wikipedia A formal grammar (sometimes simply called a grammar) is a set of rules of a specific kind, for forming strings in a formal language. The rules describe how to form strings from the language's alphabet that are valid according to the language's syntax. A grammar does not describe the meaning of the strings or what can be done with them in whatever context —only their form.

In Irony, grammars are described using a Backus-Naur-like notation inside a C# (or VB.NET) class that inherits from Irony.Parsing.Grammar. This is a marked difference from most other language tools like lex and yacc. The "|" and "+" operators have been overloaded so you can string together terminals to define productions. Afterward this class can be used to parse the language and along with other Irony features to interpret it.

To create a new grammar, just create a new class file and extend the base Grammar class found in Irony.Parsing. Here, you can set all the tokens and productions for the language as well as other properties like the list of reserved words or whether or not to create an abstract syntax tree.

The Irony project contains many examples of how to do this as well as the Grammar Explorer project which lets you test your grammar.

Typical sections in the grammar file are as follows, but obviously you can structure it however you want:

  • Language properties and flags
  • Terminals
  • Non Terminals
  • Productions
  • Registration of punctuation, operators, and keywords