0

이것은 우리 교수가 작성한 튜토리얼에서 나왔다. 파생물을 만들 수는 있지만 파생어를 분석하여 문법을 만들 수는 없습니다..그 밖의 이유로 명확한 문법을 ​​만들지는 못한다

여기서 "일치"란 무엇입니까?

matched_if, matched_stmt, unmatched_if가 어떻게 간단한 단어로 작동하는지 설명 할 수 있습니까 :)?

The following is an unambiguous grammar for the problem: 

stmt → if_stmt | nonif_stmt 
if_stmt → matched_if | unmatched_if 
matched_if → 'if' logical_expr 'then' matched_stmt 'else' matched_stmt 
matched_stmt → mathced_if | nonif_stmt 
unmatched_if → 'if' logical_expr 'then' stmt 
| 'if' logical_expr 'then' matched_stmt 'else' unmatched_if 
logical_expr → id '==' lit 
nonif_stmt → assgn_stmt 
assgn_stmt → id '=' expr 
expr → expr '+' term | term 
term → '(' expr ')' | id 
id → 'A' | 'B' | 'C' 
lit → '0' | '1' | '2' 


Consider the following input: 
if A == 0 then 
    if B == 1 then 
    C = A + B 
    else 
    B = C 

Let us do a leftmost derivation for the input: 

stmt 
=> if_stmt 
=> unmatched_if 
=> 'if' logical_expr 'then' stmt 
=> 'if' id '==' lit 'then' stmt 
=> 'if' 'A' '==' lit 'then' stmt 
=> 'if' 'A' '==' '0' 'then' stmt 
=> 'if' 'A' '==' '0' 'then' if_stmt 
=> 'if' 'A' '==' '0' 'then' matched_if 
=> 'if' 'A' '==' '0' 'then' 'if' logical_expr 'then' matched_stmt 'else' matched_stmt 
=> 'if' 'A' '==' '0' 'then' 'if' id '==' lit 'then' matched_stmt 'else' matched_stmt 
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' lit 'then' matched_stmt 'else' matched_stmt 
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' matched_stmt 'else' matched_stmt 
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' nonif_stmt 'else' matched_stmt 
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' assgn_stmt 'else' matched_stmt 
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' id '=' expr 'else' matched_stmt 
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' expr '+' term 'else' matched_stmt 
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' term '+' term 'else' matched_stmt 
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' id '+' term 'else' matched_stmt 
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' 'A' + term 'else' matched_stmt 
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' 'A' + 'B' 'else' matched_stmt 
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' 'A' + 'B' 'else' nonif_stmt 
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' 'A' + 'B' 'else' assgn_stmt 
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' 'A' + 'B' 'else' id '=' expr 
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' 'A' + 'B' 'else' 'B' '=' expr 
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' 'A' + 'B' 'else' 'B' '=' term 
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' 'A' + 'B' 'else' 'B' '=' id 
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' 'A' + 'B' 'else' 'B' '=' 'C' 

답변

2

가 "일치"는 모든 thenelse 일치된다는 것을 의미한다. if 문이 일치 else을하는

그것은 필요는 없습니다 만, 그렇지 않은 경우 그 일부 else)이 외부 then 대신 내부 then의 일치하는 것을 의미하는 것이기 때문에, 그것은 일치 if 문 안쪽이 될 수 없습니다 .

모든 문법이 위의 내용을 공식화합니다.

비슷한 문제는 후행 닫는 괄호를 생략 할 수있는 추가 규칙을 사용하여 일반 산술 연산 식의 문법 작성입니다. 예를 들어, (1+2*(1+2*(1+2이라고 쓸 수 있습니다.이 언어는 명확하게 모호하지만 문법을 작성할 때 불일치 한 괄호와 일치하지 않는 괄호가 포함 된 표현식을 처리해야합니다. 그것은 "일치"라는 단어의 동일한 용도입니다 (그리고 그 해결책은 다소 비슷할 것입니다).