English 中文(简体)
如何消除左入侵
原标题:how to remove left-recursion

我愿作一幅图形,可以要求治疗性功能。

也就是说:

a() /// good
a()() /// good
a()()() /// good
a(a) /// good
a(a()()) /// good
/// etc

我的首点是:

ID  :   ( a .. z | A .. Z | _ ) ( a .. z | A .. Z | 0 .. 9 | _ )*;

fncall  :   expr  (  (expr ( ,  expr)*)?  ) ;

expr    :   ID|fncall;

但这不能因为再入侵。

最佳回答

假设<条码>(a)(a)也有效,在此可以解决这个问题:

grammar T;

options {
  output=AST;
}

tokens {
  EXPR_LIST;
  CALL;
  INDEX;
  LOOKUP;
}

parse
 : expr EOF -> expr
 ;

expr
 : add_expr
 ;

add_expr
 : mul_exp (( +  |  - )^ mul_exp)*
 ;

mul_exp 
 : atom (( *  |  / )^ atom)*
 ;

atom
 : fncall
 | NUM
 ;

fncall
 : (fncall_start -> fncall_start) (  (  expr_list  )  -> ^(CALL $fncall expr_list)
                                  |  [  expr  ]       -> ^(INDEX $fncall expr)
                                  |  .  ID            -> ^(LOOKUP $fncall ID)
                                  )* 
 ;

fncall_start
 : ID
 |  (  expr  )  -> expr
 ;

expr_list
 : (expr ( ,  expr)*)? -> ^(EXPR_LIST expr*)
 ;

NUM :  0 .. 9 +;
ID  :  ( a .. z | A .. Z | _ ) ( a .. z | A .. Z | 0 .. 9 | _ )*;

上文图表中产生的教区将抑制投入:

(foo.bar().array[i*2])(42)(1,2,3)

and construct the following AST:

“entergraph

Without the tree rewrite rules, the grammar would look like this:

grammar T;

parse
 : expr EOF
 ;

expr
 : add_expr
 ;

add_expr
 : mul_exp (( +  |  - ) mul_exp)*
 ;

mul_exp 
 : atom (( *  |  / ) atom)*
 ;

atom
 : fncall
 | NUM
 ;

fncall
 : fncall_start (  (  expr_list  )  |  [  expr  ]  |  .  ID )* 
 ;

fncall_start
 : ID
 |  (  expr  ) 
 ;

expr_list
 : (expr ( ,  expr)*)?
 ;

NUM :  0 .. 9 +;
ID  :  ( a .. z | A .. Z | _ ) ( a .. z | A .. Z | 0 .. 9 | _ )*;
问题回答

暂无回答




相关问题
ANTLR parser hanging at proxy.handshake call

I am attempting to get a basic ECMAScript parser working, and found a complete ANTLR grammar for ECMAScript 3, which appears to compile ok and produces the appropriate Lexer/Parser/Walker Java files. (...

Will ANTLR Help? Different Suggestion?

Before I dive into ANTLR (because it is apparently not for the faint of heart), I just want to make sure I have made the right decision regarding its usage. I want to create a grammar that will parse ...

How to use ANTLR to parse xml document

can anybody tell how to use ANTLR tool(in java) to create our own grammar for xml documents and how to parse those documents using ANTLR tool(in java)?

JavaCC Problem - Generated code doesn t find all parse errors

Just started with JavaCC. But I have a strange behaviour with it. I want to verify input int the form of tokens (letters and numbers) wich are concatenated with signs (+, -, /) and wich can contain ...

How to generate introductory recognizer using ANTLR3C?

The Definitive ANTLR Guide starts with a simple recognizer. Using grammar verbatim to target C-runtime fails because %s means something to ANTLR: $ cat T.g grammar T; options { language = ...

What s the matter with this Grammar?

grammar Test; IDHEAD: ( a .. z | A .. Z | _ ); IDTAIL: (IDHEAD | 0 .. 9 ); ID: (IDHEAD IDTAIL*); fragment TYPE: ( text | number | bool ); define: define ID as TYPE; The problem ...

热门标签