2015-03-30 12 views
6

Così Diciamo che ho il seguente grammatica:Come analizzare i nodi figli?

let_stat = "let" iden [ "=" expr ]; 
if_stat = "if" expr "->" stat; 
stat = let_stat | if_stat; 

questo sarebbe il seguente codice pseudo-ish:

let_stat parseLetStat() { 


if token is "let" { 
     consume token 

     if token is identifier { 
      char *value = consumetoken.value 
      let_stat let = new let_stat; 
      let.name = value; 

      if token is "=" { 
       let.value = parseExpression; 
      } 

      return let 
     } 
    } 
} 

if_stat parseIfStat() { 
    if token is "if" { 
     consume token 

     expression expr = parseExpression; 
     block block = parseBlock; 

     if_stat ifstmt = new if_stat 
     ifstmt.expr = expr 
     ifstmt.block = block 
     return ifstmt 
    } 
} 

stat parseStatement() { 

} 

Quale sarebbe la funzione parseStatement fare? Come sceglierebbe quale funzione chiamare, la funzione if_stat o la funzione let_stat? O dovrei buttare tutto il codice in una funzione? Non capisco, ogni aiuto sarebbe grandioso dal momento che sono confuso.

risposta

1

Il problema chiave per il tuo problema specifico è che quando c'è un'alternanza in una regola EBNF, il parser per il non terminale deve chiamare tutte le alternative e chiedere a ognuna se riconosce il costrutto; ogni subparser deve restituire una bandiera che indica sì o no.

Quello che probabilmente è necessario è general principles for writing a recursive descent parser.

+0

Oh capisco, perfetto. E naturalmente, risposta dall'unica e unica Ira Baxter, sembra che tu risponda a molte mie domande :) Grazie! :) –

+0

@ user3839220: Stai facendo domande sul mio territorio: -} –

Problemi correlati