Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ANTLR doesn't generate needed methods for C++ #4651

Open
kamkow1 opened this issue Jul 3, 2024 · 1 comment
Open

ANTLR doesn't generate needed methods for C++ #4651

kamkow1 opened this issue Jul 3, 2024 · 1 comment

Comments

@kamkow1
Copy link

kamkow1 commented Jul 3, 2024

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??

@kamkow1
Copy link
Author

kamkow1 commented Jul 3, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant