antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.

Home Page:http://antlr.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ANTLR doesn't generate needed methods for C++

kamkow1 opened this issue · comments

I'm trying to generate C++ code from this grammar (I don't know where the issue is exactly, so I'll paste the entire thing, since it's not too big):

parser grammar Parser;

options {
    tokenVocab = 'Lexer';
}

file: statement *;

statement: (declaration | variableAssignment) SEMICOLON;

// -----------------------------------------------------------------
// Declarations & Assignment
// -----------------------------------------------------------------
declaration: IDENTIFIER COLON (functionDeclaration | variableDeclaration);
functionDeclaration: functionType codeBlock?;
variableDeclaration: type? EQUALS expression;

variableAssignment: IDENTIFIER EQUALS expression;

// -----------------------------------------------------------------
// Types
// -----------------------------------------------------------------
type: typeModifier* typeBase;
typeModifier: ASTERISK;
typeBase: functionType | nameType;
functionType: OPEN_PAREN functionTypeParam * CLOSED_PAREN ARROW type;
functionTypeParam: IDENTIFIER COLON type;
nameType: IDENTIFIER;


codeBlock: OPEN_BRACE statement * CLOSED_BRACE;

// -----------------------------------------------------------------
// Expressions
// -----------------------------------------------------------------
expression:
        integerExpression #IntegerExpression1
    |   identifierExpression #IdentifierExpression1
    |   callExpression #CallExpression1
;

integerExpression: INTEGER;
identifierExpression: IDENTIFIER;
callExpression: IDENTIFIER OPEN_PAREN callParamList? CLOSED_PAREN;
callParamList: expression (expression COMMA)*;

I'm getting this compiler error:

visitor.cpp: In member function ‘virtual std::any kaic::Visitor::visitVariableDeclaration(kaic::KaiParser::VariableDeclarationContext*)’:
visitor.cpp:65:46: error: ‘struct kaic::Visitor’ has no member named ‘visitExpression’; did you mean ‘visitCallExpression’?
   65 |     auto value = std::any_cast<Value*>(this->visitExpression(ctx->expression()));
      |                                              ^~~~~~~~~~~~~~~
      |                                              visitCallExpression
visitor.cpp: In member function ‘virtual std::any kaic::Visitor::visitVariableAssignment(kaic::KaiParser::VariableAssignmentContext*)’:
visitor.cpp:89:47: error: ‘struct kaic::Visitor’ has no member named ‘visitExpression’; did you mean ‘visitCallExpression’?
   89 |     auto value = std::any_cast<Value *>(this->visitExpression(ctx->expression()));
      |                                               ^~~~~~~~~~~~~~~
      |                                               visitCallExpression

why is this happening? why can't I visit the expression? Also, when I checked the generated parser files, it seems like the expression context class doesn't have any methods that would point to callExpression and others

Here's the variable declaration context class

  class  VariableDeclarationContext : public antlr4::ParserRuleContext {
  public:
    VariableDeclarationContext(antlr4::ParserRuleContext *parent, size_t invokingState);
    virtual size_t getRuleIndex() const override;
    antlr4::tree::TerminalNode *EQUALS();
    ExpressionContext *expression();
    TypeContext *type();


    virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
   
  };

Here's the expression context class:

  class  ExpressionContext : public antlr4::ParserRuleContext {
  public:
    ExpressionContext(antlr4::ParserRuleContext *parent, size_t invokingState);
   
    ExpressionContext() = default;
    void copyFrom(ExpressionContext *context);
    using antlr4::ParserRuleContext::copyFrom;

    virtual size_t getRuleIndex() const override;

   
  };

I don't get it. How am I supposed to get to callExpression, identifierExpression or integerExpression??

Also, the parser base class doesn't have visitExpression.