Translate ANTLR grammar into XText grammar: how to remove syntactic predicates

本秂侑毒 提交于 2019-12-05 20:25:13

I'd try to translate the grammar directly by removing all syntactic predicates and enabling backtracking in Xtext. If that works, I'd try to eliminate backtracking by reviewing all the problems that Antlr finds. Your grammar looks pretty much like backtracking is not necessary if you apply certain best practices like Xtext's Actions to eliminate left recursion. Some of the usage patterns that you applied in your Antlr grammar will not be allowed in Xtext so I bet that most syntactic predicates will not be necessary any more as soon as so transformed the syntax to an Xtext compliant version.

E.g.

primary_expression_or_function_call
  : ( INTCONSTANT ) => primary_expression
  | ( FLOATCONSTANT ) => primary_expression
  | ( BOOLCONSTANT ) => primary_expression
  | ( LEFT_PAREN ) => primary_expression
  | ( function_call_header ) => function_call
  | primary_expression
  ;

is effectively something like:

  PrimaryExpression:
    IntValue | FloatValue | BooleanValue | Parens | FunctionCall;

with

  IntValue: value=INTCONSTANT;
  ..
  Parens: '(' Expression ')';
  FunctionCall: function=[Function] '(' 
    (arguments+=Expression (',' arguments+=Expression)*)?
  ')'

and so on. Have a look at the docs for details.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!