How to use terminal method of NonTerminal Package

Best Rr_ruby code snippet using NonTerminal.terminal

parser-en.rb

Source:parser-en.rb Github

copy

Full Screen

1# nonterminal-symbol2class NonTerminal3 attr_reader :name # String4 attr_accessor :rules # Production rules for this symbol. Array of Arrays of Strings or other NonTerminal instances5 def initialize(n)6 @name = n7 @rules = []8 end9 def <<(r)10 @rules << r11 end12 def to_s13 "<" + @name + ">"14 end15 def inspect16 to_s17 end18end19# A parsed nonterminal symbol20class NTermInst21 attr_accessor :nonTerminal # The accompanying NonTerminal instance22 attr_accessor :decision # Index of the production rule applied to get the parsing result ( =index of the child in the decision-tree)23 attr_accessor :children # Instances of the symbols produced after applying the production rule; Array of Strings and more NTermInst-Instances24 def initialize(nt)25 @nonTerminal = nt26 @decision = -127 @children = []28 end29 # Recursive textbased representation (Parser-Tree)30 def to_s(depth = 0)31 ind = " " * depth32 ind + "[#{nonTerminal}/#{decision}" +33 if(children.size == 0)34 "]\n"35 else36 "\n" + children.map { |c| if(c.is_a?(String)) then ind + " " + c.inspect + "\n" else c.to_s(depth+1) end }.join("") + ind + "]\n"37 end38 end39 def inspect40 to_s41 end42 # Search all child elements recursively which are instances of the nonterminal symbol contNT, collect all contained child elements43 # which are of type childclass and return them as an array. childclass can be a ruby-class or a NonTerminal instance.44 # Used to read in recursive grammar-definitions as a flat array45 def collectRecursive(contNT, childclass)46 @children.select { |c|47 (childclass.is_a?(Class) && c.is_a?(childclass)) ||48 (childclass.is_a?(NonTerminal) && c.is_a?(NTermInst) && c.nonTerminal == childclass) } +49 @children.select { |c| c.is_a?(NTermInst) && c.nonTerminal == contNT }.inject([]) { |old,obj| old+obj.collectRecursive(contNT, childclass) }50 end51 # Recursively collect all read in strings ( of Terminal symbols ) and return them as one ruby string52 # Used to get the textual representation of this instance in the parsed string53 def collectString54 @children.map { |c| if(c.is_a?(String)) then c else c.collectString end }.join("")55 end56 def [](ind)57 @children[ind]58 end59end60# Represents a branch in the decision-tree of the non-deterministic stack machine (NPDA). Is managed via a query to run a breadth first search61# on that tree.62class DNode63 attr_accessor :nonTerminal # The nonterminal symbol, whose rules are the subject of the decision of this node in the tree64 attr_accessor :index # Currenty processed decision (index of the NonTerminal#rules Array)65 attr_accessor :strIndex # Index in input string66 attr_accessor :stack # Stack at the point of the decision67 attr_accessor :pnode # Previous(parent) branch68 attr_accessor :pindex # Decision that was made in the parent branch to reach this branch69 def initialize(n, i, s, si, pn)70 @nonTerminal = n71 @index = i72 @stack = s73 @strIndex = si74 @pnode = pn75 if(pn != nil)76 @pindex = pn.index77 end78 end79end80# Represents a grammar as ruby-objects. Hash of names of nonterminal symbols to the corresponding NonTerminal instances81class Grammar < Hash82 attr_accessor :start # Start symbol (initial content of the stack)83 def initialize(nn,&block)84 nn.each { |n| self[n] = NonTerminal.new(n) }85 @start = nil86 if(block != nil) then block.call(self) end87 end88 # Parse string with this grammar. Returns nil in case of non-acceptance, else the root of the parser tree as a DNode-instance else89 def parse(str)90 queue = [] # Queue of DNode-Instances, for the breadth first search in the decision tree91 stack = [start] # Stack of the NPDA92 index = 0 # Position in the input string93# debug "STACK #{stack}\n"94 95 while(!stack.empty? || index != str.length)96 if(!stack.empty? && stack.last.is_a?(String) && (stack.last.length <= str.length-index) && str[index...index+stack.last.length] == stack.last)97 # Accept-away the string (advance)98 top = stack.pop99# debug("Akzeptiere String #{top.inspect}\n")100 index += top.length101 elsif(!stack.empty? && stack.last.is_a?(NonTerminal) && stack.last.rules.size == 1)102 # Apply rule directly, no decision neccessary103 top = stack.pop104# debug "#{top} : Direkt ersetzen -> #{top.rules[0]}\n"105 stack.concat(top.rules[0].reverse)106 else107 if(!stack.empty? && stack.last.is_a?(NonTerminal))108 # Queue decision109 top = stack.pop110# debug "#{top} : Einreihen\n"111 queue.push(DNode.new(top, -1, stack, index, if(queue.empty?) then nil else queue.first end))112 else113 # Dead end - forget stack114 end115 # Apply nex dicision in the queue116 if(queue.empty? || (queue.first.index == queue.first.nonTerminal.rules.length - 1 && queue.size < 2))117 # Can't continue in the current path(branch), no other existing paths => Don't accept string118 return nil119 else120 decide = queue.first121 decide.index = decide.index+1122 # Last decision for this DNode-object; take the next from the queue123 if(decide.index == decide.nonTerminal.rules.length)124# debug("#{queue.first.nonTerminal} : Dead end go to #{queue[1].nonTerminal}/#{queue[1].index+1}")125 queue.delete_at(0)126 decide = queue.first127 decide.index = decide.index+1128 129# debug("#{decide.nonTerminal}: Last Decision")130 else131 # Next decision for this branch132# debug "#{decide.nonTerminal}: Decide #{decide.index}"133 end134 # Use the stack of this branch135 stack = decide.stack.clone136# debug " ++ #{decide.nonTerminal.rules[decide.index]}\n"137 # Decided -> Apply production rule (Fill stack)138 stack.concat(decide.nonTerminal.rules[decide.index].reverse)139 # Jump to the position of this branch in the input string140 index = decide.strIndex141 end142 end143# debug "STACK #{stack}\n"144 end145 # Backtrace what decision was applied when, to generate array of indices of decisions (backwards)146 trace = []147 if(!queue.empty?)148 d = queue.first149 trace << d.index150 while(d.pnode != nil)151 trace << d.pindex152 d = d.pnode153 end154 end155 156 # Generate parser tree157 root = NTermInst.new(start)158 stack = [root]159 while(!stack.empty?)160 top = stack.pop()161 # What decision was done here162 top.decision = if(top.nonTerminal.rules.size>1) then trace.pop() else 0 end163 164 s = top.nonTerminal.rules[top.decision].size165 top.children = Array.new(s)166 167 # For all symbols on the right side of this production rule...168 (s-1).downto(0) { |i|169 sym = top.nonTerminal.rules[top.decision][i]170 top.children[i] = if(sym.is_a?(String))171 # String ( = Sequence of terminal symbols) => Put into parser tree172 sym173 else174 # Non terminal symbols => to stack175 x = NTermInst.new(sym)176 stack.push(x)177 x178 end179 }180 end181 182# debug "Accepting\n"183 root # Parserbaum zurückgeben184 end185 # Return textual BNF of this grammar186 def to_s187 map { |k,v|188 v.to_s + " ::= " + v.rules.map { |r| r.map{|e| e.inspect}.join(" ") }.join(" | ")189 }.join("\n")190 end191 def inspect192 to_s193 end194# def debug(str)195# $stdout.write(str)196# end197 # Returns the Grammar of the modified BNF, which allows parsing arbitrary special chars198 def Grammar.BNF199 @@bnf ||= Grammar.new(["BNF", "BNF2", "Def", "Rules", "Rules2", "Rule", "Symbol", "String", "Chars", "Char", "NTerm", "Name", "Name2", "SpaceE", "Space", "Alpha", "Alnum"]) { |g|200 g["BNF"] << [g["Def"], g["BNF2"]]201 g["BNF2"].rules = [["\n", g["Def"], g["BNF2"]], [], ["\n", g["BNF2"]]]202 g["Def"] << [g["SpaceE"], g["NTerm"], g["SpaceE"], "::=", g["Rules"]]203 g["Rules"] << [g["Rule"], g["Rules2"]]204 g["Rules2"].rules= [["|", g["Rule"], g["Rules2"]], []]205 g["Rule"] << [g["SpaceE"]]206 g["Rule"] << [g["SpaceE"], g["Symbol"], g["Rule"]]207 g["Symbol"].rules = [[g["String"]], [g["NTerm"]]]208 g["String"] << ["\"", g["Chars"], "\""]209 g["Chars"] << []210 g["Chars"] << [g["Char"], g["Chars"]]211 for i in 0..255 do212 g["Char"] << [[i].pack("C").inspect[1..-2]]213 end214 g["NTerm"] << ["<", g["Name"], ">"]215 g["Name"] << [g["Alpha"]]216 g["Name"] << [g["Alpha"], g["Name2"]]217 g["Name2"] << [g["Alnum"], g["Name2"]]218 g["Name2"] << []219 g["SpaceE"] << [g["Space"]]220 g["SpaceE"] << []221 g["Space"] << [" ", g["SpaceE"]]222 g["Space"] << ["\t", g["SpaceE"]]223 g["Space"] << ["\r", g["SpaceE"]]224 for i in 0..25 do225 a = [[[65 + i].pack("C")], [[97 + i].pack("C")]]226 g["Alpha"].rules.concat(a)227 g["Alnum"].rules.concat(a)228 end229 for i in 0..9 do230 g["Alnum"] << [[48 + i].pack("C")]231 end232 g.start = g["BNF"]233 }234 end235 # Parse given grammar in BNF and return as instance of 'Grammar'. Uses the grammar for BNF.236 def Grammar.fromBNF(str)237 b = Grammar.BNF238 s = nil239 defs = b.parse(str).collectRecursive(b["BNF2"], b["Def"])240 g = Grammar.new(defs.map { |df| df[1][1].collectString })241 defs.each { |df|242# puts "foobar"243 name = df[1][1].collectString244 nt = g[name]245 if(s == nil) then s = nt end # Use the 1st defined Nonterminal symbol as start symbol246 nt.rules = df[4].collectRecursive(b["Rules2"], b["Rule"]).map { |rule|247 rule.collectRecursive(b["Rule"], b["Symbol"]).map { |sym|248 if(sym.decision == 0)249 eval(sym[0].collectString)250 else251 g[sym[0][1].collectString]252 end253 }254 }255 }256 g.start = s257 g258 end259end...

Full Screen

Full Screen

nullable.rb

Source:nullable.rb Github

copy

Full Screen

1require 'core/semantics/factories/combinators'2require 'core/semantics/factories/obj-fold'3class Grammar4 attr_reader :start, :rules5 def initialize(start, rules)6 @start = start7 @rules = rules8 end9end10class Rule11 attr_reader :name, :alts12 def initialize(name, alts)13 @name = name14 @alts = alts15 end16end17class Alt18 attr_reader :elts19 def initialize(elts)20 @elts = elts21 end22end23class Sym24end25class NonTerminal < Sym26 attr_reader :name27 def initialize(name)28 super()29 @name = name30 end31end32class Terminal < Sym33 attr_reader :value34 def initialize(value)35 super()36 @value = value37 end38end39class Nullable40 include Factory41 def FindRule(sup)42 Class.new(sup) do 43 def find_rule(name)44 @@grammar.rules.find_first {|r| r.name == name}45 end46 end47 end48 49 def Grammar(sup)50 Class.new(FindRule(sup)) do51 def nullable?(tbl)52 @@grammar = self53 find_rule(start.name).nullable?(tbl)54 end55 end56 end57 58 def Rule(sup)59 Class.new(sup) do60 def nullable?(tbl)61 alts.inject(false) { |cur, alt| cur | alt.nullable?(tbl) }62 end63 end64 end65 def Alt(sup)66 Class.new(sup) do67 def nullable?(tbl)68 elts.inject(true){ |cur, elt| cur & elt.nullable?(tbl) }69 end70 end71 end72 def Terminal(sup)73 Class.new(sup) do 74 def nullable?(tbl)75 false76 end77 end78 end79 def NonTerminal(sup)80 Class.new(FindRule(sup)) do81 def nullable?(tbl)82 x = find_rule(name).nullable?(tbl)83 tbl[name] = x84 end85 end86 end87end88 89 90Arith = Grammar.new(NonTerminal.new(:Stat), 91 [92 Rule.new(:Exp, [93 Alt.new([Terminal.new(:Var)]),94 Alt.new([NonTerminal.new(:Exp),95 Terminal.new("+"),96 NonTerminal.new(:Exp)])]),97 Rule.new(:Stat, [Alt.new([NonTerminal.new(:Exp),98 Terminal.new(";")]),99 Alt.new([Terminal.new("{"),100 NonTerminal.new(:Stats),101 Terminal.new("}")])]),102 Rule.new(:Stats, [Alt.new([]),103 Alt.new([NonTerminal.new(:Stat),104 NonTerminal.new(:Stats)])])105 ])106#FixNullable = RExtend.new(Fixpoint.new(:nullable?, true), Nullable.new, 107# {:NonTerminal => :Node})108#FixNullable = Extend.new(Rename.new(Fixpoint.new(:nullable?, true),109# {:NonTerminal => :Node}),110# Nullable.new)111FixNullable = Extend.new(Restrict.new(Circular.new({:nullable? => false}),112 [:NonTerminal]), Nullable.new)113ArithNullableFix = FFold.new(FixNullable).fold(Arith)114tbl = {}115puts ArithNullableFix.nullable?(tbl)116p tbl ...

Full Screen

Full Screen

visitor.rb

Source:visitor.rb Github

copy

Full Screen

...17 ret.push(i.accept(self))18 end19 ret20 end21 def Visitor.define_visit_Nonterminal(element_type)22 eval <<-END_OF_EVAL23 def visit_#{element_type.id2name}(element)24 apply_to_#{element_type.id2name}(element, visit_children(element))25 end26 END_OF_EVAL27 end28 def Visitor.define_visit_Terminal(element_type)29 eval <<-END_OF_EVAL30 def visit_#{element_type.id2name}(element)31 apply_to_#{element_type.id2name}(element)32 end33 END_OF_EVAL34 end35 define_visit_Terminal(:Include)36 define_visit_Terminal(:Verbatim)37 define_visit_Terminal(:MethodListItemTerm)38 define_visit_Nonterminal(:DocumentElement)39 define_visit_Nonterminal(:Headline)40 define_visit_Nonterminal(:TextBlock)41 define_visit_Nonterminal(:ItemList)42 define_visit_Nonterminal(:EnumList)43 define_visit_Nonterminal(:DescList)44 define_visit_Nonterminal(:MethodList)45 define_visit_Nonterminal(:ItemListItem)46 define_visit_Nonterminal(:EnumListItem)47 48 def visit_DescListItem(element)49 term = element.term.accept(self)50 apply_to_DescListItem(element, term, visit_children(element))51 end52 define_visit_Nonterminal(:DescListItemTerm)53 def visit_MethodListItem(element)54 term = element.term.accept(self)55 apply_to_MethodListItem(element, term, visit_children(element))56 end57 define_visit_Terminal(:StringElement)58 define_visit_Terminal(:Verb)59 define_visit_Nonterminal(:Emphasis)60 define_visit_Nonterminal(:Code)61 define_visit_Nonterminal(:Var)62 define_visit_Nonterminal(:Keyboard)63 define_visit_Nonterminal(:Index)64 define_visit_Nonterminal(:Footnote)65 def visit_Reference(element)66 children = visit_children(element)67 begin68 element.result_of_apply_method_of(self, children)69 rescue NameError70 apply_to_Reference(element, children)71 end72 end73 end 74end ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Rr_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful