0

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 numericLiterals 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.

Johnston
  • 20,196
  • 18
  • 72
  • 121

1 Answers1

1

You're missing commas in some of your object literals (specifically after both occurrences of type: BinaryExpression).

It looks like syntax simply ignores actions with syntax errors in --parse mode. If you actually compile the grammar to JavaScript using --output, you'll get an actual syntax error telling you where the mistake is when you try to run the generated file with node.

sepp2k
  • 363,768
  • 54
  • 674
  • 675