Note that there are some explanatory texts on larger screens.

plurals
  1. POToken Attributes
    primarykey
    data
    text
    <p>I have written simple lexical analyzer. And I understand the need to provide each recognized token with attribute. Let's see what I got:</p> <pre><code>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; } } </code></pre> <p>In lexer I enqueue tokens setting up thier value &amp; class. But what about attributes? How should I design the feature relative to my existing token class?</p> <p>First tought came into my mind was:</p> <ol> <li>Declare private abstract classes of "ambiguous-entities" (I mean that Number could be Integer and Real and so on) inside token class;</li> <li>Then declare inherited classes e.g. <code>public class Comma : PunctuationSeparator {}</code>;</li> <li>Add Property <code>Object Attribute {get; private set;}</code>;</li> <li>Then create method like <code>private void ApplyAttribute()</code>;</li> <li>Call <code>ApplyAttribute()</code> when token is instantiated and properties are set;</li> <li><p>Use something like this inside <code>ApplyAttribute()</code>.</p> <pre><code>switch(this.TokenClass) { case this.TokenClass.Number: { this.Attribute = (Int32.TryParse(this.Value))? new Integer() : new Real(); } } </code></pre></li> </ol> <p>In parser it would be easy to write something like that <code>if(CurToken.Attribute is Integer)</code>. One thing that stops me from doing like that is number of classes I should create. Is this solution acceptable?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload