-2

I wanted to make a simple Java parser so I've started with this grammar :

Programme
    : Class{ printf("Programme OK!\n");}
;

Class
    : ClassPrototype O_ACCOL VariableDeclaration Main C_ACCOL

ClassPrototype
    : ACCESS CLASS ID ClassInheritance
    | CLASS ID ClassInheritance
;

ClassInheritance
    : EXTENDS ID
    |
;

VariableDeclaration
    : TYPE ID VariableDeclarations
    | ACCESS TYPE ID VariableDeclarations
    |
;

VariableDeclarations
    : COMA ID VariableDeclarations
    | S_COLON VariableDeclaration
;

Main
    : MainPrototype O_ACCOL C_ACCOL
;

MainPrototype
    : ACCESS STATIC VOID MAIN O_PAREN "String" ID C_PAREN
    | STATIC VOID MAIN O_PAREN "String" ID C_PAREN
    | VOID MAIN O_PAREN "String" ID C_PAREN
;

After compiling I've got 2 shift/reduce conflicts. I think I know why (tell me if I'm wrong) : after reading ACCESS it can't really choose between VariableDeclaration and MainPrototype. So this is what I've found from my reasearches.

But I really don't know how to solve these conflicts. Any ideas ?

user207421
  • 305,947
  • 44
  • 307
  • 483
Romain B
  • 129
  • 2
  • 3
  • 12
  • 1
    You get two shift/reduce conflicts such as what? – user207421 Apr 18 '18 at 10:21
  • Generalize... You don't have specific "VariableDeclaration" or "Main", instead each class just contains *declarations*. They could be either variable or *functions* (of which `main` is just a special case). – Some programmer dude Apr 18 '18 at 10:24
  • What do you mean/want ? I'm sorry I'm pretty new to Bison/Lex so I don't really know what you want. To be more specific when I'm compiling I have a warning telling me I have 2 s/r conflicts. Do you want the content of y.output concerning the conflicts ? – Romain B Apr 18 '18 at 10:25
  • What part of 'such as what' didn't you understand? How can you expect to get an answer when you don't provide a proper question? – user207421 Apr 18 '18 at 10:26
  • @Someprogrammerdude So I should do a more general rule containing all my other rules starting the same way ? – Romain B Apr 18 '18 at 10:27
  • That's right, ignore the important comments and questions. – user207421 Apr 18 '18 at 10:28
  • That's my suggestion. It will also solve the problem that the `Main` function doesn't *have* to be last, but can come anywhere inside the class. Or not exist in a class at all. – Some programmer dude Apr 18 '18 at 10:31
  • @EJP I understand why I have a conflict but I'm looking for solutions because I can't figure out how to do it. I'm not ignoring your comments just working on my grammar and I'm trying to understand. – Romain B Apr 18 '18 at 10:34
  • @Someprogrammerdude Ok but how do I define my class then ? Because it'll start the same way my Declaration (ACCESS ...). – Romain B Apr 18 '18 at 10:38

1 Answers1

-1

Ok. So I came to this grammar which doesn't return me warnings/conflicts anymore. I generalized my Declaration rules as SomeProgrammerDude advised me. Thanks to him.

I know this grammar isn't perfect but I won't post all the problems I have on this post ^^

Programme
    : Class{ printf("Programme OK!\n");}
;

Class
    : ClassPrototype O_ACCOL Declarations C_ACCOL
;

ClassPrototype
    : ACCESS CLASS ID ClassInheritance
    | CLASS ID ClassInheritance
;

ClassInheritance
    : EXTENDS ID
    |
;

Declarations
    : Main
    | ACCESS STATIC TYPE ID Declaration Declarations
    | STATIC TYPE ID Declaration Declarations
    | TYPE ID Declaration Declarations
    | ACCESS TYPE ID Declaration Declarations
    |
;

Declaration
    : O_PAREN PrototypeParameters C_PAREN
    | VariableDeclarations
;

VariableDeclarations
    : COMA ID VariableDeclarations
    | S_COLON
;

PrototypeParameters
    : TYPE ID PrototypeParameters
    |
;

/* Parameters
    : ID Parameters
    |
; */

Main
    : MainPrototype O_ACCOL C_ACCOL
;

MainPrototype
    : ACCESS STATIC VOID MAIN O_PAREN TYPE ID C_PAREN
    | STATIC VOID MAIN O_PAREN TYPE ID C_PAREN
;
Romain B
  • 129
  • 2
  • 3
  • 12