CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2007
    Posts
    122

    lexical analysis problem with real number

    lexical analysis problem with real number and i attache the code and i hope to find solution
    Attached Files Attached Files

  2. #2
    Join Date
    Feb 2007
    Posts
    122

    Re: lexical analysis problem with real number

    plz help me

  3. #3
    Join Date
    Sep 2004
    Posts
    1,361

    Re: lexical analysis problem with real number

    plz pst mre nfo.
    k thx.

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: lexical analysis problem with real number

    Quote Originally Posted by ahmed17
    lexical analysis problem with real number and i attache the code and i hope to find solution
    Hope to find a solution for what? You have not asked any question. What exactly is the problem. People here are not going to download your code and check by themselves what the problem is.

    You will have to post the problem and what exactly do you want your code to do. Then only people here would be able to answer your questions and may suggest something that might be useful and may solve your problem.

  5. #5
    Join Date
    Feb 2007
    Posts
    122

    Re: lexical analysis problem with real number

    i am make simple lexical analyser that make scanning on the text that wrote in the richtextbox and determiine which is identifer and which is integer and so on , then they display the result in dataGrid but the problem with determine the real number .

    Code:
    namespace LexicalAnalyser
    {
        enum TokenType
        {
            EOF,
            WhiteSpace,
            Identifier,
            Invalid,
            Integer,
            Real
        }
    
        class Token
        {
            private string _lexeme;
            private TokenType _type;
    
            public Token(string lexeme, TokenType type)
            {
                _lexeme = lexeme;
                _type = type;
            }
    
            public string Lexeme { get { return _lexeme; } }
            public TokenType Type { get { return _type; } }
    
        }
    }


    Code:
    namespace LexicalAnalyser
    {
        class Lex
        {
            int _start;
    		int _state;
    		int _currentPosition;
    		int _lexemeBeginning;
    		private string _sourceCode;
    
    		public Lex(string SourceCode)
    		{
    			_start = 0;
    			_state = 0;
    			_currentPosition = 0;
    			_lexemeBeginning = 0;
    			_sourceCode = SourceCode;
    		}
    
    		private char NextChar()
    		{
    			_currentPosition++;
    			if(_currentPosition <= _sourceCode.Length)
                    return _sourceCode[_currentPosition - 1];
    			else return (char)1; //end of file
    		}
    
    		private void Retract()
    		{
    			_currentPosition--;
    		}
    
    		public Token GetToken()
    		{
    			char ch;
    			_start = 0;
    			_state = 0;
    			_lexemeBeginning = _currentPosition;
    			
                float flt;
    
    			while(true)
    			{
    				switch(_state)
    				{
    					// Transition states recognizing EOF
    					case 0:
    						ch = NextChar();
    						if(ch == 1)_state = 1;
    						else Fail();
    						break;
    
    					case 1:
    						return new Token("", TokenType.EOF);
    
    					//Transition states recognizing white spaces
    					case 2:
    						ch = NextChar();
    						if(ch == ' ' || ch == '\t' || ch == '\n' || ch == 10 || ch == 13 || ch == '\r')_state = 3;
    						else Fail();
    						break;
    
    					case 3:
    						ch = NextChar();
    						if(ch == ' ' || ch == '\t' || ch == '\n' || ch == 10 || ch == 13 || ch == '\r')_state = 3;
    						else _state = 4;
    						break;
    
    					case 4:
    						Retract();
    						return new Token(_sourceCode.Substring(_lexemeBeginning, _currentPosition - _lexemeBeginning), TokenType.WhiteSpace);
    					
    					//Transition states recognizing Identifiers
    					case 5:
    						ch = NextChar();
    						if(char.IsLetter(ch) || ch == '_')_state = 6;
    						else Fail();
    						break;
    
    					case 6:
    						ch = NextChar();
    						if(char.IsLetterOrDigit(ch) || ch == '_')_state = 6;
    						else _state = 7;
    						break;
    
    					case 7:
    						Retract();
                            return new Token(_sourceCode.Substring(_lexemeBeginning, _currentPosition - _lexemeBeginning), TokenType.Identifier);
    
    					/***************(detecting real Or integer numbers)******************/
                       
                            
                        /*detecting Integers*/
                        case 8:
                            ch = NextChar() ;
                            if (char.IsDigit(ch) || ch == '-' )
                                _state = 9; // it was 9
                                else Fail();
                            break;
    
                        case 9:
                            ch = NextChar();
                            if (char.IsDigit(ch))
                                _state = 9;
                            else _state = 10;
    
                            break;
    
                        case 10 :
                            Retract();
                            return new Token(_sourceCode.Substring(_lexemeBeginning, _currentPosition - _lexemeBeginning), TokenType.Integer);
                        /* Detecting real */
                        case 11:
                            ch = NextChar();
                           // Boolean bl = System.Single.TryParse(ch, out flt);                 
                            if (char.IsDigit(ch))                   
                                _state = 12;//it was 12
                            else Fail();
                            break;
                        case 12:
                            ch = NextChar();
                            if (char.IsDigit(ch)) 
    
                                _state = 12;
                            else _state = 13;
    
                            break;
                        case 13:
                            Retract();
                            return new Token(_sourceCode.Substring(_lexemeBeginning, _currentPosition - _lexemeBeginning), TokenType.Real);
                     
                        // Invalid token
    					default:
    						ch = NextChar();
    						return new Token(ch.ToString(), TokenType.Invalid);
    				}
    			}
    		}
    
    		private void Fail()
    		{
    			_currentPosition = _lexemeBeginning;
    			switch(_start)
    			{
    				case 0:		_start = 2;		break;
    				case 2:		_start = 5;		break;
    				case 5:		_start = 8;	    break;
                    case 8:     _start =11;       break;
                }
    			_state = _start;
    		}
        }
    }

  6. #6
    Join Date
    Sep 2004
    Posts
    1,361

    Re: lexical analysis problem with real number

    You know about regular expressions, right?

  7. #7
    Join Date
    Feb 2007
    Posts
    122

    Re: lexical analysis problem with real number

    i want the solution

  8. #8
    Join Date
    Nov 2007
    Posts
    110

    Re: lexical analysis problem with real number

    Quote Originally Posted by ahmed17
    i want the solution
    And I want a new car, doesn't mean I am going to get it without putting in some work myself rather than waiting to have it given to me.

  9. #9
    Join Date
    Feb 2007
    Posts
    122

    Re: lexical analysis problem with real number

    thanks for ur advise

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured