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ć.
A możesz nam pokazać co do tej pory zrobiłaś?
na razie nie wiem jak stworzyc taki lexer.
Iiiii?
Mało materiałów w internecie czy po prostu nie chce Ci się czytać?
lexer można sobie wygenerować np. za pomocą antLR ale nie bardzo widzę po co tu lekser ;]
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 { }
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 { }