I am just learning lex/bison/yacc etc and I am running it through a library called syntax
This is through a tutorial on UDemy.
I am just getting started and trying to get it to make it generate an AST for 2 + 2
. Which seems like it should be trivial, but I can't figure out what is wrong with the code I am writing.
It is generating a AST for the number but not for the binary expression.
%lex
%%
\s+ /* skip whitespace */
\d+ return 'NUMBER'
[\+\-] return 'ADDITIVE_OPERATION'
[\*\/] return 'MULTIPLICATIVE_OPERATION'
/lex
%%
Expression
: AdditiveExpression
;
AdditiveExpression
: AdditiveExpression ADDITIVE_OPERATION MultiplicativeExpression
{
$$ = {
type: 'BinaryExpression'
op: $2,
left: $1,
right: $3
}
}
| MultiplicativeExpression
;
MultiplicativeExpression
: MultiplicativeExpression MULTIPLICATIVE_OPERATION PrimaryExpression
{
$$ = {
type: 'BinaryExpression'
op: $2,
left: $1,
right: $3
}
}
| PrimaryExpression
;
PrimaryExpression
: Literal
| ParenthesizedExpression
;
Literal
: NumericLiteral
;
NumericLiteral
: NUMBER
{
$$ = {
type: 'NumericLiteral',
value: Number($1)
}
}
;
ParenthesizedExpression
: '(' Expression ')' { $$ = $2 }
;
Then with the syntax-cli
I run
syntax-cli --grammar grammer/noa.bnf --mode LALR1 --parse '2 + 2' --debug
I get back:
Parsed value:
{
"type": "NumericLiteral",
"value": 2
}
It should be generating a binaryExpression
with numericLiteral
s inside.
I tried removing the ADDITIVE_OPERATOR
and replacing it with just +
, so I don't think that the [\+\-]
regex is the problem. I also double checked that regex in regexr.
I know that the NumericLiteral
is correct because it parses that. It just won't grab the AdditiveExpression
.