conflit shift/reduce

42 views
Skip to first unread message

Franck NUMBI

unread,
Feb 24, 2022, 3:00:40 PM2/24/22
to SableCC
Hi everyone, I am developing a compiler with sablecc for my memory, but I encountered a conflict that I have not been able to resolve for a week, if you can help me, here is the conflit :

shift/reduce conflict in state [stack: TVariables TIdentifiant PPrefixe *] on TEn in {
        [ P$Prefixe = PPrefixe * ] followed by TEn (reduce),
        [ PSuffixe = * TEn PType ] (shift)
}

here is the grammar :

Package two;

Helpers
        signes = '+'|'-';
        chiffre = ['0'..'9'];
        nombre_entier = chiffre+;
        nombre_reel = chiffre+ '.' chiffre+;
        //valeur_byte = ['0'..'255'];
        lettre = ['a'..'z'];
        lettres = lettre+;
        chaine_caractere = '"' (lettre* chiffre*) | lettre | chiffre* lettre* '"';
        underscore = '_';
        tout_caractere = [0x0 .. 0xfffff]+;

Tokens

        variables = 'variables';
        variable = 'variable';
        const = 'const';
        debut = 'debut';
        fin = 'fin';
        entier = 'entier';
        reel = 'reel';
        byte = 'byte';
        numerique = nombre_entier | nombre_reel;
        caractere = 'caractere';
        caracteres = '"tout_caractere"';
        identifiant = (lettres|lettre) underscore? ((lettre*|chiffre*)|(chiffre*|lettre*));
        moins = '-';
        plus = '+';
        mult = '*';
        div = '/';
        mod = 'mod';
        puissance = '^';
        operateur_superieur = '>';
        operateur_inferieur = '<';
        operateur_superieur_egal = '>=';
        operateur_inferieur_egal = '=>';
        operateur_egal = '=';
        operateur_different = '<>';
        operateur_et = 'et';
        operateur_ou = 'ou';
        operateur_affectation = '<--';
        si = 'si';
        sinon = 'sinon';
        sinonsi = 'sinonsi';
        alors = 'alors';
        finsi = 'finsi';
        ecrire = 'ecrire';
        lire = 'lire';
        concat = '&';
        en = 'en';
        separateur = ',';
        parenthese_o = '(';
        parenthese_f = ')';
        commentaire_ligne = '//'tout_caractere;
        commentaire_multiligne = '/*' (lettres | lettre)  '*/';
        blanc = (' ' | 13 | 10 | 9)+;

Ignored Tokens
        commentaire_multiligne,
        commentaire_ligne,
        blanc;

Productions

        programme =
                {vide} |
                {algorithme} entete T.debut fin;

        entete =
                {vide} |
                variables variable_declaration;

        variable_declaration = identifiant def_add;
       
        def_add =
                        {single} acompose | simple_def_add;

        simple_def_add =
                        {sequence}bcompose | compose_def_add;

        compose_def_add =
                        {sequence} cfinale;

        cfinale = ccompose+;
                       
        acompose = aprefixe+;
        bcompose = bprefixe suffixe;
        ccompose = cprefixe suffixe;

        aprefixe = prefixe suffixe; //a
        bprefixe = prefixe+;
        cprefixe = prefixe+;

        prefixe = separateur identifiant; //b
        suffixe = en type;

        type =
                        {caractere} caractere |
                         {entier} entier |
                         {byte} byte |
                         {reel} reel;

Franck NUMBI

unread,
Feb 24, 2022, 3:22:08 PM2/24/22
to SableCC
I want to prioritize the shift and not the reduce

Etienne Gagnon

unread,
Feb 24, 2022, 3:40:40 PM2/24/22
to sab...@googlegroups.com
Hi Frank,

The grammar is ambiguous. SableCC requires unambiguous grammars. For information, SableCC internally uses the LALR(1) parser algorithm, but it also attempts to resolve conflicts by inlining conflictual productions.

In your grammar, def_add.single, simple_def_add.sequence, and compose_def_add.sequence are ambiguous. The three match "prefix suffix".

Maybe you should simplify the grammar to have a single "compose", then use semantic analysis to distinguish between the various versions?
 
Have fun!

Etienne

Etienne Gagnon, Ph.D.
http://sablecc.org


--
You received this message because you are subscribed to the Google Groups "SableCC" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sablecc+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sablecc/95d26ea7-7c9f-49db-9883-f27c73499dffn%40googlegroups.com.

Franck NUMBI

unread,
Feb 24, 2022, 5:45:27 PM2/24/22
to SableCC

Thank you very much, I did as you said and it worked
Reply all
Reply to author
Forward
0 new messages