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 3699708
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:08:09+00:00 2026-05-19T05:08:09+00:00

I have written simple lexical analyzer. And I understand the need to provide each

  • 0

I have written simple lexical analyzer. And I understand the need to provide each recognized token with attribute. Let’s see what I got:

public sealed class Token
{ 
    public enum TokenClass
    { 
        Identifier,
        StringLiteral,
        NumberLiteral,
        Operator,
        PunctuationSeparator,
        Bracket,
        Parenthesis
    }        
    public TokenClass Class { get; internal set; }
    public String     Value { get; internal set; }
}

In lexer I enqueue tokens setting up thier value & class. But what about attributes? How should I design the feature relative to my existing token class?

First tought came into my mind was:

  1. Declare private abstract classes of “ambiguous-entities” (I mean that Number could be Integer and Real and so on) inside token class;
  2. Then declare inherited classes e.g.
    public class Comma : PunctuationSeparator {};
  3. Add Property Object Attribute {get; private set;};
  4. Then create method like private void ApplyAttribute();
  5. Call ApplyAttribute() when token is instantiated and properties are set;
  6. Use something like this inside ApplyAttribute().

    switch(this.TokenClass)
    {
    case this.TokenClass.Number:
        {
            this.Attribute = (Int32.TryParse(this.Value))? new Integer() : new Real();                
        }
    }
    

In parser it would be easy to write something like that if(CurToken.Attribute is Integer).
One thing that stops me from doing like that is number of classes I should create. Is this solution acceptable?

  • 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-19T05:08:09+00:00Added an answer on May 19, 2026 at 5:08 am

    The attributes I’d use for a token? Probably something along the lines of

    public class Token
    {
      public TokenType Type { get ; private set ; }
      public string    Text { get ; private set ; }
      public int       LineNumber { get ; private set ; }
      public int       Column     { get ; private set ; }
    }
    
    public enum TokenType
    {
      Keyword : 1 ,
      Integer ,
      String  ,
      Whitespace ,
      Comment ,
      ... 
    }
    

    I disagree, though, with the previous poster regarding conversion of the token’s text into a ‘value’. IMHO, that is the domain of the parser and the nodes of the parse tree. Until the parser has placed the tokens in context WRT the grammar, the token is just a piece of text with a label attached to it. The lexical analyzer doesn’t know (and should care) what’s happening downstream — for all it know, the took is pretty-printing the source text (in which case, you want to leave the individual tokens alone).

    You might want to take a look at Terrance Parr’s book(s):

    • Language Implementation Patterns: Create Your Own Domain-Specific and General Programming Languages
    • The Definitive ANTLR Reference: Building Domain-Specific Languages
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a simple brainfuck interpreter in MATLAB script language. It is fed
I have written the following simple test in trying to learn Castle Windsor's Fluent
I have written a very simple bash script to help me migrate from dev
I have written a secure TCP server in .NET. This was basically as simple
I have big issue with url-rewriting for IIS 7.0. I've written simple module for
I have a simple function written in Oracle 9i (version 9.2.0.4.0) to simulate an
I have a simple little test app written in Flex 3 (MXML and some
I have a simple GUI component written in Java. The class draws an analog
I have a very simple TCP server written in C. It runs indefinitely, waiting
I have some code I've written in PHP for consuming our simple webservice, which

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.