Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

JavaCC i implementiranje petlje

[es] :: Art of Programming :: JavaCC i implementiranje petlje

[ Pregleda: 2303 | Odgovora: 1 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

masetrt
Marko Djurovic
Programer, Omni-Explorer
Beograd

Član broj: 3129
Poruke: 228
*.dynamic.sbb.co.yu.

Sajt: www.vast.com


+2 Profil

icon JavaCC i implementiranje petlje08.10.2007. u 22:46 - pre 201 meseci
Ukratko da li je neko implementirao petlje putem JavaCC. Pa ako neko jeste mali primer bez obzira na sintaksu :) bio bi od pomoci
His majesty Grand Duke of Shumadija and Western Pomoravlje
 
Odgovor na temu

masetrt
Marko Djurovic
Programer, Omni-Explorer
Beograd

Član broj: 3129
Poruke: 228
*.dynamic.sbb.co.yu.

Sajt: www.vast.com


+2 Profil

icon Re: JavaCC i implementiranje petlje11.10.2007. u 18:15 - pre 201 meseci
Pa ako nekog zanima kako je problem resen evo: Svaki token ima dve akcije u zavisnosti od moda Jedna je prava akcija dok je druga generisanje java koda. E sad kada naidjem na petlju ukljucuje se mod za generisanje koda. Zatim se od generisanog koda generise nova klasa (ciji interface naravno vec postoji) i pomocu tools.jar (koji stize uz JDK) se ta klasa dinamicki ucitava, kreira se instanca klase i ispaljuje odgovarajuca metoda. :D. Naravno ovo je java depended. Medjutim u medjuvremenu mi je stiglo jos jedno resenje (mnogo elegantnije) od gospodina Theodora Norvell-a koriscenjem JTree-a:

Citat:
I rather doubt that the FAQ says that. I think it suggests that you
build a tree and
interpret the tree, rather than trying to back up the parser. JJTree is
one way
to build a tree, but not the only one. Also you can use structures
other than
trees. For example you can generate machine code and interpret that.

I have a script language implementation that uses the tree approach. I
can send that to
you if you like.

Here is an outline of how it works. (It's not very OO.)
First in the JJT parser we parse while loops like this:

options {
NODE_DEFAULT_VOID = true ; <--- This is important
STATIC = false ;
//DEBUG_TOKEN_MANAGER = true ;
}
....
void primary() : {}
{
... stuff omitted...
|
( <WHILE> block() <DO> block() <END> )#While
|
... more stuff omitted...
}

So each while loop in the source is represented by a JJTWHILE
node with 2 children.

(In this language, there are only expressions, which is why the
guard and the body of the loop have the same syntactic category.)

Then there is a big big execution method like this:
static Object exec( SimpleNode n, HashMap env )
throws SimpleScriptError
{
switch( n.id ) {
... stuff omitted...
case JJTWHILE : {
while( true ) {
Object value = exec( child(n,0), env ) ;
boolean b = convertToBool( value ) ;
if( !b ) break ;
exec( child(n,1), env ) ; }
return trivialObj ; }
... more stuff omitted...
}
}

(Type checking is at run time and errors are represented by exceptions,
so if the guard expression can't convert to boolean, there is an exception
thrown -- in case you were wondering about that.)

Simple wah?

His majesty Grand Duke of Shumadija and Western Pomoravlje
 
Odgovor na temu

[es] :: Art of Programming :: JavaCC i implementiranje petlje

[ Pregleda: 2303 | Odgovora: 1 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.