Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7676849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:17:29+00:00 2026-05-31T17:17:29+00:00

After creating the parse tree, i have to populate symbol table now. I have

  • 0

After creating the parse tree, i have to populate symbol table now.

I have to store information like

Type, Scope, Offset etc for the identifiers.

Now how do i know the type, scope of the identifiers , since all i know is the lexeme value and line number for that particular ID (after lexical analysis).

How do i got about the whole thing. thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:17:30+00:00Added an answer on May 31, 2026 at 5:17 pm

    Now how do i know the type, scope of the identifiers , since all i
    know is the lexeme value and line number for that particular ID (after
    lexical analysis).

    As EJP mentioned, you need to step through the parse tree. Your tree should have been created so that you can do an in-order traversal, visiting each node in the same order in which source code statements and expressions are evaluated. Your tree nodes should also correspond with a specific language construct, e.g. WhileStmtNode, MethodDeclNode, etc.

    Suppose I am building the symbol table, recursively stepping through the tree, and I’ve just entered a method body node. I might have something like the following:

    public void doAction(MethodBodyNode methodBody) {
        currScope = 2;
        methodBody.getExpr().applyAction(this);
        currScope = 2;
    }
    

    I keep a global variable to manage the scope. Every time I enter a block where the scope changes, I’d increment currScope. Similarly, I’d maintain currClass and currMethod variables to store with the symbol name, type, offset, etc. for later phases.

    Update:

    Now say, i am traversing the tree, everytime i come across an ID i
    would have to enter the value to symbol table along with the type,
    scope and others, say for scope i check if i come across ‘{‘ or
    function name, but how do i know what type of ID is this.

    Each tree node should contain all the necessary information for the entire construct. If you’re using a parser generator, like CUP or Bison, you can specify how to build the tree in the grammar actions. E.g.

    variableDeclaration::= identifier:i identifier:i2 SEMICOLON {: RESULT = new VarDeclNode(i, i2, null); :};
    identifier::= ID:i {: RESULT = new IdNode(i.getLineNum(), i.getCharNum(), i.getStringValue()); :};
    

    Those productions would match Foo f; and append a variable declaration node to the tree. That node encapsulates two identifier nodes that contain the line number, character number, and string value of the lexeme. The first identifier node is the type and the second is the variable name. ID is a terminal symbol returned by the lexer upon matching an identifier. I’m assuming you are familiar with this to some extent.

    public class VarDeclNode extends StmtNode {
    
        private IdNode id;
        private IdNode type;
        private ExprNode expr;
    
        public VarDeclNode(IdNode id, IdNode type, ExprNode expr) {
            super();
            this.id = id;
            this.type = type;
            this.expr = expr;
        }
    
    }
    

    When you’ve got a syntax tree with nodes like this, you’ve got all the information you need.

    2nd Update:

    It doesn’t matter whether you’re using a custom parser or a generated one, there is one distinct point where you add a node into the tree upon matching a production. And it doesn’t matter what language you’re using. C structs will do just fine.

    if it is a non terminal has the info as Nonterminals name, and if it
    is a terminal i.e. a token, then the info in token i.e. lexeme value,
    token name and line number are stored

    You must have specialized nodes in the tree, e.g. ClassNode, TypeNode, MethodDeclNode, IfStmtNode, ExprNode. You can’t just store one type of node and put non-terminals and terminals in it. A non-terminal is represented as a tree node, there’s no other information to store about it beside the parts that compose it, which are generally non-terminals themselves. You wouldn’t store any token information. There are only but a few instances where you’d actually store a lexeme’s string value: for an identifier and for a string/boolean/integer literal.

    Have a look at this example. During the first reduction when S is reduced to (S + F), you append a ParenExprNode to the tree root. You also append a AddExprNode as child of the ParenExprNode. That logic must be hard-coded into your parser when applying a reduction by rule 2 of the grammar.

    The tree:

        ExprNode (root)
           |
      ParenExprNode
           |
       AddExprNode
       /         \
    ExprNode   ExprNode
    

    The code:

    struct ExprNode { void* exprNode; };
    struct ParenExprNode { void* exprNode; };
    struct AddExprNode { void* op1, * op2; };
    struct IdNode { char* val; int line; int charNum; };
    struct IntLiteralNode { int val; int line; int charNum; };
    
    void reduce_rule_2(ExprNode* expr) {
    
        //remove stack symbols
    
        //create nodes
        struct ParenExprNode* parenExpr = malloc(sizeof(struct ParenExprNode));
        struct AddExprNode* addExpr = malloc(sizeof(struct AddExprNode));
        addExpr->op1 = malloc(sizeof(struct ExprNode));
        addExpr->op2 = malloc(sizeof(struct ExprNode));
    
        //link them
        parenExpr->exprNode = (void*)addExpr;
        expr->exprNode = (void*)parenExpr;
    }
    

    In the next step, the left parenthesis is removed from the input. Afterward, S is on top of the stack and it is reduced to F by rule 1. Since F is the non-terminal for an identifier, it’s represented by IdNode.

    The tree:

        ExprNode
           |
      ParenExprNode
           |
       AddExprNode
       /         \
    ExprNode   ExprNode
       |
     IdNode
    

    The code:

    reduce_rule_2(addExpr->op1);
    
    void reduce_rule_1(ExprNode* expr) {
        //reduce stack symbols
        struct IdNode* id = malloc(sizeof(struct IdNode));
        id->val = parser_matched_text();
        id->lineNum = parser_line_num();
        id->charNum = parser_char_num();
        expr->exprNode = (void*)id;
    }
    

    And so on…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After creating a table (by migration), I want to insert some entries directly. How
I have such view that handles user registration. After creating new user i want
I'm using json simple to create and parse some json. However, after creating it,
After creating a instance of a class, can we invoke the constructor explicitly? For
After creating a new form, I usually perform this ritual: Change the name into
After creating a new object (Foo), I set the key (BarId) for an EntityRef
After creating a metaclass using Moose::Meta::Class->create , how do I instantiate a real Moose
After creating a child process and exiting it immediately (_exit()), I want to perform
After creating a div on the fly with this markup: $('.circuit').prepend(<div class='component' draggable='true'>TRANSISTOR</div>); It
Django is making very nice forms after creating a models.py and an admin.py. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.