ANTLR表达式语法 [英] ANTLR Grammar for expressions

查看:213
本文介绍了ANTLR表达式语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现表达式处理语法(处理嵌套括号和内容)。到目前为止,我有以下几点,但是他们无法处理某些情况(在下面的代码块之后会出现成功/失败的情况)。任何人都知道发生了什么?



注意: varname + = varname = XText中的一代助手。现在不用担心他们了。

  ... 

NilExpression返回表达式:
'nil';

FalseExpression返回表达式:
'false';

TrueExpression返回表达式:
'true';

NumberExpression返回表达式:
value = Number;

StringExpression返回表达式:
value = STRING; // EllipsesExpression:'...'
// FunctionExpression:function = function; //不允许随机函数


UnaryExpression:
op = unop('('expr = Expression')')| expr =

BinaryExpression:
'或'? AndOp; //或op

AndOp:
'和'? ComparisonOp;

CompareOp:
('>'|'<'|'> ='|'< ='|'=='|'〜=')? ConcatOp;

ConcatOp:
'..'? AddSubOp;

AddSubOp:
('+'' - ')? MultDivOp;

MultDivOp:
('*''/')? ExpOp;

ExpOp:
'^'? (('('expr = Expression')')| expr = Expression);

ExprSideOne:Variable | NilExpression | FalseExpression | TrueExpression |
NumberExpression | StringExpression | UnaryExpression;

表达式:

expression1 = ExprSideOne expression2 + = BinaryExpression *
')'

|
(expression1 = ExprSideOne expression2 + = BinaryExpression *)
;
...

这里是解析/失败的列表:

  c =((b)); // failed 
c =((a not b)); // failed
c = b; // parses
d =(b); //解析


解决方案

表达式支持单括号,但不支持多个括号(如您所述)。我没有ANTLR的具体经验,但是我已经与Javacc合作,它共享了许多类似的概念(我为Prolog写了一个语法...不要问)。



要处理嵌套括号,您通常会有以下类似的东西:

  ParenthesisExpression:'('(ParenthesisExpression | Expression)')' ; 

这意味着该表达式包含在括号中,或者它只是一个原始表达式。至于AST如何处理这个问题,一个ParenthesisExpression是一个表达式,所以它可以被表示为一个子类或一个实现(如果Expression是一个接口/抽象类的排序)。


I'm trying to implement a expression handling grammar (that deals with nested parenthesis and stuff). I have the following so far, but they can't deal with some cases (successful/failure cases appear after the following code block). Anyone know what's going on?

Note: The varname += and varname = stuff are just some additional AST generation helper stuff in XText. Don't worry about them for now.

...

NilExpression returns Expression:
  'nil';

FalseExpression returns Expression:
  'false';

TrueExpression returns Expression:
  'true';

NumberExpression returns Expression:
  value=Number;

StringExpression returns Expression:
  value=STRING; //EllipsesExpression: '...';
//FunctionExpression: function=function; //don't allow random functions


UnaryExpression:
  op=unop ('(' expr=Expression ')')|expr=Expression;

BinaryExpression:
  'or'? AndOp; //or op

AndOp:
  'and'? ComparisonOp;

ComparisonOp:
  ('>'|'<'|'>='|'<='|'=='|'~=')? ConcatOp;

ConcatOp:
  '..'? AddSubOp;

AddSubOp:
  ('+' '-')? MultDivOp;

MultDivOp:
  ('*' '/')? ExpOp;

ExpOp:
  '^'? (('(' expr=Expression ')')|expr=Expression);

ExprSideOne : Variable|NilExpression|FalseExpression|TrueExpression|
  NumberExpression|StringExpression|UnaryExpression;

Expression:
  ( 
   '('
  expression1=ExprSideOne expression2+=BinaryExpression*
   ')' 
  )
  |
  ( expression1=ExprSideOne expression2+=BinaryExpression* )
;
...

And here's the list of parses/fails:

c = ((b)); //fails
c = ((a not b)); //fails
c = b; //parses
d = (b); //parses

解决方案

What's going on is that your Expression/Expressions support single parentheses but not multiple parentheses (as you concluded). I don't have ANTLR specific experience but I've worked with Javacc which shares many similar concepts (I wrote a grammar for Prolog... don't ask).

To handle nested parentheses, you typically have something similar to:

ParenthesisExpression: '(' (ParenthesisExpression | Expression) ')';

This would mean that the expression is either wrapped in parentheses or it's just a raw expression. As for how the AST deals with this, a ParenthesisExpression 'is a' Expression, so it can be represented as a subclass or an implementation of (if Expression is an interface/abstract class of sorts).

这篇关于ANTLR表达式语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆