Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expr2 rule node #98

Merged
merged 6 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/HerbGrammar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export
clearconstraints!,
addconstraint!,
merge_grammars!,
expr2rulenode,

@pcfgrammar,

Expand Down
153 changes: 153 additions & 0 deletions src/rulenode_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,159 @@
end


"""
expr2ulenode(expr::Expr, grammar::AbstractGrammar)

Converts an expression into a [`AbstractRuleNode`](@ref) corresponding to the rule definitions in the grammar.
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be directly before the definition of expr2rulenode(...). The signature in the docstring is missing an 'r', btw.


function grammar_map_right_to_left(grammar::AbstractGrammar)
tags = Dict{Any,Any}()
for (l, r) in zip(grammar.types, grammar.rules)
tags[r] = l
end
return tags
end

function _expr2rulenode(expr::Expr, grammar::AbstractGrammar, tags::Dict{Any,Any})
if expr.head == :call

if !haskey(tags, expr)

parameters = [_expr2rulenode(expr.args[i], grammar, tags) for i in (2:length(expr.args))]
pl = map( x -> x[1], parameters)
pr = map( x -> x[2], parameters)

temp = [expr.args[1] ;pl]
newexpr = Expr(:call, temp...)
rule = findfirst(==(newexpr), grammar.rules)


oldpl = copy(pl)
oldpr = copy(pr)
pnr = length(pl)

while isnothing(rule)

updatedrule = findfirst(==(pl[pnr]), grammar.rules)

if isnothing(updatedrule)
pl[pnr] = oldpl[pnr]
pr[pnr] = oldpr[pnr]
pnr = pnr - 1
continue
end

pl[pnr] = tags[pl[pnr]]
pr[pnr] = RuleNode(updatedrule, [pr[pnr]])

temp = [expr.args[1] ;pl]
newexpr = Expr(:call, temp...)
rule = findfirst(==(newexpr), grammar.rules)

pnr = length(pl)
end
return (tags[newexpr], RuleNode(rule, pr))
else
rule = findfirst(==(expr), grammar.rules)
return (tags[expr], RuleNode(rule, []))
end
elseif expr.head == :block
(l1, r1) = _expr2rulenode( expr.args[1], grammar, tags)
(l2, r2) = _expr2rulenode( expr.args[3], grammar, tags)

temp = (l1, l2)

newexpr = Expr(:block, temp...)
rule = findfirst(==(newexpr), grammar.rules)

pl = [l1, l2]
pr = [r1, r2]

oldpl = copy(pl)
oldpr = copy(pr)
pnr = length(pl)

while isnothing(rule)

updatedrule = findfirst(==(pl[pnr]), grammar.rules)

if isnothing(updatedrule)
pl[pnr] = oldpl[pnr]
pr[pnr] = oldpr[pnr]
pnr = pnr - 1
continue
end

pl[pnr] = tags[pl[pnr]]
pr[pnr] = RuleNode(updatedrule, [pr[pnr]])

temp = (pl[1], pl[2])
newexpr = Expr(:block, temp...)
rule = findfirst(==(newexpr), grammar.rules)

pnr = length(pl)
end
return (tags[newexpr], RuleNode(rule, pr))

elseif expr.head == :quote
return _expr2rulenode(expr.args[1], grammar, tags)

Check warning on line 310 in src/rulenode_operators.jl

View check run for this annotation

Codecov / codecov/patch

src/rulenode_operators.jl#L309-L310

Added lines #L309 - L310 were not covered by tests
else
error("Only call and block expressions are supported")

Check warning on line 312 in src/rulenode_operators.jl

View check run for this annotation

Codecov / codecov/patch

src/rulenode_operators.jl#L312

Added line #L312 was not covered by tests
end
end

function _expr2rulenode(expr::Any, grammar::AbstractGrammar, tags::Dict{Any,Any})
rule = findfirst(==(expr), grammar.rules)
return (tags[expr], RuleNode(rule, []))
end

function expr2rulenode(expr::Expr, grammar::AbstractGrammar, startSymbol::Symbol)
tags = grammar_map_right_to_left(grammar)
(s, rn) = _expr2rulenode(expr, grammar, tags)
while s != startSymbol

updatedrule = findfirst(==(s), grammar.rules)

if isnothing(updatedrule)
error("INVALID STARTING SYMBOL")

Check warning on line 329 in src/rulenode_operators.jl

View check run for this annotation

Codecov / codecov/patch

src/rulenode_operators.jl#L329

Added line #L329 was not covered by tests
end

s = tags[s]
rn = RuleNode(updatedrule, [rn])
end
return rn
end

function expr2rulenode(expr::Expr, grammar::AbstractGrammar)
tags = grammar_map_right_to_left(grammar)
(s, rn) = _expr2rulenode(expr, grammar, tags)
return rn
end

function expr2rulenode(expr::Symbol, grammar::AbstractGrammar, startSymbol::Symbol)
tags = get_tags(grammar)
(s, rn) = expr2rulenode(expr, grammar, tags)
while s != startSymbol

Check warning on line 347 in src/rulenode_operators.jl

View check run for this annotation

Codecov / codecov/patch

src/rulenode_operators.jl#L344-L347

Added lines #L344 - L347 were not covered by tests

updatedrule = findfirst(==(s), grammar.rules)

Check warning on line 349 in src/rulenode_operators.jl

View check run for this annotation

Codecov / codecov/patch

src/rulenode_operators.jl#L349

Added line #L349 was not covered by tests

if isnothing(updatedrule)
error("INVALID STARTING SYMBOL")

Check warning on line 352 in src/rulenode_operators.jl

View check run for this annotation

Codecov / codecov/patch

src/rulenode_operators.jl#L351-L352

Added lines #L351 - L352 were not covered by tests
end

s = tags[s]
rn = RuleNode(updatedrule, [rn])
end
return rn

Check warning on line 358 in src/rulenode_operators.jl

View check run for this annotation

Codecov / codecov/patch

src/rulenode_operators.jl#L355-L358

Added lines #L355 - L358 were not covered by tests
end

function expr2rulenode(expr::Symbol, grammar::AbstractGrammar)
tags = get_tags(grammar)
(s, rn) = expr2rulenode(expr, grammar, tags)
return rn

Check warning on line 364 in src/rulenode_operators.jl

View check run for this annotation

Codecov / codecov/patch

src/rulenode_operators.jl#L361-L364

Added lines #L361 - L364 were not covered by tests
end

"""
Calculates the log probability associated with a rulenode in a probabilistic grammar.
"""
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ using HerbCore
using HerbGrammar
using Test


@testset "HerbGrammar.jl" verbose=true begin
include("test_csg.jl")
include("test_rulenode_operators.jl")
include("test_rulenode2expr.jl")
include("test_expr2rulenode.jl")
end
47 changes: 47 additions & 0 deletions test/test_expr2rulenode.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
@testset verbose=true "expr2rulenode" begin

g1 = @cfgrammar begin
Number = |(1:2)
Number = x
Number = Number + Number
Number = Number * Number
Number = DiffNumber
DiffNumber = |(3:4)
end

expr1 = :(1 + 2)
expr2 = :((x * (1 + 3)) + (4 * x))
@test expr2rulenode(expr1, g1) == RuleNode(4, [RuleNode(1, []), RuleNode(2, [])])
@test expr2rulenode(expr2, g1) == RuleNode(4, [ RuleNode(5, [RuleNode(3, []), RuleNode(4, [RuleNode(1, []), RuleNode(6,[RuleNode(7, [])])])]), RuleNode(5,[RuleNode(6, [RuleNode(8, [])]), RuleNode(3, [])])])


g2 = @csgrammar begin
Start = Sequence #1

Sequence = Operation #2
Sequence = (Operation; Sequence) #3
Operation = Transformation #4
Operation = ControlStatement #5

Transformation = moveRight() | moveDown() | moveLeft() | moveUp() | drop() | grab() #6
ControlStatement = IF(Condition, Sequence, Sequence) #12
ControlStatement = WHILE(Condition, Sequence) #13

Condition = atTop() | atBottom() | atLeft() | atRight() | notAtTop() | notAtBottom() | notAtLeft() | notAtRight() #14
end

expr3 = :(moveUp())
expr4 = :(moveUp(); (moveRight()))
expr5 = :(IF(atTop(), ((moveUp(); (moveRight()))), moveRight()))

@test expr2rulenode(expr3, g2) == RuleNode(9, [])
@test expr2rulenode(expr3, g2, :Start) == RuleNode(1, [RuleNode(2, [RuleNode(4, [RuleNode(9, [])])])])

@test expr2rulenode(expr4, g2) == RuleNode(3, [RuleNode(4, [RuleNode(9, [])]) , RuleNode(2, [RuleNode(4, [RuleNode(6, [])])])])
@test expr2rulenode(expr4, g2, :Start) == RuleNode(1, [RuleNode(3, [RuleNode(4, [RuleNode(9, [])]) , RuleNode(2, [RuleNode(4, [RuleNode(6, [])])])])])

@test expr2rulenode(expr5, g2) == RuleNode(12, [RuleNode(14, []), RuleNode(3, [RuleNode(4, [RuleNode(9, [])]) , RuleNode(2, [RuleNode(4, [RuleNode(6, [])])])]), RuleNode(2, [RuleNode(4, [RuleNode(6, [])])])])
@test expr2rulenode(expr5, g2, :Start) == RuleNode(1, [RuleNode(2, [RuleNode(5, [RuleNode(12, [RuleNode(14, []), RuleNode(3, [RuleNode(4, [RuleNode(9, [])]) , RuleNode(2, [RuleNode(4, [RuleNode(6, [])])])]), RuleNode(2, [RuleNode(4, [RuleNode(6, [])])])])])])])

end

Loading