Click to See Complete Forum and Search --> : lexical analysis problem with real number


ahmed17
March 7th, 2008, 08:32 AM
lexical analysis problem with real number and i attache the code and i hope to find solution

ahmed17
March 7th, 2008, 01:46 PM
plz help me

DeepT
March 7th, 2008, 02:44 PM
plz pst mre nfo.
k thx.

Shuja Ali
March 7th, 2008, 02:57 PM
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.

ahmed17
March 7th, 2008, 03:15 PM
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 .


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; } }

}
}





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;
}
}
}

DeepT
March 7th, 2008, 03:55 PM
You know about regular expressions (http://www.dotnetcoders.com/web/Learning/Regex/default.aspx), right?

ahmed17
March 8th, 2008, 05:58 AM
i want the solution

jshultz
March 8th, 2008, 08:15 AM
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.

ahmed17
March 8th, 2008, 01:22 PM
thanks for ur advise