CPGrammar Class Reference
| Inherits from | NSObject | 
| Conforms to | NSCoding | 
| Declared in | CPGrammar.h | 
Overview
The CPGrammar class represents a context free grammar. Grammars can be used later to construct parsers.
Tasks
Creating and Initialising a Grammar
- 
	
		
+ grammarWithStart:rules:Creates a grammar based on a starting non-terminal and a list of rules.
 - 
	
		
+ grammarWithStart:backusNaurForm:Creates a grammar based on a starting non-terminal and some backus naur form.
 - 
	
		
+ grammarWithStart:backusNaurForm:error:Creates a grammar based on a starting non-terminal and some backus naur form.
 - 
	
		
– initWithStart:rules:Initialises a grammar based on a starting non-terminal and a list of rules.
 - 
	
		
– initWithStart:backusNaurForm:Initialises a grammar based on a starting non-terminal and some backus naur form.
 - 
	
		
– initWithStart:backusNaurForm:error:Initialises a grammar based on a starting non-terminal and some backus naur form.
 
Configuring a Grammar
- 
	
		
– allRulesThe set of rules in the grammar.
 - 
	
		
– addRule:Adds a rule to the grammar.
 
Retreiving Grammar Rules
- 
	
		
– allNonTerminalNamesAll the non-terminals that the grammar can expand.
 - 
	
		
– rulesForNonTerminalWithName:The rules relevant when attempting to match a non-terminal.
 - 
	
		
startThe starting symbol for the grammar.
property 
Class Methods
grammarWithStart:backusNaurForm:
Creates a grammar based on a starting non-terminal and some backus naur form.
+ (id)grammarWithStart:(NSString *)start backusNaurForm:(NSString *)bnfParameters
- start
 The non-terminal that all parses must reduce to.
- bnf
 BNF for the grammar.
Return Value
Returns a CPGrammar based on the BNF and starting non-terminal.
Discussion
Creates a grammar based on a starting non-terminal and some backus naur form.
see initWithStart:backusNaurForm: for a description of the syntax used for BNF.
Bug: Warning this method is deprecated, use grammarWithStart:backusNaurForm:error: instead.
Declared In
CPGrammar.hgrammarWithStart:backusNaurForm:error:
Creates a grammar based on a starting non-terminal and some backus naur form.
+ (id)grammarWithStart:(NSString *)start backusNaurForm:(NSString *)bnf error:(NSError **)errorParameters
- start
 The non-terminal that all parses must reduce to.
- bnf
 BNF for the grammar.
- error
 A pointer to an error object which will be filled if the method returns nil.
Return Value
Returns a CPGrammar based on the BNF and starting non-terminal.
Discussion
Creates a grammar based on a starting non-terminal and some backus naur form.
see initWithStart:backusNaurForm: for a description of the syntax used for BNF.
See Also
Declared In
CPGrammar.hgrammarWithStart:rules:
Creates a grammar based on a starting non-terminal and a list of rules.
+ (id)grammarWithStart:(NSString *)start rules:(NSArray *)rulesParameters
- start
 The non-terminal that all parses must reduce to.
- rules
 An array of CPRules to describe the grammar.
Return Value
Returns a CPGrammar based on the rules and starting non-terminal.
Discussion
Creates a grammar based on a starting non-terminal and a list of rules.
Declared In
CPGrammar.hInstance Methods
addRule:
Adds a rule to the grammar.
- (void)addRule:(CPRule *)ruleParameters
- rule
 The rule to add.
Discussion
Adds a rule to the grammar.
Declared In
CPGrammar.hallNonTerminalNames
All the non-terminals that the grammar can expand.
- (NSArray *)allNonTerminalNamesReturn Value
An array of non-terminal names that are explained by the grammar.
Discussion
All the non-terminals that the grammar can expand.
Declared In
CPGrammar.hallRules
The set of rules in the grammar.
- (NSSet *)allRulesReturn Value
Returns the set of rules used to describe the grammar.
Discussion
The set of rules in the grammar.
Declared In
CPGrammar.hinitWithStart:backusNaurForm:
Initialises a grammar based on a starting non-terminal and some backus naur form.
- (id)initWithStart:(NSString *)start backusNaurForm:(NSString *)bnfParameters
- start
 The non-terminal that all parses must reduce to.
- bnf
 BNF for the grammar.
Return Value
Returns a CPGrammar based on the BNF and starting non-terminal.
Discussion
Initialises a grammar based on a starting non-terminal and some backus naur form.
The BNF is expressed using rules in the form nonTerminal ::= <subNonTerminal> "subTerminal" <subNonTerminal>;.  Rules may optionally be prefixed with a number indicating their tag.
This allows you to quickly construct grammars in a readable form.
You may also use EBNF to construct grammars.  This allows you to use the symbols “*”, “+”, and “?” to indicate that a construction may appear 0 or more; 1 or more; and 0 or 1 times respectively.
You may also parenthesise subrules.
When you use any of the above EBNF constructs or parentheses, the parser will return the contents in an NSArray.
You may use tags to identify sections of rules to be extracted in your result classes or parser delegate.  To do this, use the syntax nonTerminal ::= foo@<subNonTerminal> bar@"subTerminal";.  You may use multiple tags to identify the same section of a rule.  For example range ::= min@'Number' '-' max@'Number' | min@'Number' '-' | '-' max@'Number' | min@max@'Number'.
The grammar used for parsing the BNF can be expressed as follows:
ruleset                 ::= <rule>+;
rule                    ::= "Number"? <unNumbered>;
unNumbered              ::= "Identifier" "::=" <rightHandSide> ";";
rightHandSide           ::= (<sumset> "|")* <sumset>?;
sumset                  ::= <taggedRightHandSideItem>+;
taggedRightHandSideItem ::= ("Identifier" "@")? <rightHandSideItem>;
rightHandSideItem       ::= <unit> <repeatSymbol>?;
unit                    ::= <gramarSymbol> | "(" <rightHandSide> ")";
repeatSymbol            ::= "*" | "+" | "?";
grammarSymbol           ::= <nonTerminal> | <terminal>;
nonTerminal             ::= "<" "Identifier" ">";
terminal                ::= "String";
Bug: Warning this method is deprecated, use initWithStart:backusNaurForm:error: instead.
Declared In
CPGrammar.hinitWithStart:backusNaurForm:error:
Initialises a grammar based on a starting non-terminal and some backus naur form.
- (id)initWithStart:(NSString *)start backusNaurForm:(NSString *)bnf error:(NSError **)errorParameters
- start
 The non-terminal that all parses must reduce to.
- bnf
 BNF for the grammar.
- error
 A pointer to an error object which will be filled if the method returns nil.
Return Value
Returns a CPGrammar based on the BNF and starting non-terminal.
Discussion
Initialises a grammar based on a starting non-terminal and some backus naur form.
The BNF is expressed using rules in the form nonTerminal ::= <subNonTerminal> "subTerminal" <subNonTerminal>;.  Rules may optionally be prefixed with a number indicating their tag.
This allows you to quickly construct grammars in a readable form.
You may also use EBNF to construct grammars.  This allows you to use the symbols “*”, “+”, and “?” to indicate that a construction may appear 0 or more; 1 or more; and 0 or 1 times respectively.
You may also parenthesise subrules.
When you use any of the above EBNF constructs or parentheses, the parser will return the contents in an NSArray.
You may use tags to identify sections of rules to be extracted in your result classes or parser delegate.  To do this, use the syntax nonTerminal ::= foo@<subNonTerminal> bar@"subTerminal";.  You may use multiple tags to identify the same section of a rule.  For example range ::= min@'Number' '-' max@'Number' | min@'Number' '-' | '-' max@'Number' | min@max@'Number'.
The grammar used for parsing the BNF can be expressed as follows:
ruleset                 ::= <rule>+;
rule                    ::= "Number"? <unNumbered>;
unNumbered              ::= "Identifier" "::=" <rightHandSide> ";";
rightHandSide           ::= (<sumset> "|")* <sumset>?;
sumset                  ::= <taggedRightHandSideItem>+;
taggedRightHandSideItem ::= ("Identifier" "@")? <rightHandSideItem>;
rightHandSideItem       ::= <unit> <repeatSymbol>?;
unit                    ::= <gramarSymbol> | "(" <rightHandSide> ")";
repeatSymbol            ::= "*" | "+" | "?";
grammarSymbol           ::= <nonTerminal> | <terminal>;
nonTerminal             ::= "<" "Identifier" ">";
terminal                ::= "String";
	Declared In
CPGrammar.hinitWithStart:rules:
Initialises a grammar based on a starting non-terminal and a list of rules.
- (id)initWithStart:(NSString *)start rules:(NSArray *)rulesParameters
- start
 The non-terminal that all parses must reduce to.
- rules
 An array of CPRules to describe the grammar.
Return Value
Returns a CPGrammar based on the rules and starting non-terminal.
Discussion
Initialises a grammar based on a starting non-terminal and a list of rules.
Declared In
CPGrammar.hrulesForNonTerminalWithName:
The rules relevant when attempting to match a non-terminal.
- (NSArray *)rulesForNonTerminalWithName:(NSString *)nonTerminalNameParameters
- nonTerminalName
 The name of the non-terminal to find rules to match.
Return Value
Returns all rules that match a particular non-terminal.
Discussion
The rules relevant when attempting to match a non-terminal.
Declared In
CPGrammar.h