Download presentation
Presentation is loading. Please wait.
Published bySydni Bunt Modified over 9 years ago
1
Printf transformation in TXL Mariano Ceccato ITC-Irst Istituto per la ricerca Scientifica e Tecnologica ceccato@itc.it
2
The Problem printf is a c construct to print formatted output java does not support a similar syntax every printf should be converted to the appropriate java output command (automatically?) printf("Vertices:\tV = %d\n", V); System.out.print("Vertices:\tV =" + V + "\n");
3
Where the differences printf uses a formatting language this language is not parsed by TXL a stringlit is a token, not a tree: we can not uses construct/deconstruct on it we have to modify the string by hand [expression_statement] "Vertices:\tV = %d\n"Vprintf);(
4
printf syntax Symbolmeaning -left adjustment numberminimum field width.separation numberprecision h, lint as a short or long Conversion character Result d, idecimal number ounsigned octal x, Xunsigned hex uunsigned decimal csingle character schar * fdouble [-]m.dddddd e, E double [-]m.dddddd e xx g, Gwith no trailing zeros pvoid * % %
5
Where the problems Behavior is type-dependent precision holds different meaning in case of string, floating point or integer Between % and conversion character there is an (optional) variable size formatter command
6
Solution Solution: resolve a restricted problem “%d” formatter only not apply to any other formatter command substitute the formatter command with the appropriate value
7
Index operator N1 [index T1 T2] T1 and T2 are [stringlit] N1 is [number] it returns the index of the first instance of the text T2 in T1 it returns zero if none found N1 [index "hello, hello world" "lo"] returns 4 N1 [index "hello, hello world" "lo w"] returns 11 N1 [index "hello, hello world" "%d"] returns 0 (not found)
8
String manipulation T1 [: N1 N2] T1 is [stringlit] N1 and N2 are [number] it returns the sub-string of T1 from char N1 to N2 inclusive, 1-origin T1 = "hello, hello world" T1 [: 1 5] returns "hello" T1 [: 14 18] returns "world"
9
String length N1 [# T1] T1 is [stringlit] N1 is [number] it returns the length of T1 T1 = "hello, hello world" N1 [# T1] returns 18
10
Breaking the string construct position [number] _ [index the_string "%d"] [- 1] construct first_string [stringlit] the_string [: 1 position] construct L [number] _ [# the_string] construct rest [number] position [+ 2] [+ 1] construct second_string [stringlit] the_string[: rest L] printf(" Vertices:\tV = %d \n", V); first(1)1619last(21)
11
Building the new statement replace [expression_statement] 'printf '( the_string [stringlit]', values [list assignment_expression] ') ';... deconstruct values first_argument [assignment_expression] ', rest_argument [list assignment_expression] construct final_expression [binary_expression] first_string '+ '( first_argument ') '+ second_string construct result [expression_statement] 'System '. 'out '. 'print '( final_expression ') '; System.out.print(" Vertices:\tV =" + ( V ) + " \n");
12
Extension 1 In case of multiple “%d” call a recursive function on second_string The recursion ends when the parameter list is empty printf("BoundingBox: %d, %d, %d, %d\n", xmin, ymin, xmax, ymax); System.out.print ("BoundingBox: " + (xmin) + ", " + (ymin) + ", " + (xmax) + ", " + (ymax) + "\n");
13
Extension 2 Take a copy of the function and modify it to catch the case of standard error output fprintf(stderr, "...", list); System.err.print ("..."); replace [expression_statement] 'fprintf '( 'stderr the_string [stringlit]', values [list assignment_expression] ') ';... construct result [expression_statement] 'System '. 'err '. 'print '( final_expression ') ';
14
Summarizing TXL does not parse stringlit into a tree Focus on a simpler sub-problem (this solves the 71% of the occurrences) Use #, : and index string operators Break the string in two segments Construct a new expression using the sub-string and the first parameter Apply the transformation recursively to the second sub-string Copy and modify the rule to solve stderr
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.