Java的注释、关键字和标识符

注释

Java中的注释类型:

  1. 单行注释 格式://
  2. 多行注释 格式:/* */
    注意:多行注释不能嵌套使用,如果出现两对多行注释,那么前面三个符号会变绿,最后一个是黑色的
  3. 文档注释(Java特有)
    格式:
    /**
    @author 指定Java程序的作者
    @version 指定源文件的版本
    */
    其中文档注释的内容可以被JDK提供的工具Javadoc所解析,生成一套以网页文件形式体现的该程序的说明文档
    如果在DOS命令窗口,具体命令为java文件的上一层目录+javadoc -d mydoc -author -version +java文件名
    运行完后生成一个文件夹,进去找到index.html文件双击打开即可看到效果

关键字(keyword)的定义和特点

定义:被Java语言赋予了特殊含义,用做专门用途的字符串(单词)
特点:关键字中所有字母都为小写
在这里插入图片描述
在这里插入图片描述

标识符:

Java对各种变量、方法和类等要素命名时使用的字符序列称为标识符
技巧:凡是自己可以起名字的地方都叫标识符

定义合法标识符规则:
  1. 由26个英文字母大小写,0-9,_或$组成
  2. 数字不可以开头
  3. 不可以使用关键字和保留字,但能包含关键字和保留字
  4. Java中严格区分大小写,长度无限制
  5. 标识符不能包含空格

变量的概念:

  1. 内存中的一个存储区域
  2. 该区域的数据可以在同一类型范围内不断变化
  3. 变量是程序中最基本的存储单元。包含变量类型、变量名和存储的值
变量的作用:用于在内存中保存数据
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的基于正则表达式的词法分析程序,可以分析关键字标识符、数字、界符和运算符: ```java import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; public class LexicalAnalyzer { public static void main(String[] args) { // 输入的代码 String code = "int a = 123;\n" + "if (a > 0) {\n" + " System.out.println(\"Hello, world!\");\n" + "}"; // 关键字集合 Set<String> keywords = new HashSet<>(Arrays.asList( "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for", "if", "goto", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while" )); // 界符集合 Set<Character> delimiters = new HashSet<>(Arrays.asList( '(', ')', '{', '}', ';' )); // 运算符集合 Set<String> operators = new HashSet<>(Arrays.asList( "+", "-", "*", "/", "%", "++", "--", "==", "!=", ">", "<", ">=", "<=", "&&", "||", "!", "&", "|", "^", "~", "<<", ">>", ">>>", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "<<=", ">>=", ">>>=" )); // 匹配标识符的正则表达式 String idPatternStr = "[a-zA-Z_$][a-zA-Z0-9_$]*"; // 匹配数字的正则表达式 String numPatternStr = "\\d+(\\.\\d+)?([eE][+-]?\\d+)?"; // 分行 String[] lines = code.split("\\r?\\n"); // 逐行分析 for (int i = 0; i < lines.length; i++) { String line = lines[i]; int pos = 0; // 逐个字符分析 while (pos < line.length()) { char ch = line.charAt(pos); // 跳过空白字符 if (Character.isWhitespace(ch)) { pos++; continue; } // 匹配标识符 Pattern idPattern = Pattern.compile(idPatternStr); Matcher idMatcher = idPattern.matcher(line.substring(pos)); if (idMatcher.lookingAt()) { String id = idMatcher.group(); if (keywords.contains(id)) { System.out.printf("Line %d: Keyword %s\n", i + 1, id); } else { System.out.printf("Line %d: Identifier %s\n", i + 1, id); } pos += id.length(); continue; } // 匹配数字 Pattern numPattern = Pattern.compile(numPatternStr); Matcher numMatcher = numPattern.matcher(line.substring(pos)); if (numMatcher.lookingAt()) { String num = numMatcher.group(); System.out.printf("Line %d: Number %s\n", i + 1, num); pos += num.length(); continue; } // 匹配界符和运算符 if (delimiters.contains(ch)) { System.out.printf("Line %d: Delimiter %s\n", i + 1, ch); pos++; } else { for (int len = Math.min(operators.size(), line.length() - pos); len > 0; len--) { String op = line.substring(pos, pos + len); if (operators.contains(op)) { System.out.printf("Line %d: Operator %s\n", i + 1, op); pos += len; break; } } } } } } } ``` 在这个程序中,我们先把代码按行分割,然后逐行进行词法分析。对于每一行,我们从左到右依次扫描字符,使用正则表达式匹配标识符和数字,使用集合匹配关键字、界符和运算符。 这个程序只是一个基本的示例,实际的词法分析程序要更复杂,需要考虑更多的语法规则,比如注释、字符串等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值