Class: Racc::Grammar

### Home Classes Methods

### In Files

- lib/racc/grammar.rb

### Parent

Object

### Namespace

- CLASS Racc::Grammar::DefinitionEnv
- CLASS Racc::Grammar::PrecedenceDefinitionEnv

### Methods

- ::define
- ::new
- #[]
- #add
- #added?
- #declare_precedence
- #dfa
- #each
- #each_index
- #each_rule
- #each_with_index
- #end_precedence_declaration
- #init
- #intern
- #n_useless_nonterminals
- #n_useless_rules
- #nfa
- #nonterminal_base
- #parser_class
- #size
- #start_symbol=
- #state_transition_table
- #states
- #symbols
- #to_s
- #useless_nonterminal_exist?
- #useless_rule_exist?
- #write_log

### Files

- Manifest.txt
- README.rdoc
- pre-setup
- NEWS.en.rdoc
- grammar.en.rdoc

### Class/Module Index

Quicksearch

- Racc
- Racc::Accept
- Racc::ActionTable
- Racc::CompileError
- Racc::DebugFlags
- Racc::Error
- Racc::Goto
- Racc::Grammar
- Racc::Grammar::DefinitionEnv
- Racc::Grammar::PrecedenceDefinitionEnv
- Racc::GrammarFileParser
- Racc::GrammarFileParser::Result
- Racc::GrammarFileScanner
- Racc::ISet
- Racc::Item
- Racc::LocationPointer
- Racc::LogFileGenerator
- Racc::OrMark
- Racc::ParseError
- Racc::ParseError
- Racc::Parser
- Racc::ParserClassGenerator
- Racc::ParserFileGenerator
- Racc::ParserFileGenerator::Params
- Racc::Prec
- Racc::RRconflict
- Racc::Reduce
- Racc::Rule
- Racc::SRconflict
- Racc::Shift
- Racc::SourceText
- Racc::State
- Racc::StateTransitionTable
- Racc::StateTransitionTableGenerator
- Racc::States
- Racc::Sym
- Racc::SymbolTable
- Racc::UserAction
- Object
- Object::ParseError
- Array

No matching classes.

# Racc::Grammar

### Attributes

n_expected_srconflicts[RW]
start[R]
symboltable[R]

### Public Class Methods

define(&block) click to toggle source

Dynamic Generation Interface

    # File lib/racc/grammar.rb, line 198
    def Grammar.define(&block)
      env = DefinitionEnv.new
      env.instance_eval(&block)
      env.grammar
    end

new(debug_flags = DebugFlags.new) click to toggle source

    # File lib/racc/grammar.rb, line 23
    def initialize(debug_flags = DebugFlags.new)
      @symboltable = SymbolTable.new
      @debug_symbol = debug_flags.token
      @rules   = []  # :: [Rule]
      @start   = nil
      @n_expected_srconflicts = nil
      @prec_table = []
      @prec_table_closed = false
      @closed = false
      @states = nil
    end

### Public Instance Methods

[](x) click to toggle source

    # File lib/racc/grammar.rb, line 39
    def [](x)
      @rules[x]
    end

add(rule) click to toggle source

Grammar Definition Interface

    # File lib/racc/grammar.rb, line 163
    def add(rule)
      raise ArgumentError, "rule added after the Grammar closed" if @closed
      @rules.push rule
    end

added?(sym) click to toggle source

    # File lib/racc/grammar.rb, line 168
    def added?(sym)
      @rules.detect {|r| r.target == sym }
    end

declare_precedence(assoc, syms) click to toggle source

    # File lib/racc/grammar.rb, line 177
    def declare_precedence(assoc, syms)
      raise CompileError, "precedence table defined twice" if @prec_table_closed
      @prec_table.push [assoc, syms]
    end

dfa() click to toggle source

    # File lib/racc/grammar.rb, line 117
    def dfa
      (@states ||= States.new(self)).dfa
    end

Also aliased as: states
each(&block) click to toggle source
Alias for: each_rule
each_index(&block) click to toggle source

    # File lib/racc/grammar.rb, line 49
    def each_index(&block)
      @rules.each_index(&block)
    end

each_rule(&block) click to toggle source

    # File lib/racc/grammar.rb, line 43
    def each_rule(&block)
      @rules.each(&block)
    end

Also aliased as: each
each_with_index(&block) click to toggle source

    # File lib/racc/grammar.rb, line 53
    def each_with_index(&block)
      @rules.each_with_index(&block)
    end

end_precedence_declaration(reverse) click to toggle source

    # File lib/racc/grammar.rb, line 182
    def end_precedence_declaration(reverse)
      @prec_table_closed = true
      return if @prec_table.empty?
      table = reverse ? @prec_table.reverse : @prec_table
      table.each_with_index do |(assoc, syms), idx|
        syms.each do |sym|
          sym.assoc = assoc
          sym.precedence = idx
        end
      end
    end

init() click to toggle source

Computation

    # File lib/racc/grammar.rb, line 406
    def init
      return if @closed
      @closed = true
      @start ||= @rules.map {|r| r.target }.detect {|sym| not sym.dummy? }
      raise CompileError, 'no rule in input' if @rules.empty?
      add_start_rule
      @rules.freeze
      fix_ident
      compute_hash
      compute_heads
      determine_terminals
      compute_nullable_0
      @symboltable.fix
      compute_locate
      @symboltable.each_nonterminal {|t| compute_expand t }
      compute_nullable
      compute_useless
    end

intern(value, dummy = false) click to toggle source

    # File lib/racc/grammar.rb, line 71
    def intern(value, dummy = false)
      @symboltable.intern(value, dummy)
    end

n_useless_nonterminals() click to toggle source

    # File lib/racc/grammar.rb, line 87
    def n_useless_nonterminals
      @n_useless_nonterminals ||=
          begin
            n = 0
            @symboltable.each_nonterminal do |sym|
              n += 1 if sym.useless?
            end
            n
          end
    end

n_useless_rules() click to toggle source

    # File lib/racc/grammar.rb, line 102
    def n_useless_rules
      @n_useless_rules ||=
          begin
            n = 0
            each do |r|
              n += 1 if r.useless?
            end
            n
          end
    end

nfa() click to toggle source

    # File lib/racc/grammar.rb, line 113
    def nfa
      (@states ||= States.new(self)).nfa
    end

nonterminal_base() click to toggle source

    # File lib/racc/grammar.rb, line 79
    def nonterminal_base
      @symboltable.nt_base
    end

parser_class() click to toggle source

    # File lib/racc/grammar.rb, line 127
    def parser_class
      states = states()   # cache
      if $DEBUG
        srcfilename = caller(1).first.slice(/\A(.*?):/, 1)
        begin
          write_log srcfilename + ".output"
        rescue SystemCallError
        end
        report = lambda {|s| $stderr.puts "racc: #{srcfilename}: #{s}" }
        if states.should_report_srconflict?
          report["#{states.n_srconflicts} shift/reduce conflicts"]
        end
        if states.rrconflict_exist?
          report["#{states.n_rrconflicts} reduce/reduce conflicts"]
        end
        g = states.grammar
        if g.useless_nonterminal_exist?
          report["#{g.n_useless_nonterminals} useless nonterminals"]
        end
        if g.useless_rule_exist?
          report["#{g.n_useless_rules} useless rules"]
        end
      end
      states.state_transition_table.parser_class
    end

size() click to toggle source

    # File lib/racc/grammar.rb, line 57
    def size
      @rules.size
    end

start_symbol=(s) click to toggle source

    # File lib/racc/grammar.rb, line 172
    def start_symbol=(s)
      raise CompileError, "start symbol set twice'" if @start
      @start = s
    end

state_transition_table() click to toggle source

    # File lib/racc/grammar.rb, line 123
    def state_transition_table
      states().state_transition_table
    end

states() click to toggle source
Alias for: dfa
symbols() click to toggle source

    # File lib/racc/grammar.rb, line 75
    def symbols
      @symboltable.symbols
    end

to_s() click to toggle source

    # File lib/racc/grammar.rb, line 61
    def to_s
      "<Racc::Grammar>"
    end

useless_nonterminal_exist?() click to toggle source

    # File lib/racc/grammar.rb, line 83
    def useless_nonterminal_exist?
      n_useless_nonterminals() != 0
    end

useless_rule_exist?() click to toggle source

    # File lib/racc/grammar.rb, line 98
    def useless_rule_exist?
      n_useless_rules() != 0
    end

write_log(path) click to toggle source

    # File lib/racc/grammar.rb, line 153
    def write_log(path)
      File.open(path, 'w') {|f|
        LogFileGenerator.new(states()).output f
      }
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.