欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

javacc案例之统计字符

程序员文章站 2022-05-01 12:09:31
...

1.美图

javacc案例之统计字符

2.背景

Count.jj 文件内容

PARSER_BEGIN(NL_Xlator)

/** New line translator. */
public class NL_Xlator {

  /** Main entry point. */
  public static void main(String args[]) throws ParseException {
    NL_Xlator parser = new NL_Xlator(System.in);
    parser.ExpressionList();
  }

}

PARSER_END(NL_Xlator)

SKIP :
{
  " "
| "\t"
| "\n"
| "\r"
}

TOKEN:{
      <ID : ["a"-"z","A"-"Z","_"](["a"-"z","A"-"Z","_","0"-"9"])*>
    | <NUM : (["0"-"9"])+>
}

/** Top level production. */
void ExpressionList() :
{
	String s;
}
{
	{
	  System.out.println("Please type in an expression followed by a \";\" or ^D to quit:");
	  System.out.println("");
	}
  ( s=Expression() ";"
	{
	  System.out.println(s);
	  System.out.println("");
	  System.out.println("Please type in another expression followed by a \";\" or ^D to quit:");
	  System.out.println("");
	}
  )*
  <EOF>
}

/** An Expression. */
String Expression() :
{
	java.util.Vector termimage = new java.util.Vector();
	String s;
}
{
  s=Term()
	{
	  termimage.addElement(s);
	}
  ( "+" s=Term()
	{
	  termimage.addElement(s);
	}
  )*
	{
	  if (termimage.size() == 1) {
	    return (String)termimage.elementAt(0);
          } else {
            s = "the sum of " + (String)termimage.elementAt(0);
	    for (int i = 1; i < termimage.size()-1; i++) {
	      s += ", " + (String)termimage.elementAt(i);
	    }
	    if (termimage.size() > 2) {
	      s += ",";
	    }
	    s += " and " + (String)termimage.elementAt(termimage.size()-1);
            return s;
          }
	}
}

/** A Term. */
String Term() :
{
	java.util.Vector factorimage = new java.util.Vector();
	String s;
}
{
  s=Factor()
	{
	  factorimage.addElement(s);
	}
  ( "*" s=Factor()
	{
	  factorimage.addElement(s);
	}
  )*
	{
	  if (factorimage.size() == 1) {
	    return (String)factorimage.elementAt(0);
          } else {
            s = "the product of " + (String)factorimage.elementAt(0);
	    for (int i = 1; i < factorimage.size()-1; i++) {
	      s += ", " + (String)factorimage.elementAt(i);
	    }
	    if (factorimage.size() > 2) {
	      s += ",";
	    }
	    s += " and " + (String)factorimage.elementAt(factorimage.size()-1);
            return s;
          }
	}
}

/** A Factor. */
String Factor() :
{
	Token t;
	String s;
}
{
  t=<ID>
	{
	  return t.image;
	}
|
  t=<NUM>
	{
	  return t.image;
	}
|
  "(" s=Expression() ")"
	{
	  return s;
	}
}

但是运行报错

(base) aaa@qq.com count$ javacc Count.jj 
Java Compiler Compiler Version 5.0 (Parser Generator)
(type "javacc" with no arguments for help)
Reading from file Count.jj . . .
File "TokenMgrError.java" does not exist.  Will create one.
File "ParseException.java" does not exist.  Will create one.
File "Token.java" does not exist.  Will create one.
File "SimpleCharStream.java" does not exist.  Will create one.
Parser generated successfully.
(base) aaa@qq.com count$ javac *.java
注: NL_Xlator.java使用了未经检查或不安全的操作。
注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
(base) aaa@qq.com count$ java NL_Xlator 
Please type in an expression followed by a ";" or ^D to quit:

aes+123agd123abd+ssseeee;
Exception in thread "main" ParseException: Encountered " <ID> "agd123abd "" at line 1, column 8.
Was expecting one of:
    ";" ...
    "+" ...
    "*" ...
    
        at NL_Xlator.generateParseException(NL_Xlator.java:316)
        at NL_Xlator.jj_consume_token(NL_Xlator.java:254)
        at NL_Xlator.ExpressionList(NL_Xlator.java:29)
        at NL_Xlator.main(NL_Xlator.java:8)
(base) aaa@qq.com count$ java NL_Xlator 
Please type in an expression followed by a ";" or ^D to quit:

aes+123agd123abd+ssseeee;
Exception in thread "main" ParseException: Encountered " <ID> "agd123abd "" at line 1, column 8.
Was expecting one of:
    ";" ...
    "+" ...
    "*" ...
    
        at NL_Xlator.generateParseException(NL_Xlator.java:316)
        at NL_Xlator.jj_consume_token(NL_Xlator.java:254)
        at NL_Xlator.ExpressionList(NL_Xlator.java:29)
        at NL_Xlator.main(NL_Xlator.java:8)
(base) aaa@qq.com count$