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 Programm ing lex: Lexical Analyzer Generator This section describes three means of dealing with different environments: • The use of flags, when only a few rules change from one environment to another • The use of start conditions with rules • The use of multiple lexical analyzers running together In each case, there are rules that recognize the need to change the environment in which the following input text is analyzed and that set some parameter to reflect the change. This may be a flag explicitly tested by the user's action code; such a flag is the simplest way of dealing with the problem, since lex is not involved at all. It may be more convenient, however, to have lex remember the flags as initial conditions on the rules. Any rule may be associated with a start condition. It will only be recognized when lex is in that start condition. The current start condition may be changed at any time. Finally, if the sets of rules for the different environments are very dissimilar, clarity may be best achieved by writing several distinct lexical analyzers and switching from one to another as desired. Consider the following problem: copy the input to the output, changing the word magic to first on every line that began with the letter a, changing magic to second on every line that began with the letter b, and changing magic to third on every line that began with the letter c. All other words and all other lines are left unchanged. These rules are so simple that the easiest way to do this job is with a flag: int flag; %% "a { flag = 'a'; ECHO; } "b { flag = 'b'; ECHO; } "c { flag = 'c'; ECHO; } \n { flag = 0 ; ECHO; } magic { switch (flag) { case 'a': printf("first"); break; case 'b': printf("second"); break; case 'c': printf("third"); break; default: ECHO; break; } } 9-15
lex: Lexical Analyzer Generator XENIX Programming To handle the same problem with start conditions, each start condition must be introduced to lex in the definitions section with a line reading %Start name1 name2 ... where the conditions may be named in any order. The word Start may be abbreviated to s or S. The conditions may be referenced at the head of a rule with angle brackets. For example expression is a rule that is only recognized when lex is in the start condition namel. To enter a start condition, execute the action statement BEGIN name1; which changes the start condition to namel. To return to the initial state BEGIN 0; resets the initial condition of the lex automaton interpreter. A rule may be active in several start conditions; for example is a legal prefix. Any rule not beginning with the < > prefix operator is always active. The same example as before can be written as: %START AA BB CC %% "a { ECHO; BEGIN AA; } "b { ECHO; BEGIN BB; } "c {ECHO; BEGIN CC; } \n { ECHO; BEGIN 0; } magic printf("first"); magic printf("second"); magic printf("third"); where the logic is exactly the same as in the previous method of handling the problem, but lex does the work rather than the user's code. 9-16
- Page 141 and 142: as: Assembler Initial Value Directi
- Page 143 and 144: as: Assembler XENIX Programming int
- 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 194 and 195: XENIX Programming Specifying Source
- Page 196 and 197: XENIX Programming lex: Lexical Anal
- 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
lex: Lexical Analyzer Generator <strong>XENIX</strong> Programming<br />
To handle the same problem with start conditions, each start condition must be<br />
introduced to lex in the definitions section with a line reading<br />
%Start name1 name2 ...<br />
where the conditions may be named in any order. The word Start may be abbreviated to<br />
s or S. The conditions may be referenced at the head of a rule with angle brackets. For<br />
example<br />
expression<br />
is a rule that is only recognized when lex is in the start condition namel. To enter a<br />
start condition, execute the action statement<br />
BEGIN name1;<br />
which changes the start condition to namel. To return to the initial state<br />
BEGIN 0;<br />
resets the initial condition of the lex automaton interpreter. A rule may be active in<br />
several start conditions; for example<br />
<br />
is a legal prefix. Any rule not beginning with the < > prefix operator is always active.<br />
The same example as before can be written as:<br />
%START AA BB CC<br />
%%<br />
"a { ECHO; BEGIN AA; }<br />
"b { ECHO; BEGIN BB; }<br />
"c {ECHO; BEGIN CC; }<br />
\n { ECHO; BEGIN 0; }<br />
magic printf("first");<br />
magic printf("second");<br />
magic printf("third");<br />
where the logic is exactly the same as in the previous method of handling the problem,<br />
but lex does the work rather than the user's code.<br />
9-16