[ masetrt @ 08.10.2007. 22:46 ] @
Ukratko da li je neko implementirao petlje putem JavaCC. Pa ako neko jeste mali primer bez obzira na sintaksu :) bio bi od pomoci |
[ masetrt @ 08.10.2007. 22:46 ] @
[ masetrt @ 11.10.2007. 18:15 ] @
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? Copyright (C) 2001-2025 by www.elitesecurity.org. All rights reserved.
|