xtext: expression/factor/term grammar


This has got to be one of those well-known examples that's somewhere on the internet, but I can't seem to find it.

I'm trying to learn XText and I figured a calculator expression parser would be a good start. But I'm getting syntax errors in my grammar:

Expression: Term (('+'|'-') Term)*; Term: Factor (('*'|'/') Factor)*; Factor: number=Number | variable=ID | ('(' expression=Expression ')');

I get this error in the Expression and Term lines:

Multiple markers at this line - Cannot change type twice within a rule - An unassigned rule call is not allowed, when the 'current' was already created.

What gives? How can I fix this? And when do I have instanceName=Rule vs. Rule entries in a grammar?


I downloaded xtext integrated with eclipse and it comes with a calculator example which does approximately what you wish called arithmetics. From what I can gather you will need to assign an associativity to your tokens. This grammar runs fine for me:

Expression: Term (({Plus.left=current}'+'|{Minus.left=current}'-') right=Term)*; Term: Factor (({Multiply.left=current} '*'| {Division.left=current}'/') right=Factor)*; Factor: number=NUMBER | variable=ID | ('(' expression=Expression ')');

The example grammar they have for arithmetics can be viewed <a href="http://git.eclipse.org/c/tmf/org.eclipse.xtext.git/plain/examples/org.eclipse.xtext.xtext.ui.examples/contents/org.eclipse.xtext.example.arithmetics/src/org/eclipse/xtext/example/arithmetics/Arithmetics.xtext" rel="nofollow">here</a>. It includes a bit more than your, like function calls, but the basics are the same.


  • Cassandra: What is a subcolumn
  • How can I count unique terms in a plaintext file case-insensitively?
  • Zeromq with python hangs if connecting to invalid socket
  • rapply over a nested list in R
  • Geom_jitter colour based on values
  • F#: In which memory area is the continuation stored: stack or heap?
  • Shouldn't else be indented in the below code
  • Image map in Flex
  • Apache RewriteRule redirection with url encoded
  • Moving Android View and preventing onDraw to be called over and over again
  • Is playing sound in Javascript performance heavy?
  • HTTP/2 streams vs HTTP/1.1 connections
  • Replace value with Factor in r data.table
  • Allowing both email and username for authentication
  • C++ Partial template specialization - design simplification
  • R - Combining Columns to String Based on Logical Match
  • Is there a javascript serializer for JSON.Net?
  • Read text file and split every line in MSBuild
  • Where to put my custom functions in Wordpress?
  • Weird JavaScript statement, what does it mean?
  • How do you troubleshoot character encoding problems?
  • Return words with double consecutive letters
  • how to add data labels for bar graph in matlab
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Buffer size for converting unsigned long to string
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How do I configure my settings file to work with unit tests?
  • How to stop GridView from loading again when I press back button?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML