How to include counting of method declaration, constants, functions calls in parser?

StackOverflow https://stackoverflow.com/questions/10791876

  •  11-06-2021
  •  | 
  •  

سؤال

I am implementing parser for language similar to java but simpler. I have written a lexer, and am now writing parser. I have written the grammar, I need to implement counting of elements but I have no idea were to put it. I think that it should be in action code after productions but I don't know were to initialize it.

For example

MethodDecl ::= ReturnType:type IDENTIFIER:id LPAREN FormParsOp:pars RPAREN VarDeclListOp LBRACE StatementListOp RBRACE
       {: System.out.println("Method: " + type + " " + id + "(" + pars + ")");   methodNumber++;:} 
    | STATIC ReturnType:type IDENTIFIER:id LPAREN FormParsOp:pars RPAREN VarDeclListOp LBRACE StatementListOp RBRACE
       {: System.out.println("Static method: " + type + " " + id + "(" + pars + ")");  staticMethodNumber++;:} ;

Where should I define and initialize the variables methodNumber and staticMethodNumber?

هل كانت مفيدة؟

المحلول

Solved... I put them in action code and initialized there

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top