0

I'm trying to build a tree to make a parser. My english isn't so good so I will give an example:

function int hello(int a,b,c,d ; char f)

trying to build this tree:

                                   ARGS
                                  /    \
                                int     char
                               / | \      \ 
                              a  b  c      f

The code I already tried (tried to run just a part to see if it works cus my args grammar im stuck too):

type
    : types ids ($$=addSons($1,$2);) {} //$2 will be son of $1
    ;

types
    : INT {$$=newNode(yytext); }
    | CHAR {$$=newNode(yytext); }
    ;

ids
    : id {$$=$1}
    | idss {$$=$1}
    ;

idss
    : id ',' idss {$$=$1;}
    | id ';' {$$=$1;}
    ;

id
    : ID {$$=newNode(yytext); }
    ;

but it doesn't work. My function addson() accepts a different number of variables and every time the first arg is father of the rest sons. If I send son after son the function will add the last son to the next index of the sons automatically.

So my question is if there is any grammar(or an other solution) that can help me so I can connect this tree, unfortunately the grammar of args im stuck too with the same problem.

Liran.B
  • 1
  • 5
  • This has nothing to do with [tag:lex]. You need to use left-recursion instead of right-recursion in your grammar; you need to add the new element to each list production; – user207421 Dec 17 '19 at 07:25
  • can you show me for this example the code that I can do please? I already tried everything , if I will have one example I think I will succeed to build the compiler(the mission is to build compiler its just part1) thanks alot – Liran.B Dec 17 '19 at 07:34
  • The default action is `{ $$ = $1; }` so you can omit that. As for the rest, see https://stackoverflow.com/q/33235379/1256452 – torek Dec 18 '19 at 01:32

0 Answers0