shift reduce&& reduce reduce errors in build parser for python garmmer

Posted by user366580 on Stack Overflow See other posts from Stack Overflow or by user366580
Published on 2010-06-14T18:36:06Z Indexed on 2010/06/14 21:42 UTC
Read the original article Hit count: 354

Filed under:
|
|

i wanna build buttom up parser by java cup

i write code in java cup , it is for python language so i used grammer was written in this site :

but not all grammer , i choice partial set ,just while , identifer also i smiplified them

when i did compile for the java cup that i write

by write this command in command prompt window :

java java_cup.Main -parser CalcParser -symbols CalcSymbol < javacupfile.cup

i get conflict errors ,they are of type reduce-shift conflict and reduce-reduce conflict

you can see to print screen of the errors in these links

image 1

click here to see imge1

the grammer was in EBNF form in as refernce site and i convert it to BNF form maybe i make mistake in converting so i get such errors

the origanl grammmer was


// grammer in EBNF form

identifier ::= (letter|"_") (letter | digit | "_")*

letter ::= lowercase | uppercase

lowercase ::= "a"..."z"

uppercase ::= "A"..."Z"

digit ::= "0"..."9
compound_stmt ::= if_stmt
                | while_stmt

for_stmt ::= 
             "for" target_list "in" expression_list
              ":" suite
                ["else" ":" suite]


while_stmt ::= "while" expression ":" suite ["else" ":" suite]

suite ::= stmt_list NEWLINE

stmt_list ::=  simple_stmt (";" simple_stmt)* [";"]

simple_stmt ::= expression_stmt

expression_stmt ::=  expression_list

expression_list ::= expression ( "," expression )* [","]

expression ::= conditional_expression 

conditional_expression ::= or_test ["if" or_test "else" expression]

or_test ::= and_test | or_test "or" and_test

and_test ::= not_test | and_test "and" not_test

not_test ::= comparison | "not" not_test

comparison ::= or_expr ( comp_operator or_expr )*

comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "<>" | "!="
                | "is" ["not"] | ["not"] "in"

or_expr ::= xor_expr | or_expr "|" xor_expr

xor_expr ::= and_expr | xor_expr "^" and_expr

and_expr ::= "&" | and_expr 

the grammer after converting to BNF form

identifier ::=letterletter| letterdigit| letter"_"| "_"letter | "_"digit | "_""_"

letter ::= lowercase | uppercase

lowercase ::= "a"..."z"

uppercase ::= "A"..."Z"

digit ::= "0"..."9

while_stmt ::= "while" expression ":" suite "else" ":" suite
|"while" expression ":" suite

suite ::= stmt_list NEWLINE

stmt_list ::=  simple_stmt ";" simple_stmt stmt_list|";"

simple_stmt ::= expression_stmt

expression_stmt ::=  expression_list

expression_list ::=   expression  "," expression expression_list| ","

expression ::= conditional_expression 

conditional_expression ::= or_test "if" or_test "else" expression
|or_test

or_test ::= and_test | or_test "or" and_test

and_test ::= not_test | and_test "and" not_test

not_test ::= comparison | "not" not_test

comparison ::= or_expr  comp_operator or_expr 

comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "<>" | "!="
                | "is" ["not"] | ["not"] "in"

or_expr ::= xor_expr | or_expr "|" xor_expr

xor_expr ::= and_expr | xor_expr "^" and_expr

and_expr ::= "&" | and_expr

and the java cup file that i compile and get those errors is

import java.io.*;
terminal COMA;
terminal ELSE;
terminal WHILE;
terminal NEWLINE;
terminal SEMCOLON;
terminal CAMMA;
terminal IF;
terminal OR;
terminal AND; 
terminal NOT;
terminal LESS;
terminal GREATER;
terminal EQUAL;
terminal GREATERorE;
terminal LESSorE;
terminal NEQUAL;
terminal OROP;
terminal XOROP;
terminal ANDOP;

terminal Integer DIGIT;
terminal java.lang.String LOWERCASE;
terminal java.lang.String UPPERCASE;

non terminal java.lang.String IDENTIFIER;
non terminal java.lang.String LETTER;

non terminal   COMPOUND_STMT;
non terminal   WHILE_STMT;
non terminal   EXPRESSION;

non terminal   SUITE ;

non terminal  STMT_LIST;

non terminal  SIMPLE_STMT;

non terminal  EXPRESSION_STMT;

non terminal EXPRESSION_LIST;

non terminal   CONDITITONAL_EXPRESSION;

non terminal   OR_TEST;

non terminal AND_TEST;

non terminal NOT_TEST;

non terminal   COMPARISON;

non terminal   COMP_OPERATOR;
non terminal   OR_EXPR;
non terminal   XOR_EXPR;
non terminal   AND_EXPR;




IDENTIFIER ::=LETTER{: System.out.printf("lowercase"); :}|
{: System.out.printf("uppercase"); :}
   LETTER{: System.out.printf("lowercase"); :}|
{: System.out.printf("uppercase"); :}| LETTER{: System.out.printf("lowercase"); :}|
{: System.out.printf("uppercase"); :} DIGIT;

LETTER ::= LOWERCASE | UPPERCASE;

COMPOUND_STMT ::=WHILE_STMT;
WHILE_STMT ::= WHILE{: System.out.printf( "while"); :} 
EXPRESSION COMA {: System.out.printf(":"); :}
SUITE ELSE {: System.out.printf("else" ); :} 
COMA{: System.out.printf( ":" ); :} SUITE
|WHILE{: System.out.printf( "while" ); :} EXPRESSION 
COMA{: System.out.printf( ":" ); :} SUITE;

SUITE ::= STMT_LIST NEWLINE{: System.out.printf( "newline" ); :};

STMT_LIST ::=  SIMPLE_STMT SEMCOLON{: System.out.printf( ";" ); :} 
SIMPLE_STMT STMT_LIST|SEMCOLON{: System.out.printf( ";" ); :};

SIMPLE_STMT ::=EXPRESSION_STMT;

EXPRESSION_STMT ::=EXPRESSION_LIST;

EXPRESSION_LIST ::= EXPRESSION CAMMA{: System.out.printf( "," ); :} 
EXPRESSION EXPRESSION_LIST| CAMMA{: System.out.printf( "," ); :};

EXPRESSION ::= CONDITITONAL_EXPRESSION; 

CONDITITONAL_EXPRESSION ::= OR_TEST IF{: System.out.printf( "if"); :} 
OR_TEST ELSE{: System.out.printf("else"); :} EXPRESSION
|OR_TEST;

OR_TEST ::= AND_TEST | OR_TEST OR{: System.out.printf( "or"); :} AND_TEST;

AND_TEST ::= NOT_TEST | AND_TEST AND{: System.out.printf( "and"); :} NOT_TEST;

NOT_TEST ::= COMPARISON | NOT{: System.out.printf("not"); :} NOT_TEST;

COMPARISON ::= OR_EXPR  COMP_OPERATOR OR_EXPR ;

COMP_OPERATOR ::= LESS{: System.out.printf( "<"); :}

 | GREATER{: System.out.printf(">"); :} 

| EQUAL{: System.out.printf("=="); :} 

| GREATERorE{: System.out.printf(">="); :} 

| LESSorE{: System.out.printf("<="); :}

| NEQUAL{: System.out.printf("!="); :};



OR_EXPR ::= XOR_EXPR | OR_EXPR OROP{: System.out.printf("|"); :} XOR_EXPR;

XOR_EXPR ::= AND_EXPR | XOR_EXPR XOROP {: System.out.printf("^"); :}XOR_EXPR;

AND_EXPR ::= ANDOP{: System.out.printf("&"); :} | AND_EXPR;

can any one told me how can solve this errors to build parser correcrtly??

© Stack Overflow or respective owner

Related posts about java

Related posts about python