首页 > 解决方案 > 如何评论以“//”开头的字符串?

问题描述

这些天我正在学习编译器的构造,并且在编写代码以在其中进行注释时遇到了麻烦。实际发生的情况是,每当我在记事本文件中写入字符串时,例如 Hello //World. 然后它打印“/”这个我不想要的 div 运算符。实际上我想要的是 Hello 应该打印在输出中并且 World 应该得到评论。我知道我已经包含了 div 运算符的代码,但也有必要包含。只是想知道如何在检查检查 div 运算符的逻辑的同时实现此评论逻辑。

这是代码!

import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;

public class main {
    
    public static void main(String[] args) throws FileNotFoundException{
        
        File newFile = new File("C:/temp/sourcecode.txt");
        Scanner scanFile = new Scanner(newFile);
        //Scanner scan = new Scanner(System.in);
        
        char ch;
        String str;
        
        
        
        while(scanFile.hasNextLine()){
        str = scanFile.nextLine();
        int l = str.length();
        if(!str.startsWith("//") && !str.startsWith("/*") && !str.endsWith("*/")) {
        for(int i =0; i<l ; i++) {
            ch = str.charAt(i);
            
            System.out.println(ch);
                
            if(ch == '*'){
                System.out.println("The Operator is MUL");
                System.out.println("arop\n");
            }
            if(ch == '/')
            {
                 System.out.println("The Operator is DIV");
                 System.out.println("arop\n");
            }
            

            
        }
        }
       
        
            int OP = 0;
            
            switch(OP){
                case 0: 
                    if(str.contains("<") && str.contains(">")){
                        System.out.println("The Operator is NE");
                        System.out.println("relop\n");
                        break;
                    }
                case 1: 
                    if(str.contains("<") && str.contains("=")){
                        System.out.println("The Operator is LE");
                        System.out.println("relop\n");
                        break;
                    }
                    
                case 2: 
                    if(str.contains(">") && str.contains("=")){
                        System.out.println("The Operator is GE");
                        System.out.println("relop\n");
                        break;
                    }
                    
                case 3: 
                    if(str.contains("<")){
                        System.out.println("The Operator is LT");
                        System.out.println("relop\n");
                        break;
                    }
                case 4: 
                    if(str.contains(">")){
                        System.out.println("The Operator is GT");
                        System.out.println("relop\n");
                        break;
                    }
                case 5: 
                    if(str.contains("==")){
                        System.out.println("The Operator is EQ");
                        System.out.println("relop\n");
                        break;
                    }
                case 6: 
                    if(str.contains("+")){
                        System.out.println("The Operator is ADD");
                        System.out.println("arop\n");
                        break;
                    }
                case 7: 
                    if(str.contains("-")){
                        System.out.println("The Operator is SUB");
                        System.out.println("arop\n");
                        break;
                    }
//                case 8: 
//                    if(str.contains("*")){
//                        System.out.println("The Operator is MUL");
//                        System.out.println("arop\n");
//                        break;
//                    }
//                case 9: 
//                    if(str.contains("/")){
//                        System.out.println("The Operator is DIV");
//                        System.out.println("arop\n");
//                        break;
//                    }
                case 10: 
                    if(str.contains("=")){
                        System.out.println("The Operator is ASN");
                        System.out.println("otop\n");
                        break;
                    }
                case 11: 
                    if(str.contains("'")){
                        System.out.println("The Operator is PRN");
                        System.out.println("otop\n");
                        break;
                    }
                case 12: 
                    if(str.contains(";")){
                        System.out.println("The Operator is LTRN");
                        System.out.println("otop\n");
                        break;
                    }
                case 13: 
                    if(str.contains("{")){
                        System.out.println("The Operator is LBRC");
                        System.out.println("otop\n");
                        break;
                    }
                case 14: 
                    if(str.contains("}")){
                        System.out.println("The Operator is RBRC");
                        System.out.println("otop\n");
                        break;
                    }      
            }
        }
    }
    }

先感谢您!

标签: javacompiler-construction

解决方案


在编写编译器时,代码中的不同输入词称为标记,识别每个标记作用的阶段称为词法分析阶段。

当试图识别标记时,通常使用的是正则表达式,它是一种实现有限自动机的方法。

您可以在此处更详细地了解它:

https://en.wikipedia.org/wiki/Lexical_analysis

您应该替换 contains 的用法,并使用词法分析器,它是进行词法分析的工具的名称。它使用正则表达式,因为它不仅仅是/and //,在许多不同的情况下,您的编译器需要决定选择哪个标记。

这是一个用于识别不同标记的有限自动化示例,请注意,对于每个前缀,可能的标记可以有许多选项:

在此处输入图像描述

在 Java 中,您可以使用 jflex 来生成带有标记定义的词法分析器代码。


推荐阅读