2014-09-05 1 views
0

미안/RCocol/R 예측 파서 구현 자바

Here's 미안 지금 구문 분석을 시도 문법의 일부 :

enter image description here

제곱 된 대괄호 평균 1 또는 없음 중괄호는 0 이상을 의미합니다.

public void Cocol(){ 
    lookahead = in.next(); 
    if(lookahead.equals("COMPILER")){ 
     match("COMPILER"); 
     match("ident"); 
     ScannerSpecification(); 
     match("END"); 
     match("ident"); 
     match("."); 
    } 
    else{ 
     System.out.println("SYNTAX ERROR: \nCOMPILER expected, Found: "+lookahead); 
     try { 
      ArrayList<Integer> line = findLineOf(lookahead); 
      System.out.println("Line: "+line); 
     } catch (Exception ex) { 
      Logger.getLogger(ScannerCocol.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

} 

public void ScannerSpecification(){ 
    // 1 o mas veces “CHARACTERS” { SetDecl } 
    if(lookahead.equals("CHARACTERS")){ 
     match("CHARACTERS"); 
     // 0 or More SETDecl 

    } 
    if (lookahead.equals("KEYWORDS")){ 
     //PENDING....  
    } 

    if(WhiteSpaceDecl()){ 
      //PENDING.... 
    } 
    boolean res=match("."); 
    if(res==false){ 
     System.out.println("SYNTAX ERROR: \n \".\" expected, Found: "+lookahead); 

     //Encontrando linea 
     try { 
      ArrayList<Integer> line = findLineOf(lookahead); 
      System.out.println("Line: "+line); 
     } catch (Exception ex) { 
      Logger.getLogger(ScannerCocol.class.getName()).log(Level.SEVERE, null, ex); 
     } 


    } 

} 
public boolean match(String terminal){ 
    boolean result; 
    if(terminal.equals("number")){ 
     result = automataNumber.simularAFN(lookahead, automataNumber.inicial, conjuntoSimbolos); 
     return result; 
    } 
    else if(terminal.equals("ident")){ 
     result = automataident.simularAFN(lookahead,automataident.inicial,conjuntoSimbolos); 
     return result; 
    } 
    else if(terminal.equals("string")){ 
     result = automataString.simularAFN(lookahead,automataString.inicial,conjuntoSimbolos); 
     return result; 
    } 
    else if(terminal.equals("char")){ 
     result = automataChar.simularAFN(lookahead,automataChar.inicial,conjuntoSimbolos); 
     return result; 
    } 
    else{ 
     if(this.lookahead.equals(terminal)){ 
      lookahead= in.next(); 
      return true; 
     } 
     else{ 
      System.out.println("Error: Se esperaba: "+terminal); 
      return false; 
     } 
    } 

문제 미안 베란다는 I합니다 (SetDecl에 예를 들어) 제조 0 이상의 유도를 검색해야하는 경우에있다. 필자가 필적 할 수 없을 때까지 계속 프로덕션을 일치시켜야한다는 것을 알고 있지만 입력을 계속 읽어야 할 때 오류를보고해야 할 때 어떻게 식별해야하는지 알지 못합니다.

누구든지 나에게 아이디어를 줄 수 있습니까?

답변

1

예측 구문 분석기의 간단한 모델은 프로덕션에 해당하는 각 함수가 부울 값 (성공 또는 실패)을 반환한다는 것입니다. 그런 다음 "0 개 이상"확장을 포함하는 규칙 예를 들어, 기록 할 수 있습니다

if(lookahead.matches("CHARACTERS")){ 
    while (SetDecl()) ; 
    return true; 
} 
/* ... */ 

: 그건 그렇고

if(lookahead.equals("CHARACTERS")){ 
    match("CHARACTERS"); 
    while (SetDecl()) ; 
    return true; 
} 
/* ... */ 

, 그것은 내다 문자열을 소모 연산자를 정의하기 위해 실제로 편리합니다

1) 전나무 경우 : lookahead.matches(s)는 기본적으로 어디 if(lookahead.equals(s){match(s); return true;}else{return false;}

모든 생산 함수에 대한 일반적인 규칙은 이것이다 규칙 (토큰 또는 비 - 터미널)의 객체가 일치 할 수 없으면 입력을 그대로두고 false를 반환합니다.

2) 규칙의 다른 개체를 찾을 수 없으면 오류를 내고 복구를 시도하십시오.

+0

안녕하세요. 네가 준 규칙이 내가 필요한 것일 뿐이라고 생각해. 이 권리를 얻는 지 알고 싶습니다. 중괄호 사이에 터미널이없는 경우 첫 번째 개체를 확인하고 일치하는지 확인해야합니다. 오류를보고하지 않고 입력하십시오. 첫 번째가 아닌 다른 개체에서 오류가 발견되면 어떻게합니까? while 루프를 확인하는 중입니까? 나는 그것이보고없이 계속되어야한다고 생각하거나 나는 틀렸어? –

+0

@PabloEstrada : 100 % 맞음. while 루프는 0 회 이상의 반복을 나타내고 0 회 반복은 항상 일치 할 수 있기 때문에 항상 성공합니다. "하나 이상의 X"의 경우 "if (! X()) false를 반환하고 while (X());를 true로 반환하는"X [X] "를 효과적으로 수행합니다. – rici