[il-antlr-interest: 31406] [antlr-interest] semantic predicates

2011-02-11 Thread Dominik Dietrich
Hi,

I'm new to ANTLR and have a problem with the following toy grammar, 
which uses semantic predicates to disambiguate operator precedences. The 
idea is to support dynamic prolog style operator declarations, where 
lower precedence binds tighter. On the input 3*3+3, the epsilon 
production of the rule rterm is not choosen, but instead I get a 
NoViableAltException, which I do not understand. I'm aware that I could 
also express the precedences in the productions, but this is not what I 
want because the overall goal later is to support dynamic operators. Can 
you please help?

Thanks.

grammar PTermParser;

options {
   output=AST;
}

@members {
   public static void main(String[] args) throws Exception {
 PTermParserLexer lex = new PTermParserLexer(new 
ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream(lex);

 PTermParserParser parser = new PTermParserParser(tokens);

 try {
 parser.term(1200);
 } catch (RecognitionException e)  {
 e.printStackTrace();
 }
 }
}

WHITESPACE: (' ' | '\t')+ { $channel = HIDDEN; };
NEWLINE: ('\r'? '\n')+ { $channel = HIDDEN;} ;
IDENTIFIER :LETTER (LETTER | DIGIT | '_')*;
NUMBER :DIGIT+;
OPERATORSEQ : SPECIALCHAR+;

fragment LETTER
 : 'a'..'z'
 | 'A'..'Z'
 ;

fragment DIGIT:'0' .. '9';

fragment SPECIALCHAR :'+' | '-' | '*' | '/';

start
@init{
   System.out.println(start);
}
: term[1200];

term[int prec]
@init{
   System.out.println(term with +$prec);
}
:
 NUMBER rterm[$prec, 0]
 |IDENTIFIER rterm[$prec, 0]
 |'(' term[1200] ')' rterm[$prec, 0]
 ;

rterm[int pprec, int lprec]
@init{
   System.out.println(rterm: pprec is +pprec+ lprec is +lprec);
}
  :{(400 = $pprec)  ($lprec  400)}?= ('+'^ term[400] 
rterm[pprec,400]) { System.out.println(use + production);}
 |{(300 = $pprec)  ($lprec  300)}?= ('*'^  term[300] 
rterm[pprec,300]) { System.out.println(use * production);}
 |
 ;



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: 31407] Re: [antlr-interest] Ident with ending In or Out word

2011-02-11 Thread Roohul
Hi,
This is my sample grammar.. this did not work.. another thing is 
that my variables and connectors both can have digits so I created Alpha
 : LETTER (LETTER | DIGIT)*; for this but it also give an error. 

primitiveType
    :   'real'
    |   'integer'
    |   'char'
    |   'boolean'
    |   'void'
    ;



 
CONNECTOR : LETTER+ ('In' | 'Out');
ID: LETTER+ ;

    

fragment LETTER : ('a'..'z' | 'A'..'Z') ;
fragment DIGIT : '0'..'9';
INT : DIGIT+ ;

Regards

Roohul 
S/W Engineer, LIU, Sweden




--- On Thu, 2/10/11, Bart Kiers bki...@gmail.com wrote:

From: Bart Kiers bki...@gmail.com
Subject: Re: [antlr-interest] Ident with ending In or Out word
To: Roohul roohu...@yahoo.com
Cc: ANTLR antlr-interest@antlr.org
Date: Thursday, February 10, 2011, 10:40 AM

On Thu, Feb 10, 2011 at 6:08 PM, Roohul roohu...@yahoo.com wrote:

Hi,

 

I am working on a grammar which have some variables ending with In or Out which 
is treated as the connector between two components. I do not know how to handle 
this.

 

For example:

real abcIn

real abcOut

 

these are treated as connectors. while abcI or abcOu will be treated as normal 
variables.

You could handle this on a lexer level like this:

REAL  :  'real'
  ;

CONNECTOR  :  Alpha+ ('In' | 'Out')
  ;

VARIABLE  :  Alpha+
  ;

fragmentAlpha  :  'a'..'z'  |  'A'..'Z'  ;

(the order of the rules is important here!)
Regards,
Bart. 




  

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: 31408] Re: [antlr-interest] Ident with ending In or Out word

2011-02-11 Thread Bart Kiers
On Fri, Feb 11, 2011 at 2:27 PM, Roohul roohu...@yahoo.com wrote:

 Hi,
 This is my sample grammar.. this did not work.. another thing is that my
 variables and connectors both can have digits so I created Alpha : LETTER
 (LETTER | DIGIT)*; for this but it also give an error.


The problem descriptions *did not work* and *also give an error* do not
tell much. Care to elaborate?

Bart.

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: 31410] Re: [antlr-interest] Ident with ending In or Out word

2011-02-11 Thread Roohul
Sorry for that..

when i use only letters (a..z | A..Z) in the variables and connectors it give 
an error while creating an AST that no viable alternative at input 'real' and 
when creating variables with digits the grammar do not compile at all and give 
the following error. 
The following token definitions can never be matched because prior tokens 
match the same  input: Alpha

I hope that it is more elaboration.. 

Regards

Roohul 




--- On Fri, 2/11/11, Bart Kiers bki...@gmail.com wrote:

From: Bart Kiers bki...@gmail.com
Subject: Re: [antlr-interest] Ident with ending In or Out word
To: Roohul roohu...@yahoo.com
Cc: ANTLR antlr-interest@antlr.org
Date: Friday, February 11, 2011, 5:30 AM

On Fri, Feb 11, 2011 at 2:27 PM, Roohul roohu...@yahoo.com wrote:

Hi,
This is my sample grammar.. this did not work.. another thing is 
that my variables and connectors both can have digits so I created Alpha
 : LETTER (LETTER | DIGIT)*; for this but it also give an error. 


The problem descriptions did not work and also give an error do not tell 
much. Care to elaborate?

Bart.




  

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: 31412] Re: [antlr-interest] Ident with ending In or Out word

2011-02-11 Thread Kirby Bohling
On Fri, Feb 11, 2011 at 9:17 AM, Roohul roohu...@yahoo.com wrote:
 Sorry for that..

 when i use only letters (a..z | A..Z) in the variables and connectors it give 
 an error while creating an AST that no viable alternative at input 'real' 
 and when creating variables with digits the grammar do not compile at all and 
 give the following error.
 The following token definitions can never be matched because prior tokens 
 match the same  input: Alpha

 I hope that it is more elaboration..


I would try moving some of that to real lexer items.  You are using
generated tokens, which depending on where they go, 'real', would be
matched by the rule for ID, and there would be no way to generate
that.  I don't know where they end up, but I do remember it being
recommended to avoid them until you have more experience.  Given the
error, I think that is what is happening here.  Look at the sample
grammars and how they handle keywords.

My guess is that you are trying to do too much in the lexer, you can
detect a lot of this at a much later state in the parser/analysis
phase.  But that is just parroting common advise on this list.

Kirby



 Regards

 Roohul




 --- On Fri, 2/11/11, Bart Kiers bki...@gmail.com wrote:

 From: Bart Kiers bki...@gmail.com
 Subject: Re: [antlr-interest] Ident with ending In or Out word
 To: Roohul roohu...@yahoo.com
 Cc: ANTLR antlr-interest@antlr.org
 Date: Friday, February 11, 2011, 5:30 AM

 On Fri, Feb 11, 2011 at 2:27 PM, Roohul roohu...@yahoo.com wrote:

 Hi,
 This is my sample grammar.. this did not work.. another thing is
 that my variables and connectors both can have digits so I created Alpha
  : LETTER (LETTER | DIGIT)*; for this but it also give an error.


 The problem descriptions did not work and also give an error do not tell 
 much. Care to elaborate?

 Bart.






 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: 31413] [SPAM] [antlr-interest] [SPAM] Optional expression rule alternative

2011-02-11 Thread Olivier Lefevre
Is it possible to have a rule like this in ANTLR?

   rule[boolean z] : if (z) then rule1 else rule2 ;

Thanks,

-- O.L.




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: 31414] Re: [antlr-interest] [SPAM] [SPAM] Optional expression rule alternative

2011-02-11 Thread Kevin J. Cummings
On 02/11/2011 12:34 PM, Olivier Lefevre wrote:
 Is it possible to have a rule like this in ANTLR?
 
rule[boolean z] : if (z) then rule1 else rule2 ;

rule[boolean z]
: { z }? rule1
| rule2
;

 Thanks,
 
 -- O.L.

-- 
Kevin J. Cummings
kjch...@verizon.net
cummi...@kjchome.homeip.net
cummi...@kjc386.framingham.ma.us
Registered Linux User #1232 (http://counter.li.org)

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: 31415] [SPAM] [antlr-interest] [SPAM] Re: [SPAM] [SPAM] Optional expression rule alternative

2011-02-11 Thread Olivier Lefevre
Great! Not to abuse but would you know in which section of
The Definitive ANTLR Reference this mentioned? I couldn't
find it. What is this syntax called?

But in my real-world problem it was actually not a choice
between rule1 and rule2 but a question of whether to allow
an extra alternative in the z case without too much
verbatim repetition, i.e. (again in pseudo-code):

rule[boolean z] :
   rule1 | rule2 | ... | rule23 | (z ? rule24 : nothing) ;

That is, allow rule24 if z otherwise stop at rule23.

Thanks again,

-- O.L.


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: 31416] Re: [antlr-interest] [SPAM] [SPAM] Re: [SPAM] [SPAM] Optional expression rule alternative

2011-02-11 Thread Justin Murray
These are called Semantic Predicates. Section 12.1 in TDAR. 
Specifically, look at page 295 of the PDF. If the predicate evaluates to 
false, the alternative is effectively switched off.

- Justin

On 2/11/2011 1:04 PM, Olivier Lefevre wrote:
 Great! Not to abuse but would you know in which section of
 The Definitive ANTLR Reference this mentioned? I couldn't
 find it. What is this syntax called?

 But in my real-world problem it was actually not a choice
 between rule1 and rule2 but a question of whether to allow
 an extra alternative in the z case without too much
 verbatim repetition, i.e. (again in pseudo-code):

 rule[boolean z] :
 rule1 | rule2 | ... | rule23 | (z ? rule24 : nothing) ;

 That is, allow rule24 if z otherwise stop at rule23.

 Thanks again,

 -- O.L.


 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: 31417] Re: [antlr-interest] [SPAM] [SPAM] Re: [SPAM] [SPAM] Optional expression rule alternative

2011-02-11 Thread Bart Kiers
On Fri, Feb 11, 2011 at 7:04 PM, Olivier Lefevre lefev...@yahoo.com wrote:

 Great! Not to abuse but would you know in which section of
 The Definitive ANTLR Reference this mentioned? I couldn't
 find it. What is this syntax called?


It's called a validating semantic predicate which is explained in chapter
12 and 13.


But in my real-world problem it was actually not a choice
 between rule1 and rule2 but a question of whether to allow
 an extra alternative in the z case without too much
 verbatim repetition, i.e. (again in pseudo-code):

 rule[boolean z] :
   rule1 | rule2 | ... | rule23 | (z ? rule24 : nothing) ;

 That is, allow rule24 if z otherwise stop at rule23.


You can still do that:

rule [boolean z]
  :  {z}? (rule1 | rule2 | rule3 | ... | rule23 | rule24)
  |   (rule1 | rule2 | rule3 | ... | rule23)
  ;


Regards,

Bart.

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: 31418] [SPAM] [antlr-interest] [SPAM] Re: Re: Optional expression rule alternative

2011-02-11 Thread Olivier Lefevre
On 2/11/2011 7:34 PM, Bart Kiers wrote:
 You can still do that:
 rule [boolean z]
:  {z}? (rule1 | rule2 | rule3 | ... | rule23 | rule24)
|   (rule1 | rule2 | rule3 | ... | rule23)
;

Yes but the rule1 | ... | rule23 train is huge: that is what
I meant by without too much verbatim repetition. So I'll
pack off the first 23 rules into yet another rule; not ideal
but that'll do.

I knew about predicates (although I haven't used them so far)
but they didn't occur to me in this context because they are
presented as a solution for grammar ambiguities, not as
switches driven from outside as here.

Thanks again,

-- O.L.


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: 31419] [antlr-interest] beginner question - 'unexpected ast node' when generating from combined grammar

2011-02-11 Thread Nick C
Hi,

I'm trying learn antlr by writing a parser for a simple HTML
templating language (in combination with reading the Definitive Antlr
Reference book - I'm only just past the calculator example so far
though.)

The parser should handle something like this:

  {namespace My.Namespace}
  {template MyTemplate}
  hello
  {if $name}
  {print $name}
  {else}
  world
  {/if}
  br/
   {/template}

My current attempt is to first build a simple version of the parser
without any actions, just to get it to parse valid input correctly:

grammar Test;

options { language = 'CSharp2'; }

doc: ns
 WS*
 (template)+
 WS* ;

ns  :'{namespace' WS+ DOTTED_IDENT WS* '}';

template:'{template' WS+ IDENT WS* '}' content '{/template}';

cmdSp   :'{sp' WS* '/}';

cmdIf   :'{if' WS* '}' content ('{elseif}' content)* ('{else}'
content)? WS* '{/if}' ;

anyCmd  :cmdSp | cmdIf;
nonCmd  :~(anyCmd); /* ~('{')*;*/
content :(anyCmd | nonCmd)*;

WS  :' '|'\t'|'\r'|'\n';
IDENT   :('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
DOTTED_IDENT
:IDENT ((WS)* '.' (WS)* IDENT)*;


I was hoping to parse the first example minus the {$name} print
statement and the conditional in the {if} statement (i.e. the $name in
the {if}.)

When I try to generate the parser with the above definition, I get the
following warnings/errors:

Test.g:0:0: syntax error: buildnfa: AST:19:16: unexpected AST node: anyCmd
Test.g:19:14: set complement is empty

I'm guessing my use of ~(anyCmd) is incorrect, but I don't understand why?

If I try replacing that with ~('{')* as per the comment above, I get
these errors:

Test.g:19:18: Decision can match input such as '{else}' using
multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
... more errors like this ...
Test.g:19:18: The following alternatives can never be matched: 2

I thought this was specifying 'any character apart from {', so I don't
understand how '{else}' could be a match (or why there are multiple
alternatives - I thought I only specified one, unless * counts as
many?)

A brief explanation and/or a pointer to the section of the book I
should be reading would be most welcome.

Thanks,
Nick

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: 31420] [SPAM] [antlr-interest] [SPAM] Chaining attributes

2011-02-11 Thread Olivier Lefevre
I wonder why it is not possible to chain attributes.
E.g., $rule.start.type doesn't work. Since
$rule.start.getType() does it's not a big deal but
the former would be more elegant.

-- O.L.


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.