I have the below grammar to create Abstract Syntax Tree for the expression foo(a,b). I have made the parse tree, bit confused about the AST, looks like foo(a,b) should be the parent with two leaves id(a) and id(b). Please advise.
stmnt : assignment
| subr_call
;
assignment : ID ASSIGN expr;
subr_call : ID (arg_list);
expr : primary expr_tail;
expr_tail : op expr
| /* epsilon */
;
primary : ID
| subr_call
| (expr)
;
op : ADD
| SUB
| MUL
| DIV
;
arg_list : expr args_tail
;
args_tail : COMMA arg_list
| /* epsilon */
;AST means Abstract Syntax Tree. It is used to tree structure which is used to store the data from the immediate input from the compiler. and it also gives a tree representation for source code used in a program.
To construct a abstract syntax tree we need to have some specifications.
they are :1. variables should be declared clearly in compiler.
2. the statements which are executed should be explained clearly in perfect manner.
3. binary operations, identifiers and their assigned values should be store in secure.
these all are enough for designing the abstract syntax tree.
now from the above data we can construct abstract syntax tree either by using program language or by using step wise manner.
here the first cal is for subtraction
generally foo function is used to cal a function from start.
here we need abstract syntax tree to get addition, multiplication , division, subtraction
step 1 : first take a node and save it for multiplication function.
step 2: with the join of that node create three nodes where one node is declared as id. one for multiplication function and another to join further nodes.
step 3: now join the node which is created to connect further nodes with again three nodes one node is declared as id . one for addition and again for connecting for further nodes.
step 4: same process continues until all the operations are completed then the construction of abstract tree is completed.
there will be no need of foo function unless and until we need to cal again for main function.
if in that case we can use nodes which are created in construction of binary syntax tree.
I have the below grammar to create Abstract Syntax Tree for the expression foo(a,b). I have...