Skip to content

mattfenwick/Operator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Operator parsing issues

Precedence

  • which operator binds tighter? Is

      1 + 2 * 3
    

    parsed as 1 + (2 * 3) or (1 + 2) * 3? If precedences are different, associativity does not come into play.

  • prefix vs. infix -- assuming = is right-associative, and ! is as well, since prefix operators seem to have to associate to the right, consider:

      ! x = 3
    

    Is that !(x = 3) or (!x) = 3?

Associativity

Only matters when precedences are equal.

  • left vs. right: 1 + 2 + 3 parsed as 1 + (2 + 3) or (1 + 2) + 3?

  • non-associativity: 1 @@ 2 @@ 3 where @@ is a non-associative operator: parse error

  • mixed associativity, same precedence: x = 3 + 1 or 2 + 3 = x, assuming that + and = have the same precedence but opposite associativities. Probably should result in parse error.

  • prefix vs. postfix

     ! x ++
    

    Is it (!x) ++ or !(x++)? (Probably the latter -- but I guess it should be resolved by precedences)

Prefix and Postfix

  • prefix vs. infix: what does each use of - mean?

     3 - - - -- q
    
  • prefix vs. postfix: what does each use of ++ mean?

     ++ x + ++ y * z ++
    
  • associativity of prefix operators: right-only (???)

     ! ! ! ! x
    
  • precedence of prefix vs. postfix: irrelevant, since can't do both on same operand (???) i.e. can't do:

     ++ x ++
    
  • associativity of postfix operators: none, b/c only one allowed? i.e. can't do

     x ++ ++
    

    Counterpoint: C and C++ might support multiple postfix operators.

  • low-precedence postfix: parse this:

     3 + x ++ * 5
    

    as ((3 + x) ++) * 5, assuming ++ is lower precedence than +?

    • (3 + x) ++ doesn't make sense
    • I haven't found any low-precedence postfix operators in practice
    • would the ++ really interfere with *?

Ternary

  • unsolved: how to parse the ternary ? : operator

Error conditions

  • mixed associativity, same precedence

     infixr  @@ 5
     infixl  $$ 5
     prefix  ! 5
     postfix # 5
     
     x @@ y $$ z  -- error
     x $$ y @@ z  -- error
     ! x @@ y     -- error
     x $$ y #     -- error
    
  • postfix operator in non-postfix context

     ++
    
  • non-postfix operator in postfix context

  • non-prefix operator in prefix context (can this be reliably recognized?)

  • missing operand

     x + 
     a * b ^
    

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published