I have built a recursive decent parser based on a grammar. Currently my parser only tells if the input sequence of Tokens is accepted by the grammar. I want to return if the grammar accepts the input and an Abstract Syntax Tree. I'm not sure how to do that.
What I have so far is a function corresponding to each production rule in the grammar. I have transformed the grammar so that a Terminal is always the first element of each production rule. Below is a subset of the grammar I am trying to build a syntax tree for.
program -> VAR = exp
exp -> NUM term
exp -> LEFTPAR exp RIGHTPAR
term -> MUL NUM term
term -> DIV NUM term
term -> . (empty)
An example of a function for a rule would be:
public Pair<bool, Token> exp(Token tok)
{
if (tok.type == NUM)
{
return term(tok.next);
}
if (tok.type = LEFTPAR)
{
Pair<bool, Token> temp = exp(tok.next);
if (temp.left && temp.right.type == RIGHTPAR)
return new Pair<bool, Token>(true,temp.right.next);
return new Pair<bool, Token>(false,null);
}
}
What would a strategy be to turn functions like this into a syntax tree builder? I tried to pass a tree node as input to all the functions but when there are rules that have multiple non-terminals it gets a little more confusing. It seems like it would be easier to build a parse tree instead, then convert it to an AST afterwords. Any help is appreciated!
termin my example, or multiple (like in my full grammar)? Seems that the only solution is to have a bunch of if statements to check which ones are null and then don't return them. - McAngus