I'm currently diving into the world of Jison, a Javascript parser generator utilizing Bison syntax.
My current code snippet resembles the following:
a: "{{index()}}"
b: "{{blah(2, 'aba')}}"
My goal is to develop a parser that can interpret index()
when given string a
, and blah(2, 'aba')
when given string b
. In essence, I aim to parse strings containing method invocations.
I've been working on adapting examples from Jison but struggle due to my limited comprehension of parsing principles!
Below is the grammar file I've been working on:
/* lexical grammar */
%lex
%%
\s+ /* ignore whitespace */
[a-zA-Z0-9]+ return 'STR'
"{{" return '{{'
"}}" return '}}'
<<EOF>> return 'EOF'
. return 'INVALID'
/lex
/* operator associations and precedence */
%token '{{' '}}'
%start expressions
%% /* language grammar */
expressions
: e EOF
{ typeof console !== 'undefined' ? console.log($1) : print($1);
return $1; }
;
e
: '{{' e '}}'
{$$ = yytext;}
| STR
{$$ = yytext;}
;
It is evident that the grammar is not yet complete; it does not recognize parentheses. I'm currently focusing on a basic example of feeding the parser the string {{index}}
. When I test this string, my current parser outputs }}
. According to my (inaccurate) understanding of the grammar, I anticipated index
as the output.
What could be the mistake in my approach?