Skip to content
Snippets Groups Projects
SimpleLex.lex 1.68 KiB
Newer Older
%using ScannerHelper;
%namespace SimpleScanner

Alpha 	[a-zA-Z_]
Digit   [0-9] 
AlphaDigit {Alpha}|{Digit}
INTNUM  {Digit}+
REALNUM {INTNUM}\.{INTNUM}
ID {Alpha}{AlphaDigit}* 
lusparon332's avatar
lusparon332 committed
DotChr [^\r\n]
OneLineCmnt  \/\/{DotChr}*
StringUp \'[^']*\'

//     ,    -     Scanner
%{
  public int LexValueInt;
  public double LexValueDouble;
lusparon332's avatar
lusparon332 committed
  public List<string> idsInComment = new List<string>();
lusparon332's avatar
lusparon332 committed
%x COMMENT

%%
{INTNUM} { 
  LexValueInt = int.Parse(yytext);
  return (int)Tok.INUM;
}

{REALNUM} { 
  LexValueDouble = double.Parse(yytext);
  return (int)Tok.RNUM;
}

begin { 
  return (int)Tok.BEGIN;
}

end { 
  return (int)Tok.END;
}

cycle { 
  return (int)Tok.CYCLE;
}

{ID}  { 
  return (int)Tok.ID;
}

lusparon332's avatar
lusparon332 committed
{OneLineCmnt}  { 
  return (int)Tok.COMMENT;
}

{StringUp}  { 
  return (int)Tok.STRINGAP;
}

"{" { 
  //    COMMENT
  BEGIN(COMMENT);
  return (int)Tok.LONGCOMMENT;
}

<COMMENT> "}" { 
  //    INITIAL
  BEGIN(INITIAL);
  return (int)Tok.LONGCOMMENT;
}

<COMMENT>begin {
  
}

<COMMENT>end {
  
}

<COMMENT>cycle {
  
}

<COMMENT>{ID} {
  //  ID  
  idsInComment.Add(yytext);
}


":" { 
  return (int)Tok.COLON;
}

":=" { 
  return (int)Tok.ASSIGN;
}

";" { 
  return (int)Tok.SEMICOLON;
}

[^ \r\n] {
	LexError();
lusparon332's avatar
lusparon332 committed
	return 0; //  
}

%%

//        -      Scanner

public void LexError()
{
lusparon332's avatar
lusparon332 committed
	Console.WriteLine("({0},{1}):   {2}", yyline, yycol, yytext);
}

public string TokToString(Tok tok)
{
	switch (tok)
	{
		case Tok.ID:
			return tok + " " + yytext;
		case Tok.INUM:
			return tok + " " + LexValueInt;
		case Tok.RNUM:
			return tok + " " + LexValueDouble;
		default:
			return tok + "";
	}
}