Eclipse Community Forums Forum Search:

Home » Modeling » TMF (Xtext) » Left Factoring in grammar problem
Left Factoring in grammar problem Wed, 13 November 2013 08:53
 Kate AsMessages: 1Registered: November 2013 Junior Member
Hello everyone,
I use Xtext in order to build a language that will support array expressions. At this time I have written rules for supporting mathematical expressions and now I'm trying to write a rule to support matrix multiplication (inner product).
The rules for math expressions are written as follows:
```Logic returns Expression:
Bitwise (({And.left=current} '&&' | {Or.left=current} '||') right = Bitwise)*
;

Bitwise returns Expression:
Equal (({BitOr.left=current} '|' | {BitXor.left=current} '~' | {BitAnd.left=current}'&') right=Equal)*
;

Equal returns Expression:
Compare (({Eq.left=current} '==' | {Diff.left=current} '!=') right=Compare)*
;
Compare returns Expression:
Shift (({Ge.left=current} '>=' | {Le.left=current} '<=' | {Gt.left=current} '>' | {Lt.left=current} '<') right=Shift)*
;

```

Below these rules there are other rules such as Shift that contain operators of different precedence. All these rules lead to Atomic rule where I declare the valid operands of an Expression, among which I want to support inner product:
```Atomic returns Expression:
{NumberLiteral} value=NUMBER|
{SymbolRef} symbol=[Symbol]|
...
{InnerPr}'<'vect=Expr ',' vect2=Expr '>';
```

Now this causes left recursion. If I write the expression <x,y>-5>
when the parser will reach the first "greater than" symbol will not be able to distinguish if it is a part of Compare rule or the closing tag of the inner product rule.
I hope I made myself clear. I cannot think of a way to eliminate this problem and I would appreciate your help!
 Previous Topic: Xtextgrammar for UML-stereotypes Next Topic: Scoped highlight Occurrences
Goto Forum:

Current Time: Thu Nov 26 10:43:56 GMT 2020