IT:AD:TinyPG:HowTo:Create a Grammar
Summary
A grammar is defined as a extended-BNF notation file of terminal and non-terminal symbols.
Process
Open the TinyPG app, write a grammar.
Directives
<% @TinyPG Namespace="XAct.MyApp" //Namespace of generated `Scanner.cs`, `Parser.cs`, `ParseTree.cs`. Default is 'TinyPG'.
Language="C#" //Output language (`VB` or `C#`)
OutputPath="MyGrammarPath/" //Where to generate files. Default is './'
TemplatePath="MyParserTemplates" //relative to TinyPG.exe. Default= "Templates"
%>
<% @Parser Generate="True" %> //specifies whether to generate file. Default=true
<% @ParserTree Generate="False" %> // turn off generation of the the ParseTree.cs file
<% @Scanner Generate="True" %> //Default:true
<% @TextHighlighter Generate="True" %> //Default:false
Define Terminal Symbols
Example of terminal symbols:
BOOLEANLITERAL -> @"true|false";
DECIMALINTEGERLITERAL-> @"[0-9]+(UL|Ul|uL|ul|LU|Lu|lU|lu|U|u|L|l)?";
REALLITERAL -> @"([0-9]+\.[0-9]+([eE][+-]?[0-9]+)?([fFdDMm]?)?)|(\.[0-9]+([eE][+-]?[0-9]+)?([fFdDMm]?)?)|([0-9]+([eE][+-]?[0-9]+)([fFdDMm]?)?)|([0-9]+([fFdDMm]?))";
HEXINTEGERLITERAL -> @"0(x|X)[0-9a-fA-F]+";
STRINGLITERAL -> @"\""(\""\""|[^\""])*\"""; // shortcut implementation, not quite ready
FUNCTION -> @"[a-zA-Z_][a-zA-Z0-9_]*(?=\s*\()"; // matches only when followed by (
VARIABLE -> @"[a-zA-Z_][a-zA-Z0-9_]*(?!\s*\()"; // matches only when NOT followed by (
CONSTANT -> @"(?i)pi|e";
BRACEOPEN -> @"{\s*";
BRACECLOSE -> @"\s*}";
BRACKETOPEN -> @"\(\s*";
BRACKETCLOSE -> @"\s*\)";
SEMICOLON -> @";";
PLUSPLUS -> @"\+\+";
MINUSMINUS -> @"--";
PIPEPIPE -> @"\|\|";
AMPAMP -> @"&&";
AMP -> @"&(?!&)"; // matches only when NOT followed by second &
POWER -> @"\^";
PLUS -> @"\+";
MINUS -> @"-";
EQUAL -> @"==";
ASSIGN -> @"=";
NOTEQUAL -> @"!=|<>";
NOT -> @"!";
ASTERIKS -> @"\*";
SLASH -> @"/";
PERCENT -> @"%";
QUESTIONMARK -> @"\?";
COMMA -> @",";
LESSEQUAL -> @"<=";
GREATEREQUAL -> @">=";
LESSTHAN -> @"<(?!>)"; // matches only when not followed by >
GREATERTHAN -> @">";
COLON -> @":";
EOF -> @"^$";
[Skip] WHITESPACE -> @"\s+";
Define Non-Terminal Symbols
Compile to Parser
Press F6