Persistate API documentation
SyntaxParser Class
NamespacesPersistate.LanguageSyntaxParser

[This is preliminary documentation and is subject to change.]

Represents a syntax parser for a particular context free grammar.
Declaration Syntax
C#
public class SyntaxParser
Members
All MembersConstructorsMethodsPropertiesEvents



IconMemberDescription
SyntaxParser()()()()
Creates a new SyntaxParser with no productions.

CreateParser(Production, IGeneratorClass)
Fully creates a parser which has been given its full complement of completed Productions.

CreateProduction(String)
Adds a new Production to the parser with a given LHS.

DumpString(String)
Obtains a detailed text representation of this parser.

Equals(Object)
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Finalize()()()()
Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection.
(Inherited from Object.)
GenerateCode(ProductionToken)
Performs code generation with the output of the parser.

GenerateParser(String, IGeneratorClass, SyntaxParser%, ErrorToken%)
Generates a SyntaxParser for a particular grammar. The grammar and target language will be case sensitive.

GenerateParser(String, IGeneratorClass, Boolean, SyntaxParser%, ErrorToken%)
Generates a SyntaxParser for a particular grammar.

GenerateParserFromFile(String, IGeneratorClass, SyntaxParser%, ErrorToken%)
Generates a SyntaxParser for a particular grammar from a text file. The grammar and target language will be case sensitive.

GenerateParserFromFile(String, IGeneratorClass, Boolean, SyntaxParser%, ErrorToken%)
Generates a SyntaxParser for a particular grammar from a text file.

GetHashCode()()()()
Serves as a hash function for a particular type.
(Inherited from Object.)
GetType()()()()
Gets the Type of the current instance.
(Inherited from Object.)
MemberwiseClone()()()()
Creates a shallow copy of the current Object.
(Inherited from Object.)
NonTerminals
Gets the set of non terminals in this parser.

Parse(String, String)
Parses source text with this SyntaxParser.

ParseFile(String, String)
Parses the text of a source file with this SyntaxParser.

Productions
Gets the collection of Productions in this parser.

SymbolPattern
Gets or sets the regular expression pattern to be used to distinguish symbols during the lexical phase of the parse.

SyntaxError
This event is raised whenever the parser detects a syntax error in the source text input.

Terminals
Gets the set of terminals in this parser.

ToString()()()()
Returns a String that represents the current Object.
(Inherited from Object.)
Remarks

A SyntaxParser can handle any language which is describable with its particular variant of Extended Backus Naur Form grammar (EBNF) and which can be dealt with by an LALR(1) type parser. There are two ways to use this class. You can manually create a parser by using the SyntaxParser()()()() constructor and add productions manually using CreateProduction. This does not use a grammar file, and is not recommended for normal use.

The recommended way of using this SyntaxParser has three phases. In the first phase, you create the parser using the GenerateParser or GenerateParserFromFile methods, or one of their overloads. These methods use an EBNF grammar, which has the following differences from the standard EBNF.

  • There are no exclusions.
  • There are no numbered repeats.
  • Comma is not used for concatenation - elements are separated by spaces.
  • Parentheses are used only to group choices, and must be used thus.
  • The special terminals <symbol>, <string>, <integer>, <floating> and <datetime> are used to represent literals and symbols identified in the lexical scan of the source text.

Here is an EBNF grammar for the EBNF itself, describing itself in effect.

CopyC#
Start = Syntax ;
Syntax = ProductionRule { ProductionRule } ;
ProductionRule = Production "=" Elements ";" ;
Elements = Element { Element } ;
Element = ( Token | Terminal | Production | Option | Choice | Repeat ) ;
Token = ( "<symbol>" | "<string>" | "<integer>" | "<floating>" | "<datetime>" ) ;
Terminal = <string> ;
Production = <symbol> ;
Option = "[" Elements "]" ;
Choice = "(" Elements { "|" Elements } ")" ;  
Repeat = "{" Elements "}" ;

As well as supplying a grammar, you must supply a code generator class, which must implement IGeneratorClass. The methods in this class will be called during the third phase - code generation.

In the second phase, you parse the source code file. This is done by calling the Parse method. The output of this can be an ErrorToken if there were syntax errors, or a ProductionToken if there were not. This latter will be the root token of the Abstract Syntax Tree (AST) produced by the parser, and you use this as input into the next phase.

In the third phase you walk the AST, producing code, or whatever else you wish from the parsed contents. This is done from the bottom up, but you don't have to do it yourself. Call the GenerateCode method, passing the AST token produced by the Parse method. This will then start calling methods in your IGeneratorClass for each Production node in the AST, it will call the method with the name (LHS text) of that Production.

The methods should conform to the CodeGenerator delegate, getting the ProductionToken as parameter and producing a Token or null as result. If your IGeneratorClass has no method for a Production, then the ProductionToken will be removed from the AST, and its contained Tokens added to its parent Token in the tree.

In this method, you can examine the set of Tokens in the ProductionToken, which will correspond to the language elements in the source file which matched the Production. Some of these tokens could be ProductionTokens if the language elements matched a Production, or they could be GeneratedTokens, if a matched production had a corresponding method which returned one.

If you return null from a method, no action is performed. If you return an ErrorToken, this is added to the set of semantic errors which will be returned from GenerateCode. If you return any other type of Token, normally a GeneratedToken, then this will replace the ProductionToken and all tokens below it in the AST.

You can define any number of code generation passes you want by returning the number from the NumberOfPasses property, and the AST tree walk will happen that number of times. The AST itself could well be different in different passes, as you replace ProductionTokens with GeneratedTokens. At the end of the process, the Token returned by the topmost (start) production on the last pass will be returned from the GenerateCode method.

Inheritance Hierarchy
Object
SyntaxParser

Assembly: Persistate.Language (Module: Persistate.Language) Version: 0.6.1.20 (0.6.1.20)