Java CC實現語法分析

#語義分析

  1. 設計MiniC的上下文無關文法,在“Parser.jjt”文件中表示該文法,生成調試遞歸下降分析程序,以便對任意輸入的符號串進行分析;
  2. 以文件流的形式讀入要分析的C語言程序;
  3. 如果輸入的源程序符合MiniC的語法規範,在文件中輸出語法樹。
    #MiniC語法
    〈程序〉 → int main() {〈語句塊〉* }
    〈語句塊〉 →〈語句〉| {〈語句塊〉* }
    〈語句〉 →〈順序語句〉|〈條件語句〉|〈循環語句〉
    〈順序語句〉→ [〈聲明語句〉| 〈賦值語句〉] ”;”
    〈聲明語句〉→ int ID,ID,…,ID //思考如何表示
    〈賦值語句〉→ ID =〈表達式〉
    〈條件語句〉→ if〈條件〉〈語句塊〉
    〈循環語句〉→ while〈條件〉〈語句塊〉
    〈條件〉 →(〈表達式〉〈關係符〉〈表達式〉)
    〈表達式〉 →〈表達式〉〈運算符〉〈表達式〉|(〈表達式〉)|ID|NUM
    // 也可以使用javacc自動生成的 表達式(Expression)的文法
    〈運算符〉 → +|-|*|/
    〈關係符〉 → <|<=|>|>=|==|!=
    #源代碼
    根據設計的MiniC語法添加詞法規則聲明,除去已有的聲明,我添加了關鍵字,界限符等內容
TOKEN : /* KEYWORDS */
{
  < MAIN : "main" >
| < INT : "int" >
| < VOID : "void" >
| < WHILE : "while" >
| < RETURN : "return" >
| < IF : "if" >
}

TOKEN : /*界符*/
{
  <LC : "{">
| <RC : "}">
| <LS : "[">
| <RS : "]">
| <LP : "(">
| <RP : ")">
| <SE : ",">
| <CO : ";">
| <UL : "_">
| <COL : ":">
}

TOKEN : /*運算符*/
{
  <EQUAL :"=">
| <PLUS :"+">
| <MINUS :"-">
| <TIMES :"*" >
| <DIVIDE :"/" >
}

在原有的表達式語法規則的基礎上,添加了程序、語句塊、語句等語法規則

void procedure():/*程序*/
{}
{
  < VOID >< MAIN >< LP >< RP >< LC >(statementlock())*< RC >
}

void statementlock():/*語句塊*/
{}
{
  statement()
| < LC >(statementlock())*< RC >
}

void statement():/*語句*/
{}
{
  sequence()
| condition()
| loop()
}

void sequence():/*順序語句*/
{}
{
  [declaration() | assignment()]< CO >
}

void declaration():/*聲明語句*/
{}
{
  < INT >Identifier() (< SE >Identifier())*
}

void assignment():/*賦值語句*/
{}
{
  Identifier() < EQUAL >Expression()
}

void condition():/*條件語句*/
{}
{
  < IF >term()statementlock()
}

void loop():/*循環語句*/
{}
{
  < WHILE >term()statementlock() 
}

void term():/*條件*/
{}
{
  < LP >Expression()operator()Expression()< RP >
}

void operator():/*運算符*/
{}
{
  "<"
| "<="
| ">"
| ">="
| "=="
| "!="
}

修改jjt文件中的main方法,使其從文件中讀取源程序代替控制檯輸入源程序

	System.out.println("Reading from standard input...");
	System.out.print("Enter an expression like \"1+(2+3)*var;\" :");
    File fp = new File("test.txt");
    InputStream in = null;
    try { 
    	in = new FileInputStream(fp);
    	new Parser(in);
    }catch(IOException e) {
		System.out.println("File open error!");
    }
    //new Parser(System.in);
    try
    {
      SimpleNode n = Parser.Start();
      n.dump("");
      System.out.println("Thank you.");
    }
    catch (Exception e)
    {
      System.out.println("Oops.");
      System.out.println(e.getMessage());
    }

在SimpleNode.java文件中修改dump()方法,使其將語法樹輸出至文件代替輸出至控制檯。運行程序後在out.txt文件中可以查看輸出的語法樹。

public void dump(String prefix) {
	try {
		File file = new File("out.txt");
		FileOutputStream out = new FileOutputStream(file, true);
		byte [] a = (toString(prefix)+"\r\n").getBytes();
		out.write(a);
		out.close();
//		out = new FileOutputStream();
		 if (children != null) {
		      for (int i = 0; i < children.length; ++i) {
		        SimpleNode n = (SimpleNode)children[i];
		        if (n != null) {
		          n.dump(prefix + " ");
		        }
		      }
		    }
	} catch (Exception e) {
		// TODO: handle exception
	}
	  
//    System.out.println(toString(prefix));
//    if (children != null) {
//      for (int i = 0; i < children.length; ++i) {
//        SimpleNode n = (SimpleNode)children[i];
//        if (n != null) {
//          n.dump(prefix + " ");
//        }
//      }
//    }
  }

#運行結果
左邊爲輸入的源程序,右邊爲在out.txt文件中輸出的生成樹。
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章