I'm trying to learn how to use Jison (a Javascript parser generator that uses Bison syntax).
I have some code that looks like this:
a: "{{index()}}"
b: "{{blah(2, 'aba')}}"
I'm trying to create a parser that will return index()
if passed string a
, and blah(2, 'aba')
if passed string b
. (Essentially, I need to parse strings containing method invocations).
I've been trying to adapt from the examples provided by Jison, but I'm hampered by my lack of understanding of how parsing works!
Here's my grammar file:
/* lexical grammar */
%lex
%%
\s+ /* skip 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;}
;
Obviously it isn't complete yet; it doesn't recognize parentheses. I'm starting with the simple example of passing the parser this string: {{index}}
. When I give my current parser this, it returns }}
. Based on my (definitely erroneous) understanding of my grammar, I would expect it to return index
.
What am I doing wrong?