문제

I am pretty new to lex and yacc. I was trying out a grammar that creates matrices and performs some operations like adition,multiplication etc.

Here is the grammar I am using

 program:     block ENDS
 block:       stmt | block ENDS stmt
 stmt:        defn_stmt | init_stmt | print_stmt | oper_stmt
 defn_stmt:   DEF definitions
 definitions: MATID OPENSQ NUMBER COMMA NUMBER CLOSEQ ENDS | 
              definitions COMMA MATID OPENSQ NUMBER COMMA NUMBER CLOSEQ ENDS
 init_stmt:   INIT MATID [mat_contents]
 mat_contents: NUMBER | mat_contents COMMA mat_contents | mat_contents RANGE mat_contents
 print_stmt:  PRINT MATID
 oper_stmt:   MATID mat_operator inputs | MATID CMUL inputs
 mat_operator: ADD | MMUL
 inputs: MATID COMMA MATID

But for the following input am getting syntax error at second DEF.

 DEF A [2,3]; // defines a 2X3 Matrix A.
 DEF I [2,3]; // defines a 2X3 Matrix A.
 DEF BA[2,3],C[2,3], D[3,4], E[2,4];

ALso while running yacc its showing conflicts: 4 shift/reduce.

What causes the syntaxerror here? How can I correct it?

도움이 되었습니까?

해결책

definitions must end with ENDS (;, apparently), which means that defn_stmt must end with ;. However, a stmt must always be followed by a ;. So the grammar insists that a definition be followed by two semicolons.

You should remove the ENDS from the defn_stmt productions. Also, you might want to factor out the two occurrences of MATID OPENSQ NUMBER COMMA NUMBER CLOSEQ; that's not the cause of your problems, though.

One obvious ambiguity (actually, two) is in the productions:

mat_contents: NUMBER
            | mat_contents COMMA mat_contents
            | mat_contents RANGE mat_contents

since it is (exponentially) ambiguous how to parse:

NUMBER COMMA NUMBER COMMA NUMBER RANGE NUMBER COMMA NUMBER

You could solve this with precedence declarations, or you could be more precise in your grammar.

다른 팁

should be something like this, Not verified.

program:       block ; 
block:         stmt | block ; stmt 
stmt:          defn_stmt | init_stmt | print_stmt | oper_stmt 
defn_stmt:     DEF   definitions
definitions:   MATID  [NUMBER,NUMBER]  |  definitions,  MATID [NUMBER,NUMBER]
init_stmt:     INIT MATID [mat_contents] 
mat_contents:  NUMBER |  mat_contents ,  mat_contents |  mat_contents :  mat_contents 
print_stmt:    PRINT MATID 
oper_stmt:     MATID mat_operator inputs |  MATID CMUL  inputs
mat_operator:  ADD | MMUL 
inputs:        MATID, MATID
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top