Lexer java

0

Cześć.Potrzebuję pomocy mam napisać wygenerować Lexera w javie, który z pliku tekstowego wyznaczy największą liczbę całkowitą oraz średnią wartość wszystkich ułamków. Nie wiem jak się za to zabrać.(jak stworzyć Lexera i go później uruchomić.

0

A możesz nam pokazać co do tej pory zrobiłaś?

0

na razie nie wiem jak stworzyc taki lexer.

0

Iiiii?
Mało materiałów w internecie czy po prostu nie chce Ci się czytać?

0

lexer można sobie wygenerować np. za pomocą antLR ale nie bardzo widzę po co tu lekser ;]

0

na razie udalo mi sie sumowac liczby
class Main
{
public static void main(String args[])
{
MyLexer scanner = null;
try
{
scanner = new MyLexer( new java.io.FileReader("src/lexer/SUMOWANIE/text.txt") );
int suma = 0;
Yytoken t = null;
do
{
t = scanner.yylex();
if (t!=null)
{
int w = Integer.parseInt(t.toString());
suma = suma + w;
}
}
while (t!=null);

      System.out.println("Suma="+suma);
                   
    }        
    catch (Exception e) 
    {
      System.out.println("Blad:");
      e.printStackTrace();
    }        
}

}

class Yytoken
{
public String m_text;

Yytoken (String text)
{
m_text = new String(text);
}

public String toString()
{
return m_text;
}
}

%%

%public

%class MyLexer

INTEGER=("+"|-)?[0-9]+

%%

{INTEGER} { return new Yytoken(yytext());}
.|\n { }

0

na razie udało mi się sumować liczby całkowite a jak mam z tego sumować tylko liczby np 2.5

class Main
{
    public static void main(String args[])
    {
        MyLexer scanner = null;
        try 
        {
          scanner = new MyLexer( new java.io.FileReader("src/lexer/SUMOWANIE/text.txt") );
          int suma = 0;
          Yytoken t = null;
          do 
          {
             t = scanner.yylex();           
             if (t!=null)
             { 
             	int w = Integer.parseInt(t.toString());
             	suma = suma + w;              
             }         
          } 
          while (t!=null);         

          System.out.println("Suma="+suma);
                       
        }        
        catch (Exception e) 
        {
          System.out.println("Blad:");
          e.printStackTrace();
        }        
    }
}

class Yytoken 
{
  public String m_text;
  
  Yytoken (String text)
  {
    m_text = new String(text);
  }

  public String toString() 
  {
    return m_text;
  }
}


%%

%public

%class MyLexer


INTEGER=("+"|-)?[0-9]+

%% 

{INTEGER} 	{ return new Yytoken(yytext());}	
.|\n 		{  }

1 użytkowników online, w tym zalogowanych: 0, gości: 1