[il-antlr-interest: 32499] Re: [antlr-interest] Can't figure this one out

2011-05-20 Thread Bart Kiers
Hi David,

Your parser does not handle:

ans = 3 * (-x + y) * 4


properly since `ans` is an ANS-token and not an IDENT-token. Therefor it
does not get matched by your `assignmentStatement` rule.

Also, you should probably add the EOF at the end of your `script` rule in
your combined grammar.

Regards,

Bart.


On Fri, May 20, 2011 at 3:47 AM, David Smith david.sm...@cc.gatech.eduwrote:

 I developed a tree parser by making minor changes to Scott
 Stanchfield's tutorial videos.  I don't know where to start looking
 to explain the problem.
 Here are the pieces:

 // The grammar:
 grammar GTMat;

 options {
   language = Java;
   output=AST;
   ASTLabelType=CommonTree;
 }

 tokens {
 NEGATION;
 }


 @header {
   package parser;
 }

 @lexer::header {
   package parser;
 }

 script
 :   statement*
 ;

 statement
 :   assignmentStatement
 ;

 assignmentStatement
 :   IDENT GETS^ expression SEMI?
 ;

 actualParameters
 :   expression (COMMA expression)*
 ;


 // expressions -- fun time!

 term
 :   (IDENT OPENP ) = IDENT '(' actualParameters ')'
 |   OPENP! expression CLOSEP!
 |   INTEGER
 |   IDENT
 ;


 unary
 :   (PLUS! | negation^)* term
 ;

 negation
 :   MINUS - NEGATION
 ;

 mult
 :   unary ((MULT^ | DIV^ ) unary)*
 ;

 add
 :   mult ((PLUS^ | MINUS^) mult)*
 ;

 relation
 :   add ((EQUALS^ | NOTEQ^ | LESS^ | LESSEQ^ | GT^ | GTEQ^)
 add)*
 ;

 expression
 :   relation ((AND^ | OR^) relation)*
 ;

 GETS:   '=';
 SWITCH  :   'switch';
 CASE:   'case';
 OTHERWISE
 :   'otherwise';
 IF  :   'if';
 ELSE:   'else';
 ELSEIF  :   'elseif';
 END :   'end';
 FOR :   'for';
 WHILE   :   'while';
 ANS :   'ans';
 COMMA   :   ',';
 OPENP   :   '(';
 CLOSEP  :   ')';
 NOT :   '~';
 SEMI:   ';';
 PLUS:   '+';
 MINUS   :   '-';
 MULT:   '*';
 DIV :   '/';
 EQUALS  :   '==';
 NOTEQ   :   '!=';
 LESS:   '';
 LESSEQ  :   '=';
 GT  :   '';
 GTEQ:   '=';
 AND :   '';
 OR  :   '||';
 SINGLE  :   '\'';

 fragment LETTER : ('a'..'z' | 'A'..'Z') ;
 fragment DIGIT : '0'..'9';
 INTEGER : DIGIT+ ;
 IDENT : LETTER (LETTER | DIGIT)*;
 WS : (' ' | '\t' | '\n' | '\r' | '\f')+ {$channel = HIDDEN;};
 COMMENT : '%' .* ('\n'|'\r') {$channel = HIDDEN;};

 // The Walker Grammar:
 tree grammar EvaluatorWalker;

 options {
   language = Java;
   tokenVocab = GTMat;
   ASTLabelType = CommonTree;
 }

 @header {
   package parser;
   import java.util.Map;
   import java.util.HashMap;
 }

 @members {
   private MapString, Integer variables = new HashMapString, Integer();
 }

 evaluator
 :   assignment* EOF
 ;

 assignment
 :   ^('=' IDENT e=expression)
 { variables.put($IDENT.text, e); }
 ;

 expression returns [int result]
 :   ^('+' op1=expression op2=expression) { result = op1 + op2;
 }
 |   ^('-' op1=expression op2=expression) { result = op1 - op2;
 }
 |   ^('*' op1=expression op2=expression) { result = op1 * op2;
 }
 |   ^('/' op1=expression op2=expression) { result = op1 / op2;
 }
 |   ^(NEGATION e=expression)  { result = -e; }
 |   IDENT { result = variables.get($IDENT.text); }
 |   INTEGER { result = Integer.parseInt($INTEGER.text); }
 ;
 // The Test Program:
 package parser;

 import org.antlr.runtime.ANTLRFileStream;
 import org.antlr.runtime.CharStream;
 import org.antlr.runtime.CommonTokenStream;
 import org.antlr.runtime.RecognitionException;
 import org.antlr.runtime.TokenStream;
 import org.antlr.runtime.tree.CommonTreeNodeStream;
 import java.io.IOException;

 public class Test4 {
 public static void main(String[] args)
 throws RecognitionException, IOException {
 CharStream stream =
 new ANTLRFileStream(Test.m);
 GTMatLexer lexer = new GTMatLexer(stream);
 TokenStream tokenStream = new CommonTokenStream(lexer);
 GTMatParser parser = new GTMatParser(tokenStream);
 GTMatParser.script_return evaluator = parser.script();
 System.out.println(evaluator.tree.toStringTree());
 CommonTreeNodeStream nodeStream = new
  CommonTreeNodeStream(evaluator.tree);
 EvaluatorWalker walker = new EvaluatorWalker(nodeStream);
 walker.evaluator();
 System.out.println(ok);
 }
 }
 // The input code:
 x = 8
 y = 2 + 3
 ans = 3 * (-x + y) * 4

 // When I run it, I get this:

 (= x 8) (= y (+ 2 

[il-antlr-interest: 32500] [antlr-interest] AST Question

2011-05-20 Thread massimiliano.m...@gmail.com
Hello All,

I'm more or less a newbie using antlr. I have a small issue on creating
the AST, using rewrite rules. I'm so sorry if this is a FAQ or similar! :)

I have the following productions (it's like an algebra with
3 operators with different priorities):


targetsExpr  : (category) ('CAND' targetsExpr)*
   -^('CAND' category targetsExpr*)
 ;
category: (matchEl) ('OR' category)*
   - ^('OR' matchEl category* )
 ;
matchEl : factor ('AND' factor)* -^('AND' factor*)
 ;

factor
 : matchId OPAR targetValue COMMA targetName CPAR -^('FAC' matchId
targetValue targetName)


The problem is that the AST created contains productions as:

CAND -OR - OR- AND (FAC, FAC).

The second OR is created because the ``category'' production is passed
multiple times.

Is there a way to not create these kind of  rules?

You can see a sample of the AST created in
http://www.mascanc.net/~max/policy.pdf.



-- 
Massimiliano Masi

http://www.mascanc.net/~max

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: 32501] [antlr-interest] Error in the method specialStateTransition(int, IntStream)

2011-05-20 Thread preitz sharma
Hi,
I need to develop a grammar with the following structure:

set Param-Name Param-Val

The grammar which I have developed is working fine except for one Problem:

- If the Param-Val does not match the constraints defined for certain
Param_Name, then I need to throw an error.

I am doing it like this:

command:SET ARRAYSIZE ( Int  {/*Process the arraysize*/}
| (.)+ {/*Display eror message*/})
  ;
Int:('0'..'9')+;
Char:('a'..'z');
SPL_CHARS  :  ('\U' .. '\U')+;

The problem is that I am getting an error: The code of method
specialStateTransition(int, IntStream) is exceeding the 65535 bytes limit
What should I do?
PS: The grammar which I have to write is pretty big.. If I make SPL_CHARS
:  ('\U' .. '\U')  then its working fine
Please suggest some solution..

Thanks
Preeti Sharma

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: 32502] Re: [antlr-interest] AST Question

2011-05-20 Thread Bart Kiers
Hi

Your rule:

targetsExpr
  :  category ('CAND' targetsExpr)* - ^('CAND' category targetsExpr*)
  ;


is incorrect. You're always using `CAND` in your rewrite rule but that rule
could just match `category` only.

You'll probably want to do:

targetsExpr
  :  category ('CAND'^ targetsExpr)*
  ;

(Note the ^ after CAND which makes it the root)

Regards,

Bart.


On Fri, May 20, 2011 at 10:11 AM, massimiliano.m...@gmail.com 
massimiliano.m...@gmail.com wrote:

 Hello All,

 I'm more or less a newbie using antlr. I have a small issue on creating
 the AST, using rewrite rules. I'm so sorry if this is a FAQ or similar! :)

 I have the following productions (it's like an algebra with
 3 operators with different priorities):


 targetsExpr  : (category) ('CAND' targetsExpr)*
   -^('CAND' category targetsExpr*)
  ;
 category: (matchEl) ('OR' category)*
   - ^('OR' matchEl category* )
  ;
 matchEl : factor ('AND' factor)* -^('AND' factor*)
  ;

 factor
  : matchId OPAR targetValue COMMA targetName CPAR -^('FAC' matchId
 targetValue targetName)


 The problem is that the AST created contains productions as:

 CAND -OR - OR- AND (FAC, FAC).

 The second OR is created because the ``category'' production is passed
 multiple times.

 Is there a way to not create these kind of  rules?

 You can see a sample of the AST created in
 http://www.mascanc.net/~max/policy.pdf.



 --
 Massimiliano Masi

 http://www.mascanc.net/~max

 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: 32505] Re: [antlr-interest] Error in the method specialStateTransition(int, IntStream)

2011-05-20 Thread Jim Idle
SPL_CHARS  :  ('\U' .. '\U')+;


The '+' in this means match everything and keep matching everything. You
cannot use + here, you need:

SPL_CHARS  : .;

Then use SPL_CHARS+ in your grammar not (.)+

Though I think you would be better writing a small piece of code to
process this really; ANTLR is a bit overkill for set x y

Jim

 -Original Message-
 From: antlr-interest-boun...@antlr.org [mailto:antlr-interest-
 boun...@antlr.org] On Behalf Of preitz sharma
 Sent: Friday, May 20, 2011 1:28 AM
 To: antlr-interest@antlr.org
 Subject: [antlr-interest] Error in the method
 specialStateTransition(int, IntStream)

 Hi,
 I need to develop a grammar with the following structure:

 set Param-Name Param-Val

 The grammar which I have developed is working fine except for one
 Problem:

 - If the Param-Val does not match the constraints defined for certain
 Param_Name, then I need to throw an error.

 I am doing it like this:

 command:SET ARRAYSIZE ( Int  {/*Process the arraysize*/}
 | (.)+ {/*Display eror message*/})
   ;
 Int:('0'..'9')+;
 Char:('a'..'z');
 SPL_CHARS  :  ('\U' .. '\U')+;

 The problem is that I am getting an error: The code of method
 specialStateTransition(int, IntStream) is exceeding the 65535 bytes
 limit
 What should I do?
 PS: The grammar which I have to write is pretty big.. If I make
 SPL_CHARS
 :  ('\U' .. '\U')  then its working fine Please suggest some
 solution..

 Thanks
 Preeti Sharma

 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: 32506] Re: [antlr-interest] AST Question

2011-05-20 Thread massimiliano.m...@gmail.com
Hi,

On Fri, May 20, 2011 at 10:55 AM, Bart Kiers bki...@gmail.com wrote:
 targetsExpr
   :  category ('CAND' targetsExpr)* - ^('CAND' category targetsExpr*)
   ;

 is incorrect. You're always using `CAND` in your rewrite rule but that rule
 could just match `category` only.
 You'll probably want to do:

 targetsExpr
   :  category ('CAND'^ targetsExpr)*
   ;


Thank you for your answer! It works now! But I've another question now.
When I traverse the tree using this function (is there an example to
have a visitor
created by antlr?)

 public void visitTree(CommonTree t) throws Exception {
if ( t != null ) {
for ( int i = 0; i  t.getChildCount(); i++ ) {


//  System.out.println(t.getChild(i).toString() +  type  +
t.getType() +  child type  + t.getChild(i).getType());
visitNode(t);

visitTree((CommonTree)t.getChild(i));

}
}
}

in the visitNode() I have the same token type as for the children. I
explain better:
If I have something like:

FAC-child(1),child(2), child(3)

the visitNode(t) is called 3 times with the same token type!

You can see the grammar here: http://www.mascanc.net/~max/xacml3.g

Thank you and best regards,

Massimiliano
-- 
Massimiliano Masi

http://www.mascanc.net/~max

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: 32507] Re: [antlr-interest] AST Question

2011-05-20 Thread Bart Kiers
On Fri, May 20, 2011 at 9:54 PM, massimiliano.m...@gmail.com 
massimiliano.m...@gmail.com wrote:

 Hi,

 On Fri, May 20, 2011 at 10:55 AM, Bart Kiers bki...@gmail.com wrote:
  targetsExpr
:  category ('CAND' targetsExpr)* - ^('CAND' category targetsExpr*)
;
 
  is incorrect. You're always using `CAND` in your rewrite rule but that
 rule
  could just match `category` only.
  You'll probably want to do:
 
  targetsExpr
:  category ('CAND'^ targetsExpr)*
;
 

 Thank you for your answer! It works now! But I've another question now.
 When I traverse the tree using this function (is there an example to
 have a visitor
 created by antlr?)


Yes, this is typically what tree grammars are for. But you can also walk it
manually.
See:

http://www.antlr.org/article/1100569809276/use.tree.grammars.tml
http://www.antlr.org/article/1170602723163/treewalkers.html

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: 32508] [antlr-interest] gUnit: Test of a parser rule always fail

2011-05-20 Thread Simon Marchi
Hello !

I a trying to automatize the testing of my grammar using gUnit, but I
ran into the following problem: when I try to test a parser rule, the
input string is never recognized. The lexer rules can be tested
without problem. I made a simple example to illustrate:

Grammar file Cool.g:
grammar Cool;

options {language = Java;}
@lexer::header {package a.b.c;}
@header {package a.b.c;}

// Parser rule
fullName: FIRSTNAME LASTNAME;

// Lexer tokens
FIRSTNAME: 'SIMON';
LASTNAME: 'MARCHI';

gUnit file Cool.gunit:
gunit Cool;

@header {package a.b.c;}

// The test case
fullName:
  SIMON MARCHI OK

Normally, the test case should pass, since the input is part of the
grammar. I use the ANTLR plugin inside Eclipse, and pasting the input
(SIMON MARCHI) in the interactive interpreter tells me that it is
accepted by the rule fullName. However, the test fails. If I change
from OK to FAIL, then the test passes (obviously).

Is there anything I am doing wrong here ?

Thank you,

Simon

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.