📄 compiler.java
字号:
/*******************************************************************************
* Compiler.
* New language which is called BASCAL.
* Date : 20/9/2006,Time : 2:41.
******************************************************************************/
package mk.ceit;
import java.util.StringTokenizer;
import java.util.Scanner;
import java.io.*;
import javax.swing.JOptionPane;
public class Compiler
{
private Scanner scan;
private StringTokenizer token;
private StringTokenizer temp;
private String code,tokens,error="",details="";
private int counter=0;
private Token t;
private int numberOfErrors=0;
/**************************************************************************/
/**The following constructor is used for sent the text of code into it.
/**************************************************************************/
public Compiler(String code)
{
this.code=code;
start();
newLine();
}
/**************************************************************************/
/**The following constructor is used for sent the file of code into it.
/**************************************************************************/
public Compiler(File fileName)throws IOException
{
code="";
Scanner scanner=new Scanner(fileName);
while(scanner.hasNextLine())
{
code+=scanner.nextLine()+"\n";
}
start();
newLine();
}
/**************************************************************************/
public void compile()
{
match("program","KeyWord");
match("","Identifier");
match(";","KeyWord");
declarations();
subprogram_declarations();
compound_statement();
match(".","KeyWord");
}
/**************************************************************************/
private void declarations()
{
if(t.getTokenName().equalsIgnoreCase("var"))
{
match("var","KeyWord");
identifier_list();
match(":","KeyWord");
type();
match(";","KeyWord");
declarations();
}
else{}
}
/**************************************************************************/
private void identifier_list()
{
match("","Identifier");
identifier_list2();
}
/**************************************************************************/
private void identifier_list2()
{
if(t.getTokenName().equals(","))
{
match("," ,"KeyWord");
match("" ,"Identifier");
identifier_list2();
}
else{}
}
/**************************************************************************/
private void type()
{
if(t.getTokenName().equalsIgnoreCase("array"))
{
match("array","KeyWord");
match("[" ,"KeyWord");
match("" ,"Numeric");
match(".." ,"KeyWord");
match("" ,"Numeric");
match("]" ,"KeyWord");
match("of" ,"KeyWord");
standard_type();
}
else
standard_type();
}
/**************************************************************************/
private void standard_type()
{
if(t.getTokenName().equalsIgnoreCase("integer")
||t.getTokenName().equalsIgnoreCase("real"))
match(t.getTokenName(),"KeyWord");
}
/**************************************************************************/
private void subprogram_declarations()
{
if(!t.getTokenName().equalsIgnoreCase("function")&&
!t.getTokenName().equalsIgnoreCase("procedure"))
{}
else
{
subprogram_declaration();
match(";","KeyWord");
subprogram_declarations();
}
}
/**************************************************************************/
private void subprogram_declaration()
{
subprogram_head();
declarations();
compound_statement();
}
/**************************************************************************/
private void subprogram_head()
{
if(t.getTokenName().equalsIgnoreCase("function"))
{
match("function","KeyWord");
match("" ,"Identifier");
arguments();
match(":","KeyWord");
standard_type();
match(";","KeyWord");
}
else
if(t.getTokenName().equalsIgnoreCase("procedure"))
{
match("procedure","KeyWord");
match("" ,"Identifier");
arguments();
match(";","KeyWord");
}
}
/**************************************************************************/
private void arguments()
{
if(t.getTokenName().equals("("))
{
match("(","KeyWord");
parameter_list();
match(")","KeyWord");
}
else
{}
}
/**************************************************************************/
private void parameter_list()
{
identifier_list();
match(":","KeyWord");
type();
parameter_list2();
}
/**************************************************************************/
private void parameter_list2()
{
if(t.getTokenName().equals(";"))
{
match(";","KeyWord");
identifier_list();
match(":","KeyWord");
type();
parameter_list2();
}
else
{}
}
/**************************************************************************/
private void compound_statement()
{
match("begin","KeyWord");
optional_statements();
match("end","KeyWord");
}
/**************************************************************************/
private void optional_statements()
{
if(!t.getTokenName().equalsIgnoreCase("end"))
if(!t.getTokenName().equalsIgnoreCase("begin")&&
!t.getTokenName().equalsIgnoreCase("if")&&
!t.getTokenName().equalsIgnoreCase("while")&&
(!t.getTokenName().matches("[a-zA-Z]\\w*")&&
!isKeyWord(t.getTokenName())))
{}
else
statement_list();
}
/**************************************************************************/
private void statement_list()
{
statement();
statement_list2();
}
/**************************************************************************/
private void statement()
{
if(t.getTokenName().equalsIgnoreCase("if"))
{
match("if","KeyWord");
expression();
match("then","KeyWord");
statement();
match("else","KeyWord");
statement();
}
else
if(t.getTokenName().equalsIgnoreCase("while"))
{
match("while","KeyWord");
expression();
match("do","KeyWord");
statement();
}
else
if(t.getTokenName().equalsIgnoreCase("begin"))
{
compound_statement();
}
else
if(t.getTokenName().matches("[a-zA-Z]\\w*"))
{
match("","Identifier");
statement2();
}
}
/**************************************************************************/
private void statement_list2()
{
if(t.getTokenName().equals(";"))
{
match(";","KeyWord");
statement();
statement_list2();
}
else
{}
}
/**************************************************************************/
private void statement2()
{
if(t.getTokenName().equals(":="))
{
match(":=","KeyWord");
expression();
}
else
if(t.getTokenName().equals("["))
{
match("[","KeyWord");
expression();
match("]","KeyWord");
match(":=","KeyWord");
expression();
}
else
if(t.getTokenName().equals("("))
{
match("(","KeyWord");
expression_list();
match(")","KeyWord");
}
else
{}
}
/**************************************************************************/
private void expression()
{
simple_expression();
expression2();
}
/**************************************************************************/
private void expression2()
{
if(t.getTokenName().equals(":=")||t.getTokenName().equals(">")||
t.getTokenName().equals("<")||t.getTokenName().equals(">=")||
t.getTokenName().equals("<=")||t.getTokenName().equals("<>"))
{
match(t.getTokenName(),"KeyWord");
simple_expression();
}
else
{}
}
/**************************************************************************/
private void expression_list()
{
expression();
expression_list2();
}
/**************************************************************************/
private void expression_list2()
{
if(t.getTokenName().equals(","))
{
match(",","KeyWord");
expression();
expression_list2();
}
else
{}
}
/**************************************************************************/
private void simple_expression()
{
if(t.getTokenName().equals("-")||t.getTokenName().equals("+"))
{
match(t.getTokenName(),"KeyWord");
term();
simple_expression2();
}
else
{term();simple_expression2();}
}
/**************************************************************************/
private void simple_expression2()
{
if(t.getTokenName().equals("-")||t.getTokenName().equals("+")||
t.getTokenName().equalsIgnoreCase("or"))
{
match(t.getTokenName(),"KeyWord");
addop();
term();
simple_expression2();
}
else
{}
}
/**************************************************************************/
private void sign()
{
if(t.getTokenName().equals("-")||
t.getTokenName().equals("+"))
{
match(t.getTokenName(),"KeyWord");
}
}
/**************************************************************************/
private void addop()
{
if(t.getTokenName().equals("-")||
t.getTokenName().equals("+")||
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -