Home:ALL Converter>Handling Identifiers and literals in yacc/Bison

Handling Identifiers and literals in yacc/Bison

Ask Time:2021-10-25T17:29:49         Author:Villager

Json Formatter

For a class I have to write a compiler for Go. But I'm having some problems with handling identifiers. More specifically when they are used in arithmetic or logical operations. When working with just literals I can have rules like:

LOGICAL_OPERATION: BOOLLITERAL            
    | NOT LOGICAL_OPERATION                 
    | LPAREN LOGICAL_OPERATION RPAREN          
    | LOGICAL_OPERATION AND LOGICAL_OPERATION 
    | LOGICAL_OPERATION OR LOGICAL_OPERATION  
    ;

ARITHMETIC: ARITHMETIC PLUS ARITHMETIC
    | ARITHMETIC MIN ARITHMETIC       
    | ARITHMETIC MUL ARITHMETIC        
    | ARITHMETIC DIV ARITHMETIC        
    | DIGIT                            
    | LPAREN ARITHMETIC RPAREN 
    ;  

This works, but when I try to add Identifiers to these operations I get reduce/reduce errors When the next symbol is a semicolon, which is defined to end each statement, he doesn't know to which he needs to go.

Author:Villager,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69705658/handling-identifiers-and-literals-in-yacc-bison
yy