Fix precedences

This commit is contained in:
Glenn Y. Rolland 2017-09-12 17:09:53 +02:00
parent 62005c6087
commit b89add6adf
2 changed files with 11 additions and 9 deletions

View file

@ -2,18 +2,18 @@ module Mm2ep
module Depend
class Lexer < Rly::Lex
token :SPACE, /\s+/
token :L_PAR, /\(/
token :R_PAR, /\)/
token :NUMBER, /[0-9]+(\.[0-9]+)?/
token :STRING, /"[^"]*"/
token :EQ_OP, /\=/
token :T_BOOL, /[tT]rue/
token :F_BOOL, /[fF]alse/
token :T_BOOL, /true/i
token :F_BOOL, /false/i
token :VAR, /[a-z][a-zA-Z0-9_]+/
token :AND_OP, /AND/
token :OR_OP, /OR/
token :NOT_OP, /NOT/
token :SPACE, /\s+/
on_error do |t|
puts "Illegal character #{t.value}"

View file

@ -74,7 +74,7 @@ module Mm2ep
end
def to_s
"(#{@expr1.to_s}) AND (#{@expr2.to_s})"
"( #{@expr1.to_s}) AND (#{@expr2.to_s} )"
end
end
@ -89,7 +89,7 @@ module Mm2ep
end
def to_s
"(#{@expr1.to_s}) OR (#{@expr2.to_s})"
"( #{@expr1.to_s}) OR (#{@expr2.to_s} )"
end
end
@ -103,7 +103,7 @@ module Mm2ep
end
def to_s
"NOT (#{@expr.to_s})"
"NOT ( #{@expr.to_s} )"
end
end
@ -124,8 +124,10 @@ module Mm2ep
class Parser < Rly::Yacc
precedence :left, 'OR_OP', 'EQ_OP'
precedence :left, 'AND_OP', 'NOT_OP'
precedence :left, 'OR_OP'
precedence :left, 'AND_OP'
precedence :left, 'EQ_OP'
precedence :right, :UMINUS
rule 'statement : expr' do |st, e|
st.value = e.value
@ -146,7 +148,7 @@ module Mm2ep
ex.value = BoolValue.new(l.value.to_s)
end
rule 'expr : NOT_OP SPACE expr' do |ex, l, s, e|
rule 'expr : NOT_OP SPACE expr %prec UMINUS' do |ex, l, s, e|
ex.value = NotOp.new(e.value)
end