Writing a Tokenizer class in C# for a BASIC Interpreter -


For a little fun, I am trying to write a basic interpreter in C #. The following is my Tokeninger class (with only a few keywords) I am after any suggestions or comments ... Code clarity is more important to me than efficiency.

  class tokensager {const string operator = "+ - / *%  gt; = & amp;;"; Private listing & lt; String & gt; Keyword = new list & lt; String & gt; {"LET", "DIM", "PRINT", "REM"}; Private listing & lt; String & gt; Token = new list & lt; String & gt; (); Private listing & lt; String & gt; Token type = new list & lt; String & gt; (); Private Ink Token Index; // Turn on command string in token Public invalid tokenize (string cmdline) {string token = ""; Char lastc = ''; Bool inString = false; Tokens.Clear (); TokenTypes.Clear (); // Step through line and divide into Tocane Foursachet (4 C in Cndline) {If (C == '' ') inString =! InString; If (! InString) {If (Isopater (last)) AddToken (Ref Token); If (iSophiteSpace (C)) adton (referee token); If (Iopater (C)) AddToken (Ref Token); Add (ifer (a) and a. Token + c =}; and token + c = lastc = c;} Add the last token addToken (empty token); tokenindex = 0;} public string token () {token Token [tokenIndex];} Public string tokentype () {return token type [tokenIndex];} Public Zero NextToken () {tok enIndex ++;} Public Hair TokensLeft () {Return tokenIndex  

You usually do not want to write parser code like manual manually, Learning good parser generator tools like your time is a good investment. If you're just being funny / being a computer language, if you really want to do this manually, there are some things to think about yourself:

  • Use string builder instead of string
  • Simply Checking Ddharnon is not enough, what's left of the citations?
  • How do you handle floating point numbers (in all formats)?
  • How do you handle identifiers that contain numbers?

There are some issues that will run in you, then I really recommend learning a parser generator tool, it makes such a thing more fun (not to mention right and efficient ).


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -