How to parse child nodes?
So, let's say I have the following grammar:
let_stat = "let" iden [ "=" expr ]; if_stat = "if" expr "->" stat; stat = let_stat | if_stat; This will be the following psuedo-ish code:
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() { } What would the parseStatement function parseStatement ? How will he choose which function to call, the if_stat function or the let_stat function? Or would I throw all the code in one function? I do not quite understand, any help would be great, as I am confused.
The key problem for your specific problem is that when alternating in an EBNF rule, the parser for the non-terminal must call all alternatives and specify each of them if it recognizes the construct; each subparameter must return a flag indicating yes or no.
Perhaps you need some general guidelines for writing a recursive descent parser.