#sedscript to fully parenthesize Fortran expressions #using binary + - * / ** (no unary + or -) # # ( becomes ((( (line 27) # ) becomes ))) (line 27) # # + becomes ))+(( (line 29) # - becomes ))-(( (line 29) # # * becomes )*( (line 36) # / becomes )/( (line 36) # #Finally, add (( and )) to the start and end of the line (line 41). #For example, # # A+B becomes ((A))+((B)) # A+B*C becomes ((A))+((B)*(C)) # A+B*C**D becomes ((A))+((B)*(C**D)) # A**(B+C) becomes ((A**(((B))+((C))))) # #The algorithm adds more parentheses than are strictly necessary. #It works correctly even if the input already has some parentheses. #Triple each of the user's parenthesis to overpower the ones inserted below. #See Handout 8, p. 6 for the ampersand. Why must this substitute command come #before all the others that insert parentheses? s/[()]/&&&/g s/[+-]/))&((/g #Temporarily change ** to @ so that the substitute command in line 36 will not #mistake ** for two multiplication symbols. Use \* to search for an asterisk. s/\*\*/@/g #* has no special meaning within [], so we need no backslash to turn it off. s/[*/]/)&(/g #Now that we're safely past line 36, change exponentiation back to **. s/@/**/g s/.*/((&))/