Data una grammatica LL (1) Che cos'è una struttura dati o un algoritmo appropriati per produrre un albero di sintassi concreto immutabile in modo funzionalmente puro? Non esitate a scrivere codice di esempio in qualsiasi lingua preferite.Che cos'è una struttura dati o un algoritmo appropriati per produrre un albero di sintassi concreto immutabile in modo funzionalmente puro?
mia Idea
symbol : either a token or a node result : success or failure token : a lexical token from source text value -> string : the value of the token type -> integer : the named type code of the token next -> token : reads the next token and keeps position of the previous token back -> token : moves back to the previous position and re-reads the token node : a node in the syntax tree type -> integer : the named type code of the node symbols -> linkedList : the symbols found at this node append -> symbol -> node : appends the new symbol to a new copy of the node
Ecco un'idea che ho pensato. Il problema principale qui è la gestione degli errori di sintassi. Intendo dire che potrei fermarmi al primo errore ma non mi sembra giusto.
let program token =
sourceElements (node nodeType.program) token
let sourceElements node token =
let (n, r) = sourceElement (node.append (node nodeType.sourceElements)) token
match r with
| success -> (n, r)
| failure -> // ???
let sourceElement node token =
match token.value with
| "function" ->
functionDeclaration (node.append (node nodeType.sourceElement)) token
| _ ->
statement (node.append (node nodeType.sourceElement)) token
Nota Bene
mi offriranno un piacevole grazia per la migliore risposta in modo da non sentire affrettato. Le risposte che pubblicano semplicemente un link avranno meno peso sulle risposte che mostrano il codice o contengono spiegazioni dettagliate.
nota finale
Sono veramente nuovo a questo genere di cose, quindi non abbiate paura di chiamare me uno scemo.
Cosa c'è di sbagliato negli alberi composti dal basso verso l'alto? –
@Ira - Dimmi, sono nuovo di tutto questo. – ChaosPandion
Dal momento che indovinare la conoscenza precedente è difficile, potresti voler commentare le risposte e chiedere chiarimenti. –