Inherits from NSObject
Conforms to NSCoding
Declared in CPRule.h
CPRule.m

Overview

The CPRule class represents a context free rule.

Rules are used to specify how your language is structured – each rule specifies that the non-terminal on their left can be constructed by finding the sequence specified on the right hand side.

Rules are added to CPGrammars to construct a language to parse.

During parsing, a CPParser will inform its delegate of which CPRule it has matched to form a reduction. The tag property is provided to allow you to easily identify which rule has been matched.

Tasks

Creating and Initialising a Rule

Configuring a Rule

  •   name

    Specifies the name of the non-terminal this rule describes.

    property
  •   rightHandSideElements

    Specifies the right hand side of the rule.

    property
  •   tag

    A tag used to identify the rule in the parser delegate.

    property
  •   representitiveClass

    The class used to represent syntax trees based off this rule.

    property

Properties

name

Specifies the name of the non-terminal this rule describes.

@property (readwrite, retain) NSString *name

Discussion

Specifies the name of the non-terminal this rule describes.

Declared In

CPRule.h

representitiveClass

The class used to represent syntax trees based off this rule.

@property (readwrite, assign) Class representitiveClass

Discussion

The class used to represent syntax trees based off this rule.

Declared In

CPRule.h

rightHandSideElements

Specifies the right hand side of the rule.

@property (readwrite, copy) NSArray *rightHandSideElements

Discussion

Specifies the right hand side of the rule.

Elements of this array must be CPGrammarSymbols.

Declared In

CPRule.h

tag

A tag used to identify the rule in the parser delegate.

@property (readwrite, assign) NSUInteger tag

Discussion

A tag used to identify the rule in the parser delegate.

Declared In

CPRule.h

Class Methods

ruleWithName:rightHandSideElements:

Creates a rule based on a non-terminal name and an array of grammar symbols to make up the right hand side.

+ (id)ruleWithName:(NSString *)name rightHandSideElements:(NSArray *)rightHandSideElements

Parameters

name

The non-terminal the rule reduces.

rightHandSideElements

An array of CPGrammarSymbols that make up the right hand side of the rule.

Return Value

Returns a CPRule based on the right hand side and non-terminal name.

Discussion

Creates a rule based on a non-terminal name and an array of grammar symbols to make up the right hand side.

Declared In

CPRule.h

ruleWithName:rightHandSideElements:representitiveClass:

Creates a rule based on a non-terminal name and an array of grammar symbols to make up the right hand side. Also specifies the class to allocate to represent this rule.

+ (id)ruleWithName:(NSString *)name rightHandSideElements:(NSArray *)rightHandSideElements representitiveClass:(Class)representitiveClass

Parameters

name

The non-terminal the rule reduces.

rightHandSideElements

An array of CPGrammarSymbols that make up the right hand side of the rule.

representitiveClass

The class used to represent this rule.

Return Value

Returns a CPRule based on the right hand side and non-terminal name.

Discussion

Creates a rule based on a non-terminal name and an array of grammar symbols to make up the right hand side. Also specifies the class to allocate to represent this rule.

Declared In

CPRule.h

ruleWithName:rightHandSideElements:tag:

Creates a rule based on a non-terminal name and an array of grammar symbols to make up the right hand side. Also specifies a tag by which the rule can be identified in a parser delegate

+ (id)ruleWithName:(NSString *)name rightHandSideElements:(NSArray *)rightHandSideElements tag:(NSUInteger)tag

Parameters

name

The non-terminal the rule reduces.

rightHandSideElements

An array of CPGrammarSymbols that make up the right hand side of the rule.

tag

A tag to identify the rule by.

Return Value

Returns a CPRule based on the right hand side and non-terminal name.

Discussion

Creates a rule based on a non-terminal name and an array of grammar symbols to make up the right hand side. Also specifies a tag by which the rule can be identified in a parser delegate

Declared In

CPRule.h

Instance Methods

initWithName:rightHandSideElements:

Initialises a rule based on a non-terminal name and an array of grammar symbols to make up the right hand side.

- (id)initWithName:(NSString *)name rightHandSideElements:(NSArray *)rightHandSideElements

Parameters

name

The non-terminal the rule reduces.

rightHandSideElements

An array of CPGrammarSymbols that make up the right hand side of the rule.

Return Value

Returns a CPRule based on the right hand side and non-terminal name.

Discussion

Initialises a rule based on a non-terminal name and an array of grammar symbols to make up the right hand side.

Declared In

CPRule.h

initWithName:rightHandSideElements:representitiveClass:

Initialises a rule based on a non-terminal name and an array of grammar symbols to make up the right hand side. Also specifies the class to allocate to represent this rule.

- (id)initWithName:(NSString *)name rightHandSideElements:(NSArray *)rightHandSideElements representitiveClass:(Class)representitiveClass

Parameters

name

The non-terminal the rule reduces.

rightHandSideElements

An array of CPGrammarSymbols that make up the right hand side of the rule.

representitiveClass

The class used to represent this rule.

Return Value

Returns a CPRule based on the right hand side and non-terminal name.

Discussion

Initialises a rule based on a non-terminal name and an array of grammar symbols to make up the right hand side. Also specifies the class to allocate to represent this rule.

Declared In

CPRule.h

initWithName:rightHandSideElements:tag:

Initialises a rule based on a non-terminal name and an array of grammar symbols to make up the right hand side. Also specifies a tag by which the rule can be identified in a parser delegate

- (id)initWithName:(NSString *)name rightHandSideElements:(NSArray *)rightHandSideElements tag:(NSUInteger)tag

Parameters

name

The non-terminal the rule reduces.

rightHandSideElements

An array of CPGrammarSymbols that make up the right hand side of the rule.

tag

A tag to identify the rule by.

Return Value

Returns a CPRule based on the right hand side and non-terminal name.

Discussion

Initialises a rule based on a non-terminal name and an array of grammar symbols to make up the right hand side. Also specifies a tag by which the rule can be identified in a parser delegate

Declared In

CPRule.h