This project has moved and is read-only. For the latest updates, please go here.

Help on writing grammar

Mar 7, 2010 at 5:44 PM

Hi, I'm using Irony to write a parser for our custom languange, that is very similar to vb.

Before begin to wriyte the entire grammar, i write little fragment of it with Irony,

I thus write this:

<font size="2">



var variableName = new IdentifierTerminal("variableName");

 var intNumber = new NumberLiteral("intNumber", NumberOptions.IntOnly);

 var lValue = new NonTerminal("lValue");

 var lValueSizeDef = new NonTerminal("lValueSizeDef");

  this.Root = lValue

  lValue.Rule = variableName + lValueSizeDef;

  lValueSizeDef.Rule = intNumber | Empty;

This grammar allows to write 1 variable name followed by an optional int number.

When I try this grammar with grammar explorer, in the tab "parser state" i get the following state table:

State S0
  Shift items:
    lValue' -> ·lValue EOF
    lValue -> ·variableName lValueSizeDef
  Transitions: lValue->S1, variableName->S2,

State S1
  Shift items:
    lValue' -> lValue ·EOF

State S2 (Inadequate)
  Shift items:
    lValue -> variableName ·lValueSizeDef
    lValueSizeDef -> ·[ intNumber ]
    lValueSizeDef -> ·[ intNumber , intNumber ]
  Reduce items:
    lValueSizeDef -> · [EOF]
  Transitions: lValueSizeDef->S4, [->S5,

State S3
  Reduce items:
    lValue' -> lValue EOF ·

State S2 reports "Inadeguate", because, if i understand, that the parser don't know if reduce or to shift.

So the way I write the grammar is wrong. What is the right way?




Our language admint to declare variable implicity, but for string need to know the size:

G$[50]="this is a string"

this is needed only once, after the first occurrence, the variable can appear without size:

G$=" another string"

In the grammar I wrote before, I eliminate the sbaces to simplify.



<font size="2">








Mar 7, 2010 at 6:06 PM
Edited Mar 7, 2010 at 6:07 PM

"Inadequate" is a commonly-used term in compiler construction field meaning that the state has both shift and reduce items. It does not mean that parser wouldn't know what to do - it will use lookaheads (next symbol) to make the decision. Only if it cannot decide based on lookahead it reports the case as a "conflict" (error) on Grammar Errors page. So pay attention only to these error message, you are ok so far.