Intel XENIX 286 Programmers Guide (86) - Tenox.tc
Intel XENIX 286 Programmers Guide (86) - Tenox.tc Intel XENIX 286 Programmers Guide (86) - Tenox.tc
XENIX Programming lex: Lexical Analyzer Generator As a trivial problem, consider copying an input file while adding 3 to every positive number divisible by 7. Here is a suitable lex source program to do just that: %% [0-9] + int k; { k = atoi(yytext); if (k% 7 = = 0) printf("%d", k+3); else printf("%d",k); } The rule [0-9]+ recognizes strings of digits; atoi converts the digits to binary and stores the result in k. The remainder operator {%) is used to check whether k is divisible by 7; if it is, it is incremented by 3 as it is written out. Note that this program will alter such input items as 49.63 or X7 and will also increment the absolute value of all negative numbers divisible by 7. To avoid this, just add a few more rules after the active one, as here: %% -?[0-9] + int k; { k = atoi(yytext); printf("%d", k% 7 = = 0 ? k + 3 k); } -?[0-9.] + ECHO; [A-Za-z][A-Za-z0-9] + ECHO; Numerical strings containing a decimal point or preceded by a letter will be picked up by one of the last two rules and not changed. The if-else has been replaced by a C conditional expression to save space; the form a?b:c means "if a then b else c". For an example of statistics gathering, here is a program that makes histograms of word lengths, where a word is defined as a string of letters: %% [a-z] + \n %% yywrap() { int i; int lengs[100]; l engs[yyl eng] + +; I printf("Length No. words\n"); for(i = 0; i < 1 00; i + +) if (lengs[i] > 0) printf(" % Sd% 1 Od\n" ,i,l engs[i]); return( 1 ); } 9-19
lex: Lexical Analyzer Generator XENIX Programming This program accumulates the histogram, while producing no output. At the end of the input it prints the table. The final statement return(l); indicates that lex is to perform wrapup. If yywrap returns zero (false) it implies that further input is available and the program is to continue reading and processing. To provide a yywrap that never returns true causes an infinite loop. As a larger example, here are some parts of a program written to convert double precision FORTRAN to single precision FORTRAN. Because FORTRAN does not distinguish between upper- and lowercase letters, this routine begins by defining a set of classes including both cases of each letter: a [aA] b [bB] c [cC] z [zZ] An additional class recognizes white space: w [ \t]* The first rule changes double precision to real, or DOUBLE PRECISION to REAL: {d} {o} {u} {b} {I} {e} {W} {p} {r} {e} {c} {i} {s} {i } {o} {n} { printf(yytext[O] = = 'd'? "real" : "REAL"); } Care is taken throughout this program to preserve the case of the original program. The conditional operator is used to select the proper form of the keyword. The next rule copies continuation card indications to avoid confusing them with constants: "[" 0] ECHO; In the regular expression, the quotes surround the blanks. It is interpreted as beginning of line, then five blanks, then anything but blank or zero. Note the two different meanings of the caret (") here. Some rules then follow to change double precision constants to ordinary floating constants: 9-20 [0-9] + {W} {d} {W} [ + -] ? {W} [0-9] + I [0-9] + {W}" . " {W} {d} {W} [ + -] ? {W} [0-9] + I "."{W} [0-9] + {W} {d} {W} [ + -] ? {W} [0-9] + { I* convert constants * I for(p = yytext; *p ! = 0; p + +) { if (*p = = 'd' II *p = = 'D') *p + = 'e'-'d'; ECHO; }
- Page 145 and 146: as: A sse m bier sub subb test test
- Page 147 and 148: as: Assembler XENIX Programming lnt
- Page 149 and 150: as: Assembler XENIX Programming lnt
- Page 151 and 152: as: Assembler XENIX Programming Imm
- Page 153 and 154: as: A sse m bier XENIX Programming
- Page 156 and 157: CHAPTER 8 csh : C SHEll The C shell
- Page 158 and 159: XENIX Programming csh: C Shell Some
- Page 160 and 161: XENIX Programm ing *w 32 * q % !c -
- Page 162 and 163: XENIX Programming csh: C Shell the
- Page 164 and 165: XENIX Programming csh: C Shell Usin
- Page 166 and 167: XENIX Programming csh: C Shell Usin
- Page 168 and 169: XENIX Programming csh: C Shell The
- Page 170 and 171: XENIX Programming csh: C Shell Note
- Page 172 and 173: XENIX Programming switch ( string c
- Page 174 and 175: XENIX Programming csh: C Shell Star
- Page 176 and 177: XENIX Programming csh: C Shell Spec
- Page 178 and 179: CHAPTER 9 lex : LEXICAL ANA LYZER G
- Page 180 and 181: XENIX Programming lex: Lexical Anal
- Page 182 and 183: XENIX Programming lex: Lexical Anal
- Page 184 and 185: XENIX Programming lex: Lexical Anal
- Page 186 and 187: XENIX Programming lex: Lexical Anal
- Page 188 and 189: XENIX Programming lex: Lexical Anal
- Page 190 and 191: XENIX Programming lex: Lexical Anal
- Page 192 and 193: XENIX Programm ing lex: Lexical Ana
- Page 194 and 195: XENIX Programming Specifying Source
- Page 198 and 199: XENIX Programming lex: Lexical Anal
- Page 200 and 201: XENIX Programming The definitions s
- Page 202 and 203: CHAPTER 10 yacc: COMPILER-COMPILER
- Page 204 and 205: XENIX Programming yacc: Compiler-Co
- Page 206 and 207: XENIX Programming yacc: Compiler-Co
- Page 208 and 209: XENIX Programming yacc: Compiler-Co
- Page 210 and 211: XENIX Programming yacc: Compiler-Co
- Page 212 and 213: XENIX Programming yacc: Compiler-Co
- Page 214 and 215: XENIX Programming yacc: Compiler-Co
- Page 216 and 217: XENIX Programming yacc: Compiler-Co
- Page 218 and 219: XENIX Programming yacc: Compiler-Co
- Page 220 and 221: XENIX Programming yacc: Compiler-Co
- Page 222 and 223: XENIX Programming yacc: Compiler-Co
- Page 224 and 225: XENIX Programming yacc: Compiler-Co
- Page 226 and 227: XENIX Programming yacc: Compiler-Co
- Page 228 and 229: XENIX Programming yacc: Compiler-Co
- Page 230 and 231: XENIX Programming yacc: Compiler-Co
- Page 232 and 233: XENIX Programming yacc: Compiler-Co
- Page 234 and 235: XENIX Programming yacc: Compiler-Co
- Page 236 and 237: XENIX Programming %left %left %left
- Page 238 and 239: XENIX Programming I* lexical analys
- Page 240: XENIX Programming yacc: Compiler-Co
- Page 243 and 244: m4: Macro Processor XENIX Programmi
- Page 245 and 246: m4: Macro Processor XENIX Programmi
lex: Lexical Analyzer Generator <strong>XENIX</strong> Programming<br />
This program accumulates the histogram, while producing no output. At the end of the<br />
input it prints the table. The final statement return(l); indicates that lex is to perform<br />
wrapup. If yywrap returns zero (false) it implies that further input is available and the<br />
program is to continue reading and processing. To provide a yywrap that never returns<br />
true causes an infinite loop.<br />
As a larger example, here are some parts of a program written to convert double<br />
precision FORTRAN to single precision FORTRAN. Because FORTRAN does not<br />
distinguish between upper- and lowercase letters, this routine begins by defining a set of<br />
classes including both cases of each letter:<br />
a [aA]<br />
b [bB]<br />
c [cC]<br />
z [zZ]<br />
An additional class recognizes white space:<br />
w [ \t]*<br />
The first rule changes double precision to real, or DOUBLE PRECISION to REAL:<br />
{d} {o} {u} {b} {I} {e} {W} {p} {r} {e} {c} {i} {s} {i } {o} {n} {<br />
printf(yytext[O] = = 'd'? "real" : "REAL");<br />
}<br />
Care is taken throughout this program to preserve the case of the original program. The<br />
conditional operator is used to select the proper form of the keyword. The next rule<br />
copies continuation card indications to avoid confusing them with constants:<br />
"[" 0] ECHO;<br />
In the regular expression, the quotes surround the blanks. It is interpreted as beginning<br />
of line, then five blanks, then anything but blank or zero. Note the two different<br />
meanings of the caret (") here. Some rules then follow to change double precision<br />
constants to ordinary floating constants:<br />
9-20<br />
[0-9] + {W} {d} {W} [ + -] ? {W} [0-9] + I<br />
[0-9] + {W}" . " {W} {d} {W} [ + -] ? {W} [0-9] + I<br />
"."{W} [0-9] + {W} {d} {W} [ + -] ? {W} [0-9] + {<br />
I* convert constants * I<br />
for(p = yytext; *p ! = 0; p + +)<br />
{<br />
if (*p = = 'd' II *p = = 'D')<br />
*p + = 'e'-'d';<br />
ECHO;<br />
}