根据PASCAL语言的说明语句结构,设计一个对PASCAL语言常量说明语句进行词法分析的简单程序,并用C、C++、Java或Python语言编程实现。要求程序能够对从键盘输入或从文件读入的形如“const count = 10, sum = 81.5, char1 = 'f', string1 = "abcds f 89h", max = 169;”的字符串进行分析处理,如果是,识别出该输入串中所说明的各种常量名、常量类型及常量值,以二元组的形式输出所有常量,并统计各种类型的常量个数。
代码实现如下:
public class exercise {
static String name;
static String type;
static String errormessage;//错误信息
static String value;
static int intnum = 0;
static int floatnum = 0;
static int charnum = 0;
static int stringnum = 0;
static int correctname;//0 = error,1 = correct
static int correctvalue;//0 = error,1 = correct
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String s = in.nextLine();
s = s.trim();
boolean re = s.startsWith("const") && s.endsWith(";");
while(!re)
{
System.out.println("It is not a constant declaration statement!");
System.out.println("Please input a string again!");
s = in.nextLine();
s = s.trim();
re = s.startsWith("const") && s.endsWith(";");
}
Output(s);
in.close();
}
//判断常量名
public static int checkname(char[] a,int i)
{
name = "";
while(a[i] != '=') {
name += a[i];
i++;
}
name = name.trim();
String regex = "[a-zA-Z][a-zA-Z0-9_]*";
boolean re = name.matches(regex);//检测字符串是否匹配给定正则表达式,匹配返回真,否则返回假
if(re)
{
correctname = 1;
}
else
{
correctname = 0;
}
System.out.println(i);
return i;
}
//判断类型
public static int checktype(char a[],int i)
{
errormessage = "";
value = "";
while(a[i] != ',' && a[i] != ';') {
value += a[i];
i++;
}
value = value.trim();
String regexr1 = "[+|-]?[0-9]*";
boolean va = value.matches(regexr1);//检测字符串是否匹配给定正则表达式,匹配返回真,否则返回假
if(correctname == 1)
{
String str = value;
if(va) {//判断整数
if(value.charAt(0)=='+' || value.charAt(0)=='-')//检索value是否以+或—开头,即正数或负数
{
str = value.substring(1);//从下标为1开始截取后面字符串
}
if(str.equals("0") || str.matches("[1-9][0-9]*"))//判断整数是否合法
{
correctvalue = 1;
type = "integer";//整数
intnum++;
}else {
errormessage = "Wrong!The integer can't be started with '0'.";//报错,整数不可以0开头
correctvalue = 0;
}
}
else if(value.matches("[+|-]?[0-9]*[.][0-9]+")||value.matches("[+|-]?[0-9]*[.][0-9]+e[+|-]?[0-9]*")) {//判断浮点数
correctvalue = 1;
type = "float";
floatnum++;
}
else if(value.startsWith("\'")&&value.endsWith("\'")) {//判断字符型
if(value.length()==3) {
correctvalue = 1;
type = "char";
charnum++;
}else {
errormessage += "Wrong!There are more than one char in ''.";
correctvalue = 0;
}
}
else if(value.startsWith("\"")&&value.endsWith("\"")) {
correctvalue = 1;
type = "String";
stringnum++;
}
else {
correctvalue = 0;
errormessage += "Wrong!The format of the value string is not correct!";
}
}
return i;
}
static void Output(String s) {
// TODO Auto-generated method stub
char str[] = s.toCharArray();
int i = 5;
while(i<str.length - 1) {
i = checkname(str,i);
i = checktype(str,i+1)+1;
if(correctname == 1) {
if(correctvalue==1) {
System.out.println(name+"("+type+","+value+")");//输入合法
}
else {
System.out.println(name+"("+errormessage+")");//名对值错
}
}
else {
System.out.println("("+name+","+"Wrong!It is not a identifier!"+")");
}
}
System.out.println("int_num="+intnum+"; float_num="+floatnum+"; char_num="+charnum+"; string_num="+stringnum+".");
}
}