dierre
18-02-2011, 13:06
Mi rendo conto che un long shot chiedere qui (di solito non si parla di parser generator), ma tentare non nuoce.
Con SableCC 3.2 ho implementato un Custom Lexer che mi stampa i token letti fino ad un certo punto. Ecco un esempio:
TId, state : 0, text : [font]
TLPar, state : 0, text : [(]
TBlank, state : 0, text : [ ]
TId, state : 0, text : [arial]
TComma, state : 0, text : [,]
TBlank, state : 0, text : [ ]
TId, state : 0, text : [bold]
TComma, state : 0, text : [,]
TBlank, state : 0, text : [ ]
TNumber, state : 0, text : [20]
TRPar, state : 0, text : [)]
TBlank, state : 0, text : [ ]
TLPar, state : 0, text : [(]
TBlank, state : 0, text : [ ]
TLPar, state : 0, text : [(]
TElement, state : 0, text : [v]
TDot, state : 0, text : [.]
TId, state : 0, text : [degree]
TBlank, state : 0, text : [ ]
TCop, state : 0, text : [>=]
TBlank, state : 0, text : [ ]
TNumber, state : 0, text : [3]
TBlank, state : 0, text : [ ]
TRPar, state : 0, text : [)]
TBlank, state : 0, text : [ ]
TLogicAnd, state : 0, text : [AND]
TBlank, state : 0, text : [ ]
TLogicNot, state : 0, text : [NOT]
TBlank, state : 0, text : [ ]
TElement, state : 0, text : [v]
TDot, state : 0, text : [.]
TId, state : 0, text : [label]
TBlank, state : 0, text : [ ]
TCop, state : 0, text : [==]
TBlank, state : 0, text : [ ]
TString, state : 0, text : ["Roma"]
TBlank, state : 0, text : [ ]
TRPar, state : 0, text : [)]
TBlank, state : 0, text : [ ]
TRPar, state : 0, text : [)]
Parser Exception
[1,71] expecting: EOF
Come potete vedere, siccome l'istruzione è incorretta, il parser genera un'eccezione dicendo dove è sbagliato il codice. Il che mi va benissimo, però il fatto che sia in grado di capire dov'è l'errore implica che sto benedetto AST è parzialmente costruito. Il problema è che il TreeWalker che deriva da un DepthFirstAdapter si attiva solo quando il parser non ha generato errori, io invece vorrei avere comunque accesso all'AST perché in pratica mi serve per colorare la sintassi e in questo caso i vari TId o TNumber che vedete sono in realtà dei parametri, io vorrei avere accesso al padre:
par =
{numero} number |
{stringa} string |
{idpar} id ;
Con SableCC 3.2 ho implementato un Custom Lexer che mi stampa i token letti fino ad un certo punto. Ecco un esempio:
TId, state : 0, text : [font]
TLPar, state : 0, text : [(]
TBlank, state : 0, text : [ ]
TId, state : 0, text : [arial]
TComma, state : 0, text : [,]
TBlank, state : 0, text : [ ]
TId, state : 0, text : [bold]
TComma, state : 0, text : [,]
TBlank, state : 0, text : [ ]
TNumber, state : 0, text : [20]
TRPar, state : 0, text : [)]
TBlank, state : 0, text : [ ]
TLPar, state : 0, text : [(]
TBlank, state : 0, text : [ ]
TLPar, state : 0, text : [(]
TElement, state : 0, text : [v]
TDot, state : 0, text : [.]
TId, state : 0, text : [degree]
TBlank, state : 0, text : [ ]
TCop, state : 0, text : [>=]
TBlank, state : 0, text : [ ]
TNumber, state : 0, text : [3]
TBlank, state : 0, text : [ ]
TRPar, state : 0, text : [)]
TBlank, state : 0, text : [ ]
TLogicAnd, state : 0, text : [AND]
TBlank, state : 0, text : [ ]
TLogicNot, state : 0, text : [NOT]
TBlank, state : 0, text : [ ]
TElement, state : 0, text : [v]
TDot, state : 0, text : [.]
TId, state : 0, text : [label]
TBlank, state : 0, text : [ ]
TCop, state : 0, text : [==]
TBlank, state : 0, text : [ ]
TString, state : 0, text : ["Roma"]
TBlank, state : 0, text : [ ]
TRPar, state : 0, text : [)]
TBlank, state : 0, text : [ ]
TRPar, state : 0, text : [)]
Parser Exception
[1,71] expecting: EOF
Come potete vedere, siccome l'istruzione è incorretta, il parser genera un'eccezione dicendo dove è sbagliato il codice. Il che mi va benissimo, però il fatto che sia in grado di capire dov'è l'errore implica che sto benedetto AST è parzialmente costruito. Il problema è che il TreeWalker che deriva da un DepthFirstAdapter si attiva solo quando il parser non ha generato errori, io invece vorrei avere comunque accesso all'AST perché in pratica mi serve per colorare la sintassi e in questo caso i vari TId o TNumber che vedete sono in realtà dei parametri, io vorrei avere accesso al padre:
par =
{numero} number |
{stringa} string |
{idpar} id ;