«« ( Date ) »» // «« ( Thread ) »» // ir4pp1 - 2006

Re: pitanje!!

by Jovanovic Ivan
subota, 25. novembar 2006 - 00:11.

java_cup.runtime.Symbol scan() throws java.lang.Exception {
Symbol simbol = getScanner().next_token();
if (simbol!=null)
{
while (simbol.sym==sym.INVALID)
{
System.out.println("LEKSICKA GRESKA: " + simbol.value+" !");
simbol=getScanner().next_token();
}
return (simbol!=null) ? simbol: new Symbol (EOF_sym());
}
else
return new Symbol(EOF_sym());
}
Interesantni primeri ulaznog fajla za leksicki analizator su:
1) Prazan fajl. Jedini simbol u fajlu je EOF.
2) Jedini simboli u fajlu su simbol INVALID i EOF.

Ponasanje metode :
1) Scanner procita EOF. Simbol ima vrednost EOF. Simbol nije null. Simbol nije INVALID. While petlja se zaobilazi. DOlazi se na return. Posto symbol nije null, vraca se symbol.
2) Scanner procita INVALID. Simol ima vrednost INVALID. Simbol nije null. Simbol jeste INVALID I ulazi se u while petlju. Ispisuje se poruka o gresci. Uzima se sledeci symbol. Sledeci symbol je EOF. Simbol nije INVALID I napusta se while petlja. Simbol nije null I vraca se symbol EOF.

Koliko ja vidim simbol u ovoj metodi nikada nije null. Tako da okruzujuca if naredba moze da se ukloni. Return naredba moze da se uprosti tako da uvek vraca simbol.

Evo isecka iz Jlex specifikacije za definisanje vrednosti za EOF.
2.2.17 Specifying the Return Value on End-of-File The %eofval{ ... %eofval} directive specifies the return value on end-of-file. This directive allows the user to write Java code to be copied into the lexical analyzer tokenizing function Yylex.yylex() for execution when the end-of-file is reached. This code must return a value compatible with the type of the tokenizing function Yylex.yylex().
%eofval{
<code>
%eofval}
The specified Java code in <code> determines the return value of Yylex.yylex() when the end-of-file is reached for the input file the lexical analyzer class is processing. This will also be the value returned by Yylex.yylex() each additional time this function is called after end-of-file is initially reached, so <code> may be executed more than once. Finally, the %eofval{ and %eofval} directives should be situated at the beginning of a line.
An example of usage is given below. Suppose the return value desired on end-of-file is (new token(sym.EOF)) rather than the default value null. The user adds the following declaration to the specification file.
%eofval{
return (new token(sym.EOF));
%eofval}
The code is then copied into Yylex.yylex() into the appropriate place.
public Yytoken yylex () { ...
return (new token(sym.EOF));
... }
The value returned by Yylex.yylex() upon end-of-file and from that point onward is now (new token(sym.EOF)).
Kaze se da je default povratna vrednost metode next_tekon kada se dodje do kraja fajla null. Ali upotrebom gore navedene direktive uvek se vraca EOF pa povratna vrednost nikada nije null.

Tako da smatram da je gornje uproscenje metode scan opravdano.


---------------------------------
Everyone is raving about the all-new Yahoo! Mail beta.