I am trying to use Jison, which is a JS port of Bison, the parser generator. My goal is to convert this input:
foo(10)
bar()
foo(28)
baz(28)
into this:
[
{ func: 'foo', arg: 10 },
{ func: 'bar' },
{ func: 'foo', arg: 28 },
{ func: 'baz', arg: 28 }
]
Here is my bison file:
%lex
%%
[0-9]+\b return 'INTEGER'
\( return 'OPEN_PAREN'
\) return 'CLOSE_PAREN'
[\w]+\s*(?=\() return 'FUNC_NAME'
\n+ return 'LINE_END'
/lex
%%
expressions
: expressions expression
| expression
;
expression
: LINE_END
| e LINE_END
{return $1}
;
e
: FUNC_NAME OPEN_PAREN INTEGER CLOSE_PAREN
{$$ = { func: $1, arg: $3 };}
| FUNC_NAME OPEN_PAREN CLOSE_PAREN
{$$ = { func: $1 };}
;
The output of the resulting generated parser is { func: 'foo', arg: 10 }
. In other words, it only returns the parsed object from the first statement and ignores the rest.
I know my problem has to do with semantic value and the "right side" of expression
, but I am pretty lost otherwise.
Any help would be extremely appreciated!