5

I have a grammar called leach.xtext and when I put this into Xtext in my file myDsl.xtext I'm getting the following error:

Generated package 'leach' may not be empty.

By looking at other examples, they all add some "rule" at the start of the xtext file which then points to the first original rule in the grammar. But I don't understand how to do that. Below is the whole grammar, although I think only first few lines should be relevant.

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate leach "http://www.xtext.org/example/mydsl/MyDsl" //error appears here..

start : ('Prepare' ':' '{' ingredients '}')+ (procdef) cook;
cook  : 'Cook' ID ':' '{' instructions serve ';' '}';

ingredients : ingredient ( ',' ingredient)*;
ingredient : amount food;
quality : 'large' | 'sliced' | 'finelySliced' | 'chopped' | 'fresh' | 'grated' |
            'ground' | 'unsalted' | 'fluffy' | 'goldenBrown' ;

amount : INT (unit);
unit : 'l' | 'ml' | 'cl' | 'oz' | 'g' | 'kg' | 'tesp' | 'tbsp';

temperature : INT heat;
heat : 'c' | 'f';

tlength : tunit '(' INT ')';
tunit : 'sec' | 'min';

hobheat : 'LOW' | 'MEDIUM' | 'HIGH';
hob : 'hob1' | 'hob2' | 'hob3' | 'hob4';

food : 'cookingApples' | 'sugar' | 'shortcrustPastry' | 'wensleydaleCheese' | 'whippedCream' |
    'bacon' | 'redOnion' | 'doubleCream' | 'flatleatParsley' | 'parmesan' | 'greenSalad' |
    'tomatoKetchup' | 'blackPepper' | 'goldenSyrup' | 'vanillaExtract' | 'brownSugar' | 'eggs' | 
    'pecanNuts' | 'lard' | 'vegetableOil' | 'butter' | 'readyPastry' | 'salt' | ID ;

instructions : (instruction);
instruction : 'if' '(' expr ')' '{' instructions '}' ( 'else' '{' instructions '}')  | 
    whil '(' expr ')' '{'  instructions '}' |
    'do' '{' instructions '}' whil '(' expr ')' |
    process ';' | 
    assign ';' |
    cook;

whil : 'while' | 'until';
assign : 'set' varname '=' expr;
varname : '@' ID;

process : 'Preheat' '(' temperature ')' | 
    'AddToOven' '(' container ')' |
    'Slice' '(' food ',' amount ')' |
    'RemoveFromHeat' '(' container ')' |
    'Drain' '(' container ')' |
    'Grease' '(' container ',' food ')' |
    'Layer' '(' container ',' food ')' |
    'SetHeat' '(' ( (hobheat ',' hob) | temperature ) ')' |
    'Whisk' '(' container ')' |
    'Stir' '(' container ')' |
    'AddTo' '(' container ',' food ',' amount ')' |
    'PutOnHub' '(' container ','  hob ')' |
    'Wait' '(' (expr | tlength)+ ')' |
    'EmptyTo' '(' container ',' container ')' |
    'MoveTo' '(' container ',' food ',' amount ')' |
    '~' ID '(' (expr (',' expr) ) ')' ;

procdef : 'function' ID '(' (expr (',' expr) ) ')' block;
block : '{' instructions (retur)? '}';
serve : 'serve' (container | '@'ID);
retur : 'return' expr ';' ;

container : 'bowl' | 'saucePan' | 'fryingPan' | 'bakingTray' | 'pieDish' | 'plate';
expr : e1 ('~~' e1 | 
    '<' e1 |
    '<=' e1 |
    '>' e1 |
    '>=' e1 |
    '==' e1 |
    '!=' e1 )*;

e1 : e2 ('^' e2)*;



e2 : e3 ('|' e3)*;
    e3 : e4 ('&' e4)*;
    e4 : e5 ('+' e5 | '-' e5)*;
    e5 : e6  ('*' e6 | '/' e6 | '&' e6)*;
    e6 : ('!') e7;
    e7 : e8 ('**' e7);
    e8 : 'true' | 'false' | INT | quality | food | container | process | '(' expr ')'; 
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Force444
  • 3,321
  • 9
  • 39
  • 77

1 Answers1

5

Your grammar does not look like a reasonable valid Xtext grammar. All your rules are data type rules thus no types are inferred which yields the error message. Please have a look at the manual, especially at the section about assignments.

Your grammar should probably start with something along these lines:

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate leach "http://www.xtext.org/example/mydsl/MyDsl" //error appears here..

Start : ('Prepare' ':' '{' ingredients+=Ingredients '}')+ (def=Procdef) cook=Cook;
Cook  : 'Cook' name=ID ':' '{' instructions=Instructions serve=Serve ';' '}';

Ingredients : ingredients+=Ingredient ( ',' ingredients+=Ingredient)*;

Please not that rules usually start with an uppercase letter and that you have to use assignments, e.g. = and += in order to produce a proper AST from the input.

Sebastian Zarnekow
  • 6,609
  • 20
  • 23
  • Are you sure about this? Because it's very similar to one that is presented through some lecture notes that I have. – Force444 Mar 15 '13 at 02:32
  • Yes I'm quite sure about it - I implemented both the EPackage inference and the constraints ;-) Where did you take the lecture notes from? – Sebastian Zarnekow Mar 15 '13 at 08:26
  • Oh lol, no arguing with you then ;) It's from my university course. Do you mind just writing out the first few lines as they should be then? Thanks – Force444 Mar 15 '13 at 08:42
  • Thank you :) I was not aware of this. Should probably also read the official documentation.. – Force444 Mar 16 '13 at 12:18