. .

6.8.

java Automaton, , , 6.5.

 

package converter;

/* . , . nextToken() . nextSym() Lexer. makeSymToken() makeIdnToken() LexTab. Lexer. */

public class Automaton {

public Token tk;

public Lexer lr;

public LexTab lt;

public Buffer bf;

int isym;

char sym;

// state

private char state = 'I';

String s;

public Automaton() {

bf = new Buffer();

lt = new LexTab();

lt.init();

}

// Lexer

public void init(Lexer ll){lr = ll;}

// LexTab

public LexTab getLexTab(){return lt;}

/* , .

. */

public Token nextToken() throws LexBug{

while(true){

if ( (isym = lr.nextSym()) == -1)return Token.Toknull;

sym = (char) isym;

//, .

switch (state) {

case 'I':

if (Character.isLetter(sym)) {

bf.startBuf();

bf.addBuf(sym);

state = 'A';

break;

}

//

else if (sym == '{' || sym == '}' || sym == ':' ||
sym == ';' || sym == ',' || sym == '(' ||
sym == ')' || sym == '=' || sym == '|' ||
sym == '&' || sym == '~') {

tk = lt.makeSymToken(sym);

return tk;

}

else {

System.out.println(sym + " "+ lr.linenum +"
"+ lr.symnum);

throw new LexBug("Automaton.nextToken()
:
");}

case 'A': if(Character.isLetterOrDigit(sym)){

bf.addBuf(sym);

state = 'A';

break;

}

//

else if(sym == ':' || sym == '=' ||
sym == '&' || sym == '|' || sym == ')'

|| sym == ',' || sym == ';' ){

tk = lt.makeIdnToken(bf.getAll());

// .

lr.restore(sym);

state = 'I';

return tk;

}

else { System.out.println(sym + " "+
lr.linenum +" "+ lr.symnum);

throw new LexBug("Automaton.nextToken() :

");

}

default: throw new LexBug("Automaton.nextToken() :
");

}//end switch

 

} // end while

 

Lexer Converter_src.rar. 7.9. , , .

LexTest, . .

 

 

{

bool: a1, b1, c1, dd, ee, ff, x11 ;

a1 = true; b1 = false; c1 = a1; dd = b1;

ee = ~(a1 | ~(b1 & ~( c1 | ~(dd | ee & ff))));

}

 

 

Token: S 1

 

Token: I 4

 

Token: S 5

Token: K 0

 

Token: S 11

 

Token: S 7

Token: S 4

 

Token: K 2

 

Token: S 8

Token: I 3

 

Token: S 3

 

Token: I 5

Token: S 10

 

Token: I 5

 

Token: S 6

Token: I 4

 

Token: S 11

 

Token: S 7

Token: S 10

 

Token: I 3

 

Token: S 8

Token: I 5

 

Token: S 3

 

Token: I 6

Token: S 10

 

Token: I 6

 

Token: S 6

Token: I 6

 

Token: S 11

 

Token: I 7

Token: S 10

 

Token: I 4

 

Token: S 5

Token: I 7

 

Token: S 3

 

Token: I 8

Token: S 10

 

Token: I 7

 

Token: S 9

Token: I 8

 

Token: S 11

 

Token: S 9

Token: S 10

 

Token: S 7

 

Token: S 9

Token: I 9

 

Token: S 8

 

Token: S 9

Token: S 3

 

Token: I 3

 

Token: S 3

Token: I 3

 

Token: S 6

 

Token: S 2

Token: S 11

 

Token: S 7

 

Token: @ 7777777

Token: K 1

 

Token: S 8

 

 

Token: S 3

 

Token: I 4

 

 

 

Lex Table

 

bool

true

false

a1

b1

c1

dd

ee

ff

x11

 

. .