* 编译原理词法分析:控制台输入方式
* @author fei
*
*/
public class Test2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
sc.useDelimiter("\n");
String tr=sc.next();
char[] ch=tr.toCharArray();
compile cp=new compile();
char s=0;
StringBuffer token=new StringBuffer();
for(int i=0;i<ch.length;i++){
if(ch[i]>='a'&&ch[i]<='z'){
token.append(ch[i]);
}
if(ch[i]>=0&&ch[i]<=9){
token.append(ch[i]);
}
if((!cp.IsLetter(ch[i])&&!cp.IsNumber(ch[i]))||ch[i]==' '){
if(token.length()>0){
System.out.print("('1','"+token.toString()+"')");
token.delete(0, token.length());
}
}
if(ch[i]=='+'){
System.out.println("('4','"+ch[i]+"')");
}
if(ch[i]=='-'){
System.out.println("('4','"+ch[i]+"')");
}
if(ch[i]=='*'){
System.out.println("('4','"+ch[i]+"')");
}
if(ch[i]=='/'){
System.out.println("('4','"+ch[i]+"')");
}
if(ch[i]=='>'||ch[i]=='<'||ch[i]=='!'){
s=ch[i];
}
if(ch[i]=='='){
if(s!=0&&(s=='>'||s=='<'||s=='!')){
System.out.println("('4','"+s+ch[i]+"')");
s=0;
}else{
System.out.println("('4','"+ch[i]+"')");
}
}
if(ch[i]=='('){
System.out.println("('5' '"+ch[i]+"')");
}
if(ch[i]==')'){
System.out.println("('5' '"+ch[i]+"')");
}
if(ch[i]=='{'){
System.out.println("('5' '"+ch[i]+"')");
}
if(ch[i]=='}'){
System.out.println("('5' '"+ch[i]+"')");
}
if(ch[i]==';'){
System.out.println("('5' '"+ch[i]+"')");
}
if(ch[i]==','){
System.out.println("('5' '"+ch[i]+"')");
}
}
}
}
class compile{
int code;
String[] keys={"if","int","for","while","do","return","break","continue"};
StringBuffer token=new StringBuffer();
public void concat(char ch){
token.append(ch);
}
public int Reserve(){
for(int i=0;i<keys.length;i++){
if(token.toString().equals(keys[i]))
return 1;
}
if(token.length()!=0){
if(token.charAt(0)>='0'&&token.charAt(0)<='9'){
return 3;
}
}
return 2;
}
public void Retrat(){
code=Reserve();
if(code==1){
System.out.println("('"+1+"','"+token+"')");
}else if(code==2){
System.out.println("('"+2+"','"+token.toString()+"')");
}else if(code==3){
System.out.println("('"+3+"','"+token+"')");
}
token.delete(0, token.length());
}
public boolean IsLetter(char ch){
if(ch>='a'&&ch<='z'){
return true;
}
return false;
}
public boolean IsNumber(char ch){
if(ch>='0'&&ch<='9'){
return true;
}
return false;
}
}
* 编译原理词法分析:读取文件方式
* @author fei
*
*/
public class Test4 {
public StringBuffer token = new StringBuffer();
String[] keys={"if","int","for","while","do","return","break","continue","main"};
public boolean IsLetter(int ch){
if((ch>=65 && ch <= 90) || (ch >= 97 && ch <=122)){
return true;
}
return false;
}
public boolean IsNumber(int ch){
if(ch>=48 && ch <= 57){
return true;
}
return false;
}
public boolean IsEmpty(int ch){
if(ch == 32){
return true;
}
return false;
}
public void concat(char ch){
token.append(ch);
}
public int Reserve(){
for(int i=0;i<keys.length;i++){
if(token.toString().equals(keys[i]))
return 1;
}
if(token.length()!=0){
if(token.charAt(0)>='0'&&token.charAt(0)<='9'){
return 3;
}
}
return 2;
}
public void Retrat(int code){
code=Reserve();
if(code==1){
System.out.println("('"+1+"','"+token+"')");
}else if(code==2){
System.out.println("('"+2+"','"+token.toString()+"')");
}else if(code==3){
System.out.println("('"+3+"','"+token+"')");
}
token.delete(0, token.length());
}
public void scanner() throws IOException{
BufferedReader br = new BufferedReader(new FileReader("tests.txt"));
int ch;
char s1=0;
while((ch =br.read()) != -1){
char s=(char)ch;
if(IsLetter(s)){
concat(s);
}
if(IsNumber(s)){
concat(s);
}
if((!IsLetter(s)&&!IsNumber(s))||IsEmpty(s)){
if(token.length()>0){
Retrat(Reserve());
}
}
if(s=='+'){
System.out.println("('4','"+s+"')");
}
if(s=='-'){
System.out.println("('4','"+s+"')");
}
if(s=='*'){
System.out.println("('4','"+s+"')");
}
if(s=='/'){
System.out.println("('4','"+s+"')");
}
if(s=='>'||s=='<'||s=='!'){
s1=s;
}
if(s=='='){
if(s1!=0&&(s1=='>'||s1=='<'||s1=='!')){
System.out.println("('4','"+s1+s+"')");
s1=0;
}else{
System.out.println("('4','"+s+"')");
}
}
if(s=='('){
System.out.println("('5' '"+s+"')");
}
if(s==')'){
System.out.println("('5' '"+s+"')");
}
if(s=='{'){
System.out.println("('5' '"+s+"')");
}
if(s=='}'){
System.out.println("('5' '"+s+"')");
}
if(s==';'){
System.out.println("('5' '"+s+"')");
}
if(s==','){
System.out.println("('5' '"+s+"')");
}
}
}
public static void main(String[] args) throws IOException {
Test4 t=new Test4();
t.scanner();
}
}