[il-antlr-interest: 34115] Re: [antlr-interest] Unexpected text

2011-09-22 Thread Bart Kiers
Hi Graham,

Inside your parser-grammar, $typeSpec is the object that is returned by the
parser rule (RuleReturnScope). It includes, among others, the start- and
end-token and the tree of the rule. Using `.text`, which is short for
`getText()` will get the source of your input from `start` to `end` for that
rule. If you'd print `$typeSpec.tree` instead, you'd see the AST without
the "as" text.

Regards,

Bart.


On Thu, Sep 22, 2011 at 6:41 AM, Graham Mer  wrote:

> "Hi everybody!"
>  - Dr. Nick.
>
> I have a larger grammar, a tiny portion of which is attached below.
> Everything works as expected, except for one thing. In the tree
> grammar, note the following rule:
>
> decl: ID typeSpec { System.out.println( "decl id=" + $ID + ";type=" +
> $typeSpec.text );}
>
> Given the following input:
>  Dim foo as String
>  Dim bar as Int
>
> I expect it to print:
>  decl id=foo;type=String
>  decl id=bar;type=Int
>
> But instead I get:
>  decl id=foo;type=as String
>  decl id=bar;type=as Int
>
> The string trees all look as expected, like "(Dim foo String) (Dim bar
> Int)", but I get the extra "as" in the type node, even though I
> exclude the "AS" node when building the tree in the following rule:
>
> asType: AS! type;
>
>
> I get the same results when the parser is generated by ANTLR 3.3 and
> 3.4. What am I doing wrong?
>
>
> Here are the grammars:
>
> grammar test;
>
> options{ output=AST; }
>
> start   :   varDef+ EOF;
>
> varDef  :   DIM^ ID asType;
>
> asType  :   AS! type;
>
> type:   STRING_T | INT_T;
>
>
> AS  :   ('A'|'a')('S'|'s');
>
> DIM :   ('D'|'d')('I'|'i')('M'|'m');
>
> STRING_T:   ('S'|'s')('T'|'t')('R'|'r')('I'|'i')('N'|'n')('G'|'g');
>
> INT_T   :   ('I'|'i')('N'|'n')('T'|'t');
>
> ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;
>
> WS  :   ( ' '
>| '\t'
>| '\r'
>| '\n'
>) {$channel=HIDDEN;}
>;
>
>
>
> tree grammar testTree;
>
> options
> {
>tokenVocab=test;
>ASTLabelType=CommonTree;
>output=AST;
> }
>
> start   :   statement+
>;
>
> statement
>:   ^(DIM decl)
>;
>
> decl:   ID typeSpec { System.out.println( "decl id=" + $ID +
> ";type=" +
> $typeSpec.text );}
>;
>
> typeSpec:   STRING_T | INT_T
>;
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to il-antlr-inter...@googlegroups.com.
To unsubscribe from this group, send email to 
il-antlr-interest+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.



[il-antlr-interest: 34116] Re: [antlr-interest] How to do iteration in Tree Grammar

2011-09-22 Thread Bart Kiers
Hi Yifan,

How about something like this:

foo: ^(VIRTUAL_NODE (bar {if($bar.value) return;})* );

?

Regards,

Bart.


2011/9/22 轶凡 

> Hi, I defined a tree grammar as below:
>
> foo: ^(VIRTUAL_NODE bar*) {
>  echo($bar.value);
> };
>
> bar returns [boolean value] : //… Omitted
>
> The generated source of rule foo is like below:
>
> public final void foo() throws XX
> {
>  boolean bar40 = false;
>  do{
>//omitted
> bar40=bar();
> //omitted
>  }while (true)
>  echo(bar40)
> }
>
> Actually in the rule ‘foo’, I want to do some actions against every ‘bar’,
> not the final bar’s value, code in imagination:
>
> public final void foo() throws XX
> {
>  boolean bar40 = false;
>  do{
>//omitted
> bar40=bar();
> if (bar40){
> echo(bar40);
> return;
> }
> //omitted
>  }while (true)
>  echo(bar40)
> }
> How to change the rule ‘foo’ to archive my goal?
>
> Thanks for your help!
>
> 
> This email (including any attachments) is confidential and may be legally
> privileged. If you received this email in error, please delete it
> immediately and do not copy it or use it for any purpose or disclose its
> contents to any other person. Thank you.
>
>
> 本电邮(包括任何附件)可能含有机密资料并受法律保护。如您不是正确的收件人,请您立即删除本邮件。请不要将本电邮进行复制并用作任何其他用途、或透露本邮件之内容。谢谢。
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to il-antlr-inter...@googlegroups.com.
To unsubscribe from this group, send email to 
il-antlr-interest+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.



[il-antlr-interest: 34117] Re: [antlr-interest] How to do iteration in Tree Grammar

2011-09-22 Thread 轶凡
Yes! it solved my problem. Bart, thank your help!

From: Bart Kiers [bki...@gmail.com]
Sent: 22 September 2011 15:22
To: 轶凡
Cc: antlr-interest@antlr.org
Subject: Re: [antlr-interest] How to do iteration in Tree Grammar

Hi Yifan,

How about something like this:

foo: ^(VIRTUAL_NODE (bar {if($bar.value) return;})* );

?

Regards,

Bart.


2011/9/22 轶凡 mailto:yifan@taobao.com>>
Hi, I defined a tree grammar as below:

foo: ^(VIRTUAL_NODE bar*) {
 echo($bar.value);
};

bar returns [boolean value] : //… Omitted

The generated source of rule foo is like below:

public final void foo() throws XX
{
 boolean bar40 = false;
 do{
   //omitted
bar40=bar();
//omitted
 }while (true)
 echo(bar40)
}

Actually in the rule ‘foo’, I want to do some actions against every ‘bar’, not 
the final bar’s value, code in imagination:

public final void foo() throws XX
{
 boolean bar40 = false;
 do{
   //omitted
bar40=bar();
if (bar40){
echo(bar40);
return;
}
//omitted
 }while (true)
 echo(bar40)
}
How to change the rule ‘foo’ to archive my goal?

Thanks for your help!


This email (including any attachments) is confidential and may be legally 
privileged. If you received this email in error, please delete it immediately 
and do not copy it or use it for any purpose or disclose its contents to any 
other person. Thank you.

本电邮(包括任何附件)可能含有机密资料并受法律保护。如您不是正确的收件人,请您立即删除本邮件。请不要将本电邮进行复制并用作任何其他用途、或透露本邮件之内容。谢谢。

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address


List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to il-antlr-inter...@googlegroups.com.
To unsubscribe from this group, send email to 
il-antlr-interest+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.



[il-antlr-interest: 34118] [antlr-interest] How to stop automatic evaluation in Tree Grammar?

2011-09-22 Thread 轶凡
Hi, gurus, I defined a tree grammar which contains if statements, it looks like 
below:

ifs: ^(IF test elseif_part else_part) ;

In normal, this rule will generate source code as below:

test(); //it will return a boolean value

elseif_part();

else_part();

As you see, if the rule 'test' is ok in some conditions, there's no need to 
evaluate rule 'elseif_part' and 'else_part'.
What I need is conditional tree node expansion. Ideally generated code in my 
mind:

if (test())
{
 //skip elseif_part and else_part
}else{
  elseif_part();
  if (some condition was satisfied){
//skip else_part;
  }else{
else_part();
  }
}

Hope for you help, thanks!


This email (including any attachments) is confidential and may be legally 
privileged. If you received this email in error, please delete it immediately 
and do not copy it or use it for any purpose or disclose its contents to any 
other person. Thank you.

本电邮(包括任何附件)可能含有机密资料并受法律保护。如您不是正确的收件人,请您立即删除本邮件。请不要将本电邮进行复制并用作任何其他用途、或透露本邮件之内容。谢谢。

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to il-antlr-inter...@googlegroups.com.
To unsubscribe from this group, send email to 
il-antlr-interest+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.



[il-antlr-interest: 34119] Re: [antlr-interest] Antlr v3.3/3.4 documentation hurdles

2011-09-22 Thread The Researcher
On Wed, Aug 24, 2011 at 4:36 PM, Ramanand Mandayam <
ramanand_manda...@yahoo.com> wrote:

>
> error(10):  internal error: GTgrammar.g : java.lang.ClassCastException:
> org.antlr.runtime.tree.CommonTree cannot be cast to
> org.antlr.tool.GrammarAST



I just stumbled upon a simple way to create this error.


Grammar Demo;
Name : 'a' |'b' | 'c';

The problem is that Name is a lexer rule because it starts with a capitol
letter. There are no parser rules which start with a lower case letter. Just
change 'Name' to 'name".

Eric

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to il-antlr-inter...@googlegroups.com.
To unsubscribe from this group, send email to 
il-antlr-interest+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.



[il-antlr-interest: 34120] [antlr-interest] NullPointer exception and inline template

2011-09-22 Thread Gabriel Miro
Folks,

I'm trying to write a rule to rewrite '<>' to '!=' in some expression
strings, but every time I declare an inline template it gives me a
NullPointerException and I'm unable to compile the parser code. Any help is
appreciated.

Here's the grammar:

grammar test;
options {
output=template;
rewrite=true;
}
@header{
package com.adp.acs.rules;
}

rule:expr+;
expr:(BOOLEAN_OP)? LPAREN WS? (calc|comp|in) WS? RPAREN WS?;
calc:lside WS? ARITH_OP WS? FIELD;
comp:lside WS? COMP_OP WS? STRING;
in:FIELD 'IN' WS? LPAREN (CHAR | CHAR ',') RPAREN;
lside:FIELD | sum;
sum:LPAREN WS? FIELD WS? ARITH_OP WS? FIELD RPAREN;
RPAREN:')';
LPAREN:'(';
ARITH_OP:'+' | '-' | '*';
COMP_OP: '<>' -> template() "!="
| '<='
| '<'
| '>='
| '>'
;
BOOLEAN_OP
: 'AND' | 'OR';

FIELDoptions{greedy=false;}:'A'..'Z'+;

WS  :   ( ' '
| '\t'
| '\r'
| '\n'
) {$channel=HIDDEN;}
;

CHAR
:  '\'' ( ESC_SEQ | ~('\\'|'\'') ) '\''
;

STRING
:  '\'' ( ESC_SEQ | ~('\\'|'\'') )* '\''
;

fragment
HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;

fragment
ESC_SEQ
:   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
|   UNICODE_ESC
|   OCTAL_ESC
;

fragment
OCTAL_ESC
:   '\\' ('0'..'3') ('0'..'7') ('0'..'7')
|   '\\' ('0'..'7') ('0'..'7')
|   '\\' ('0'..'7')
;

fragment
UNICODE_ESC
:   '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
;

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to il-antlr-inter...@googlegroups.com.
To unsubscribe from this group, send email to 
il-antlr-interest+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.



[il-antlr-interest: 34123] Re: [antlr-interest] NullPointer exception and inline template

2011-09-22 Thread The Researcher
On Thu, Sep 22, 2011 at 3:46 PM, Gabriel Miro  wrote:

> COMP_OP: '<>' -> template() "!="
>| '<='
>| '<'
>| '>='
>| '>'
>;
>

 String templates are not my strong suit and I could not verify this in the
book on on the web site, so there is a chance this might be wrong.

COMP_OP is a lexer rule and lexer rules don't support string templates.
Think about what a lexer and parser do and it should be more apparent.

Eric

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to il-antlr-inter...@googlegroups.com.
To unsubscribe from this group, send email to 
il-antlr-interest+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.



[il-antlr-interest: 34124] [antlr-interest] Fwd: NullPointer exception and inline template

2011-09-22 Thread The Researcher
-- Forwarded message --
From: The Researcher 
Date: Thu, Sep 22, 2011 at 4:22 PM
Subject: Re: [antlr-interest] NullPointer exception and inline template
To: Gabriel Miro 




On Thu, Sep 22, 2011 at 3:46 PM, Gabriel Miro  wrote:

>  It gives me a NullPointerException and I'm unable to compile the parser
> code.
>

Try using with the -Xsavelexer option to see more of the error.

 student@antlr:~/projects/antlr/mail/Gabriel$ java org.antlr.Tool
-Xsavelexer test.g

error(100): test__.g:20:26: syntax error: antlr: NoViableAltException(
75@[921:1: rewrite_alternative options {k=1; } : ({...}? => rewrite_template
| {...}? => ( rewrite_element )+ -> {!stream_rewrite_element.hasNext()}? ^(
ALT[LT(1),"ALT"] EPSILON["epsilon"] EOA[""] ) -> ^(
ALT[LT(1),"ALT"] ( rewrite_element )+ EOA[""] ) | -> ^(
ALT[LT(1),"ALT"] EPSILON["epsilon"] EOA[""] ) | {...}? ETC );])
error(100): test__.g:20:26: syntax error: antlr: NoViableAltException(75@[])
error(100): test.g:0:1: syntax error: assign.types:
MismatchedTreeNodeException(0!=18)
error(10):  internal error: test.g : java.lang.NullPointerException
org.antlr.grammar.v3.DefineGrammarItemsWalker.countAltsForRule(DefineGrammarItemsWalker.java:153)
org.antlr.grammar.v3.DefineGrammarItemsWalker.rule(DefineGrammarItemsWalker.java:1546)
org.antlr.grammar.v3.DefineGrammarItemsWalker.rules(DefineGrammarItemsWalker.java:1190)
org.antlr.grammar.v3.DefineGrammarItemsWalker.grammarSpec(DefineGrammarItemsWalker.java:623)
org.antlr.grammar.v3.DefineGrammarItemsWalker.grammar_(DefineGrammarItemsWalker.java:255)
org.antlr.tool.Grammar.defineGrammarSymbols(Grammar.java:748)
org.antlr.tool.CompositeGrammar.defineGrammarSymbols(CompositeGrammar.java:369)
org.antlr.Tool.process(Tool.java:521)
org.antlr.Tool.main(Tool.java:93)

Hope that helps.

Eric

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to il-antlr-inter...@googlegroups.com.
To unsubscribe from this group, send email to 
il-antlr-interest+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.



[il-antlr-interest: 34125] Re: [antlr-interest] How to stop automatic evaluation in Tree Grammar?

2011-09-22 Thread Gary Miller
Hey 轶凡,

You might like to match the elseif_part and else_part with an any.
Then continue the walk on the correct branch.

   ifs: ^(IF test a=. b=.) ;

Have a look at a previous post that does something similar.
  http://www.antlr.org/pipermail/antlr-interest/2011-April/041307.html

Regards
Gary

On Fri, Sep 23, 2011 at 12:47 AM, 轶凡  wrote:
> Hi, gurus, I defined a tree grammar which contains if statements, it looks 
> like below:
>
> ifs: ^(IF test elseif_part else_part) ;
>
> In normal, this rule will generate source code as below:
>
> test(); //it will return a boolean value
> 
> elseif_part();
> 
> else_part();
>
> As you see, if the rule 'test' is ok in some conditions, there's no need to 
> evaluate rule 'elseif_part' and 'else_part'.
> What I need is conditional tree node expansion. Ideally generated code in my 
> mind:
>
> if (test())
> {
>  //skip elseif_part and else_part
> }else{
>  elseif_part();
>  if (some condition was satisfied){
>//skip else_part;
>  }else{
>else_part();
>  }
> }
>
> Hope for you help, thanks!
>
>
> This email (including any attachments) is confidential and may be legally 
> privileged. If you received this email in error, please delete it immediately 
> and do not copy it or use it for any purpose or disclose its contents to any 
> other person. Thank you.
>
> 本电邮(包括任何附件)可能含有机密资料并受法律保护。如您不是正确的收件人,请您立即删除本邮件。请不要将本电邮进行复制并用作任何其他用途、或透露本邮件之内容。谢谢。
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: 
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to il-antlr-inter...@googlegroups.com.
To unsubscribe from this group, send email to 
il-antlr-interest+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.