Class: Dendroid::Syntax::Grammar
- Inherits:
-
Object
- Object
- Dendroid::Syntax::Grammar
- Defined in:
- lib/dendroid/syntax/grammar.rb
Overview
A grammar specifies the syntax of a language.
Formally, a grammar has:
* One start symbol,
* One or more other production rules,
* Each production has a rhs that is a sequence of grammar symbols.
* Grammar symbols are categorized into:
-terminal symbols
-non-terminal symbols
Instance Attribute Summary collapse
-
#name2symbol ⇒ Hash{String|Symbol => Dendroid::Syntax::GrmSymbol}
readonly
A Hash that maps symbol names to their grammar symbols.
-
#nonterm2production ⇒ Hash{Dendroid::Syntax::GrmSymbol => Dendroid::Syntax::Rule}
readonly
A Hash that maps symbol names to their grammar symbols.
-
#rules ⇒ Array<Dendroid::Syntax::Rule>
readonly
The list of production rules for the language.
-
#symbols ⇒ Array<Dendroid::Syntax::GrmSymbol>
readonly
The list of grammar symbols in the language.
Instance Method Summary collapse
-
#add_rule(rule) ⇒ Object
Add a rule to the grammar.
-
#complete! ⇒ Object
A event method to notify the grammar that all grammar rules have been entered.
-
#initialize(terminals) ⇒ Grammar
constructor
Constructor.
-
#start_symbol ⇒ Dendroid::Syntax::NonTerminal
Return the start symbol for the language, that is, the non-terminal symbol used to denote the top-level construct of the language being defined.
Constructor Details
#initialize(terminals) ⇒ Grammar
Constructor.
34 35 36 37 38 39 40 |
# File 'lib/dendroid/syntax/grammar.rb', line 34 def initialize(terminals) @symbols = [] @name2symbol = {} @rules = [] @nonterm2production = {} add_terminals(terminals) end |
Instance Attribute Details
#name2symbol ⇒ Hash{String|Symbol => Dendroid::Syntax::GrmSymbol} (readonly)
A Hash that maps symbol names to their grammar symbols
22 23 24 |
# File 'lib/dendroid/syntax/grammar.rb', line 22 def name2symbol @name2symbol end |
#nonterm2production ⇒ Hash{Dendroid::Syntax::GrmSymbol => Dendroid::Syntax::Rule} (readonly)
A Hash that maps symbol names to their grammar symbols
30 31 32 |
# File 'lib/dendroid/syntax/grammar.rb', line 30 def nonterm2production @nonterm2production end |
#rules ⇒ Array<Dendroid::Syntax::Rule> (readonly)
The list of production rules for the language.
26 27 28 |
# File 'lib/dendroid/syntax/grammar.rb', line 26 def rules @rules end |
#symbols ⇒ Array<Dendroid::Syntax::GrmSymbol> (readonly)
The list of grammar symbols in the language.
18 19 20 |
# File 'lib/dendroid/syntax/grammar.rb', line 18 def symbols @symbols end |
Instance Method Details
#add_rule(rule) ⇒ Object
Add a rule to the grammar.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/dendroid/syntax/grammar.rb', line 46 def add_rule(rule) if lhs_already_defined?(rule) msg = "Non-terminal '#{rule.head}' is on left-hand side of more than one rule." raise StandardError, msg end if duplicate_rule?(rule) raise StandardError, "Duplicate production rule '#{rule}'." end add_symbol(rule.head) rule.nonterminals.each { |nonterm| add_symbol(nonterm) } rules << rule nonterm2production[rule.head] = rule end |
#complete! ⇒ Object
A event method to notify the grammar that all grammar rules have been entered. The grammar, in turn, reacts by validating the production rules.
73 74 75 76 |
# File 'lib/dendroid/syntax/grammar.rb', line 73 def complete! validate analyze end |
#start_symbol ⇒ Dendroid::Syntax::NonTerminal
Return the start symbol for the language, that is, the non-terminal symbol used to denote the top-level construct of the language being defined.
66 67 68 |
# File 'lib/dendroid/syntax/grammar.rb', line 66 def start_symbol rules.first.lhs end |