[il-antlr-interest: 31311] [antlr-interest] tree grammar and list via +=

2011-02-02 Thread Bastian Asam
Hello again,
since you helped me nicely last time a have an other question:

Is it now possible to use a list in a tree grammar? I found a mail from 
2007 saying it will be implemented soon.

But when I try it, I get a very long error Message.
Here is the grammar:

tree grammar TreeWalker;
options{
language=C;
tokenVocab=Grammar;
output=AST;
ASTLabelType=pANTLR3_BASE_TREE;
}
...
idrefs : list+=id+ ;
...

When generating the code, I get this:
error(10): internal error: /TreeWalker.g :
java.util.NoSuchElementException: template listLabelAST has no such 
attribute: elem in template context [outputFile treeParser 
genericParser(...) rule ruleBlockSingleAlt alt element 
positiveClosureBlock altSwitchCase alt element ruleRefAndListLabel invoke 
listLabelAST arg context]
...

Sounds to me like I cannot use a list with output AST, but when I set 
output to template I get:
error(10): internal error: no such group file ST.stg

output=AST would be neccessary anyway.

Any suggestions? Thanks in advance
Bastian





IMPORTANT  -  CONFIDENTIALITY  NOTICE  - This e-mail is intended only for 
the use of the individual or entity shown above as addressees. It may 
contain information which is privileged, confidential or otherwise 
protected from disclosure under applicable laws.  If the reader of this 
transmission is not the intended recipient, you are hereby notified that 
any dissemination, printing, distribution, copying, disclosure or the 
taking of any action in reliance on the contents of this information is 
strictly prohibited.  If you have received this transmission in error, 
please immediately notify us by reply e-mail or using the address below 
and delete the message and any attachments from your system. 

Amadeus Data Processing GmbH 
Geschäftsführer: Eberhard Haag 
Sitz der Gesellschaft: Erding 
HR München 48 199 
Berghamer Strasse 6 
85435 Erding 
Germany

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: 31313] Re: [antlr-interest] Memory management of C target

2011-02-02 Thread Marco Trudel
Dear Jim

On 01.02.2011 18:15, Jim Idle wrote:
 snip

 I think
 that in 3.3 I have fixed a bug that was not releasing memory references
 when building a tree until the tree was freed. Try making a version that
 does not build a tree and see how it differs.

 snip

Ok, made a version of my grammar that doesn't build a tree:

libantlr3c-3.2, no tree output: works, uses about 600mb memory
libantlr3c-3.2, tree output: crash, wants more than 2gb

libantlr3c-3.3-SNAPSHOT, no tree output: works, uses about 630mb memory
libantlr3c-3.3-SNAPSHOT, tree output: crash, wants more than 2gb

The only changes to the grammar where removing/adding output = AST;. 
Is your fix already in the current SNAPSHOT?


Thanks
Marco

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: 31314] Re: [antlr-interest] Memory management of C target

2011-02-02 Thread Jim Idle
I think that the fix is in there, which means that your input is too big
to build the tree in the way it is being done. Write an input stream
wrapper that splits the input by just returning EOF at the split point
then resets to the next unit.

How are you ending up with 640,000 lines of C input?

Jim


 -Original Message-
 From: antlr-interest-boun...@antlr.org [mailto:antlr-interest-
 boun...@antlr.org] On Behalf Of Marco Trudel
 Sent: Wednesday, February 02, 2011 8:20 AM
 To: antlr-interest@antlr.org
 Subject: Re: [antlr-interest] Memory management of C target

 Dear Jim

 On 01.02.2011 18:15, Jim Idle wrote:
  snip
 
  I think
  that in 3.3 I have fixed a bug that was not releasing memory
  references when building a tree until the tree was freed. Try making
 a
  version that does not build a tree and see how it differs.
 
  snip

 Ok, made a version of my grammar that doesn't build a tree:

 libantlr3c-3.2, no tree output: works, uses about 600mb memory
 libantlr3c-3.2, tree output: crash, wants more than 2gb

 libantlr3c-3.3-SNAPSHOT, no tree output: works, uses about 630mb memory
 libantlr3c-3.3-SNAPSHOT, tree output: crash, wants more than 2gb

 The only changes to the grammar where removing/adding output = AST;.
 Is your fix already in the current SNAPSHOT?


 Thanks
 Marco

 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: 31315] [antlr-interest] SLL(2) in The Definitive ANTLR Reference

2011-02-02 Thread Alan D. Cabrera
I was reading about the following grammar on page 287 of the PDF document

grammar t;
s : X r A B
  | Y r B
  ;

r : A
  |
  ;

I don't see where the problem is since the alternatives in s begin with two 
different tokens X and Y.  I think that since these two tokens are different I 
can easily construct a DFA that would unambiguously parse a stream of tokens.

I sense that this example was supposed to bring out a finer point about how 
ANTLR generates parsers but I'm afraid that it is being lost on me.


Regards,
Alan


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: 31316] Re: [antlr-interest] Memory management of C target

2011-02-02 Thread Marco Trudel
On 02.02.2011 18:01, Jim Idle wrote:
 I think that the fix is in there, which means that your input is too big
 to build the tree in the way it is being done. Write an input stream
 wrapper that splits the input by just returning EOF at the split point
 then resets to the next unit.

Actually I already rewrote my splitter. Now I get almost exactly the 
same performance for parsing the files splitted and as a whole.
Also the overall memory consumption is much better this way since I 
actually build an AST in another framework. I only use antlr to parse 
the input.
I just wanted to compare the performance of different approaches.
I now also tried to use the start and end pointers to the input instead 
of using $text. Doesn't make a difference in memory consumption or 
speed. So I stick with the convenient $text.

 How are you ending up with 640,000 lines of C input?

I'm doing some program analysis and preprocess the C programms with CIL 
[1]. vim and libgsl for instance then are about half a million lines of 
code.


Thanks for your time
Marco

[1] http://hal.cs.berkeley.edu/cil/


 Jim


 -Original Message-
 From: antlr-interest-boun...@antlr.org [mailto:antlr-interest-
 boun...@antlr.org] On Behalf Of Marco Trudel
 Sent: Wednesday, February 02, 2011 8:20 AM
 To: antlr-interest@antlr.org
 Subject: Re: [antlr-interest] Memory management of C target

 Dear Jim

 On 01.02.2011 18:15, Jim Idle wrote:
 snip

 I think
 that in 3.3 I have fixed a bug that was not releasing memory
 references when building a tree until the tree was freed. Try making
 a
 version that does not build a tree and see how it differs.

 snip

 Ok, made a version of my grammar that doesn't build a tree:

 libantlr3c-3.2, no tree output: works, uses about 600mb memory
 libantlr3c-3.2, tree output: crash, wants more than 2gb

 libantlr3c-3.3-SNAPSHOT, no tree output: works, uses about 630mb memory
 libantlr3c-3.3-SNAPSHOT, tree output: crash, wants more than 2gb

 The only changes to the grammar where removing/adding output = AST;.
 Is your fix already in the current SNAPSHOT?


 Thanks
 Marco

 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

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: 31317] [antlr-interest] JOB: Senior Software Engineer with Parsing Expertise needed at CISCO

2011-02-02 Thread breroger
Please contact Brent at brero...@cisco.com if you are interested in the job
opening listed below.
 
Regards,
Brent

Senior Software Engineer
 
Location ­ San Jose, California
 
Security Technology Business Unit (STBU) within WSRTG, is seeking a Software
Engineer. STBU offers network and content security solutions that enable our
customers to collaborate with confidence. These solutions include our
firewall, intrusion prevention, remote access/VPN, unified client, web and
email security.
 
Responsibilities:
Develop software for the Cisco Adaptive Security Appliance (ASA) and more
specifically for the SSL VPN Remote Access component. Program in C, C++ and
Javascript on a Linux-based platform with a slew of open source and
proprietary libraries and infrastructure. Small team is central owner of
WebVPN functionality; engage in all aspects of the product life-cycle from
specification, design to development and post release maintenance. Great
opportunity to employ a breadth of software engineering skills in a highly
visible product.
 
The successful candidate would have 3+ years of industry experience or more,
would have experience working on large projects, analyzing and debugging
Layer 7 protocols, writing parsers for high level languages and has written
javascript to generate javascript.
 
This candidate will show talent, passion and pride in software architecture,
authorship and quality, excellent collaboration skills, teamwork and cross
functional aptitude.
 
Expertise in most of the following areas is required:
 
* Flash decompilation
* Unix/POSIX/Linux application level programming
* Very strong Javascript development skills, with ability to use debuggers
and profilers
* Ability to diagnose issues with proprietary protocols
* knowledge of parsers, regular expressions
* Strong knowledge of the HTTP protocol and HTML
* NTLM, SSL, character encoding
* Differences between browsers, proxies
* Experience with tools and frameworks like wireshark, Pcap, HTTPwatch,
Spidermonkey, webkit


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: 31318] Re: [antlr-interest] SLL(2) in The Definitive ANTLR Reference

2011-02-02 Thread Terence Parr
try for rule 'r' though ;)
Ter
On Feb 2, 2011, at 9:07 AM, Alan D. Cabrera wrote:

 I was reading about the following grammar on page 287 of the PDF document
 
 grammar t;
 s : X r A B
  | Y r B
  ;
 
 r : A
  |
  ;
 
 I don't see where the problem is since the alternatives in s begin with two 
 different tokens X and Y.  I think that since these two tokens are different 
 I can easily construct a DFA that would unambiguously parse a stream of 
 tokens.
 
 I sense that this example was supposed to bring out a finer point about how 
 ANTLR generates parsers but I'm afraid that it is being lost on me.
 
 
 Regards,
 Alan

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: 31319] [antlr-interest] Question of Repetead tokens and early termination

2011-02-02 Thread Victor Giordano
Hi there. I am having trouble with the error handling.
I have a grammar for recoignize linear expression. And it works great!.
The grammar for a linear expresion is the following:

tokens
{
PLUS= '+';
MINUS   = '-';
MUL = '*';
DIV = '/';
}

linexpr : (MINUS|PLUS)? linterm ((PLUS|MINUS) linterm)*;
linterm : factor? ID;

expr returns [double value]
: e=term {$value = $e.value;}
(   PLUS e=term {$value += $e.value;}
|   MINUS e=term {$value -= $e.value;}
)*;

term returns [double value]
: f=factor {$value = $f.value;}
(   MUL f=factor {$value *= $f.value;}
|   DIV f=factor {$value /= $f.value;}
)*;

factor returns [double value]
: DOUBLE {$value = Double.parseDouble($DOUBLE.text);}
| '(' e=expr ')'{$value = $e.value;};

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

DOUBLE
:   ('0'..'9')+
|   ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
 |   '.' ('0'..'9')+ EXPONENT?
 |   ('0'..'9')+ EXPONENT
 ;

fragment EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;

NEWLINE:'\r'? '\n' { $channel = HIDDEN; };

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


But the problem ocurrs when, for example, i have:
x x x

Then the parsers stop after processing the first x.
¿How do i correctly emit an invalid syntax error?.
I Try with the catch EarlyExitException, but it doesn't works.
I Want, inside my java aplicacition to catch this, and show to the final 
user.
Something like this...
//line is equals to the user input...

 CharStream cs = new ANTLRStringStream(line);
 LinearExpressionLexer lexer = new LinearExpressionLexer(cs);
 CommonTokenStream tokens = new CommonTokenStream(lexer);
 LinearExpressionParser parser = new 
LinearExpressionParser(tokens);
 res = parser.linexpr (); // and here, it's suppose to fail, 
but it isn't.
Actually, the linexpr does returns some kind of data whose type is a 
custom class called LinearExpresion. I omit to put the return in the 
linearexpr parser rule to simplify things.

Hope anyone can help me.
Greettings and thanks for advance.
Víctor.

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: 31321] Re: [antlr-interest] Question of Repetead tokens and early termination

2011-02-02 Thread John B. Brodie
Your grammar does not mention the EOF token. (more below...)
On Wed, 2011-02-02 at 16:18 -0300, Victor Giordano wrote:
 Hi there. I am having trouble with the error handling.
 I have a grammar for recoignize linear expression. And it works great!.
 The grammar for a linear expresion is the following:
 
 tokens
 {
   PLUS= '+';
   MINUS   = '-';
   MUL = '*';
   DIV = '/';
 }
 
 linexpr : (MINUS|PLUS)? linterm ((PLUS|MINUS) linterm)*;
 linterm : factor? ID;
 
 expr returns [double value]
   : e=term {$value = $e.value;}
   (   PLUS e=term {$value += $e.value;}
   |   MINUS e=term {$value -= $e.value;}
   )*;
 
 term returns [double value]
   : f=factor {$value = $f.value;}
   (   MUL f=factor {$value *= $f.value;}
   |   DIV f=factor {$value /= $f.value;}
   )*;
 
 factor returns [double value]
   : DOUBLE {$value = Double.parseDouble($DOUBLE.text);}
   | '(' e=expr ')'{$value = $e.value;};
   
 ID  : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;
 
 DOUBLE
   :   ('0'..'9')+
   |   ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
  |   '.' ('0'..'9')+ EXPONENT?
  |   ('0'..'9')+ EXPONENT
  ;
 
 fragment EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
 
 NEWLINE:'\r'? '\n' { $channel = HIDDEN; };
 
 WS  :   (' '|'\t'|'\n'|'\r')+ { $channel = HIDDEN; };
 
 
 But the problem ocurrs when, for example, i have:
 x x x
 
 Then the parsers stop after processing the first x.
 ¿How do i correctly emit an invalid syntax error?.
 I Try with the catch EarlyExitException, but it doesn't works.
 I Want, inside my java aplicacition to catch this, and show to the final 
 user.
 Something like this...
 //line is equals to the user input...
 
  CharStream cs = new ANTLRStringStream(line);
  LinearExpressionLexer lexer = new LinearExpressionLexer(cs);
  CommonTokenStream tokens = new CommonTokenStream(lexer);
  LinearExpressionParser parser = new 
 LinearExpressionParser(tokens);
  res = parser.linexpr (); // and here, it's suppose to fail, 
 but it isn't.
 Actually, the linexpr does returns some kind of data whose type is a 
 custom class called LinearExpresion. I omit to put the return in the 
 linearexpr parser rule to simplify things.
 
 Hope anyone can help me.
 Greettings and thanks for advance.

Greetings!

By design ANTLR parsers stop after consuming the longest possible VALID
input sequence. I believe the rational for this is that any remaining
input will be available for some other tool to process.

If you want ANTLR to try to process the entire input, reporting and
recovering from syntax errors in the input; you must tell it to do that.

By referring to the EOF token (a special built-in token) in your
top-most rule will cause ANTLR to consume the entire input string. E.g.
the parse will not have a valid input until the EOF is seen and so will
consume all of the input sentence.

I suggest adding a top-level rule similar to:

start : linexpr EOF! ;

and then call parser.start() instead of parser.linexpr() in your driver.

(note the ! meta-character after the EOF token above will keep the EOF
out of any AST produced, but you do not seem to be building an AST so it
won't make any difference...)

Hope this helps...
   -jbb



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: 31322] Re: [antlr-interest] SLL(2) in The Definitive ANTLR Reference

2011-02-02 Thread Alan D. Cabrera
Ahh, the DFA for the 'r' rule.  That makes sense now.  Interesting example.

Thanks Terence and Sam!


Regards,
Alan

On Feb 2, 2011, at 11:17 AM, Terence Parr wrote:

 try for rule 'r' though ;)
 Ter
 On Feb 2, 2011, at 9:07 AM, Alan D. Cabrera wrote:
 
 I was reading about the following grammar on page 287 of the PDF document
 
 grammar t;
 s : X r A B
 | Y r B
 ;
 
 r : A
 |
 ;
 
 I don't see where the problem is since the alternatives in s begin with two 
 different tokens X and Y.  I think that since these two tokens are different 
 I can easily construct a DFA that would unambiguously parse a stream of 
 tokens.
 
 I sense that this example was supposed to bring out a finer point about how 
 ANTLR generates parsers but I'm afraid that it is being lost on me.
 
 
 Regards,
 Alan
 
 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: 31326] Re: [antlr-interest] tree pattern matching rewrite bug ?

2011-02-02 Thread Michael Bedward
Just a little more on the issue below, when I swapped the tree
grammars that were provoking the  Can't set single child to a list
error from pattern matching mode (filter = true) to complete grammars
(filter = false) the tree rewrites run successfully.

I'd still be interested to hear if issue ANTLR-419 is known to be
current problem.

cheers
Michael


On 2 February 2011 11:42, Michael Bedward michael.bedw...@gmail.com wrote:
 Hello all,

 I hope it's ok to ask about ANTLR Jira issues on this list.

 I've been using pattern matching for some simple tree rewriting tasks.
 Most work, but for some of them I keep hitting the error Can't set
 single child to a list. I've spent quite a bit of time in the
 debugger without success.

 Google found this issue for version 3.2 which appears to be related
 and still open (which I assume means not fixed in version 3.3)...

 http://www.antlr.org/jira/browse/ANTLR-419

 Before I spend more time debugging, I wonder if anyone can tell me if
 this is indeed a current bug. If so I can probably work around it by
 going back to rewriting using a complete grammar.

 cheers
 Michael


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: 31328] [antlr-interest] Catching errors

2011-02-02 Thread Victor Giordano
Okey. So adding and EOF forces the parser to go to the end of the input 
in search of others tokens in correct order.

1)But a still have a problem, consider the following grammar:

grammar LinearMath;

tokens
{
 PLUS = '+';
 MINUS = '-';
 MUL= '*';
 DIV= '/';
}

inecuation: linexpr ((RELATIONSHIP) linexpr)+ EOF!;
catch [UnwantedTokenException ute]
{
System.out.println (inecuation UnwantedTokenException   + 
ute.toString());
throw ute;
}

linexpr : (MINUS|PLUS)? linterm ((PLUS|MINUS) linterm)* EOF;

linterm : factor? ID;

expr returns [double value]
 : e=term {$value = $e.value;}
 (PLUS e=term {$value += $e.value;}
 |MINUS e=term {$value -= $e.value;}
 )*;

term returns [double value]
 : f=factor {$value = $f.value;}
 (MUL f=factor {$value *= $f.value;}
 |DIV f=factor {$value /= $f.value;}
 )*;

factor returns [double value]
 : DOUBLE {$value = Double.parseDouble($DOUBLE.text);}
 | '(' e=expr ')'{$value = $e.value;};

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

DOUBLE
 :   ('0'..'9')+
 |('0'..'9')+ '.' ('0'..'9')* EXPONENT?
   |   '.' ('0'..'9')+ EXPONENT?
   |   ('0'..'9')+ EXPONENT
   ;

fragment EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;

NEWLINE:'\r'? '\n' { $channel = HIDDEN; };

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


RELATIONSHIP :  ''|'='|'='|''|'=';

and with the following input: x  y x
that isn't a valid inecuation beacause the y x must have a binary 
aritmetic operator (PLUS OR MINUS). The parser do his job very well, he 
consume the x then  later y and when it reachs the seconds x it 
emits an UnwantedTokenException. The think is, that i am not being 
able to catch it, and display an error to the final user. Look that i am 
using to parse that input the inecuation rule.

Hope anyone can help me with this again.

2) Other thing is about invalid tokens, i manage to treat then 
overriding a member function of the lexer called nextToken(), like this:

@lexer::members
{
@Override
public Token nextToken()
{
while (true) {
state.token = null;
state.channel = Token.DEFAULT_CHANNEL;
state.tokenStartCharIndex = input.index();
state.tokenStartCharPositionInLine = 
input.getCharPositionInLine();
state.tokenStartLine = input.getLine();
state.text = null;
if ( input.LA(1)==CharStream.EOF ) {
return Token.EOF_TOKEN;
}
try {
mTokens();
if ( state.token==null ) {
emit();
}
else if ( state.token==Token.SKIP_TOKEN ) {
continue;
}
return state.token;
}
catch (RecognitionException re) {
reportError(re);
throw new RuntimeException(Invalid Character  
:  + (char) (re.c)); 
// or throw Error
}
}
}
}

¿It's that the correct way?

Well that is all!!!
Thanks for advance!.
Victor!!

El 02/02/2011 05:32 p.m., John B. Brodie escribió:
 Your grammar does not mention the EOF token. (more below...)
 On Wed, 2011-02-02 at 16:18 -0300, Victor Giordano wrote:
 Hi there. I am having trouble with the error handling.
 I have a grammar for recoignize linear expression. And it works great!.
 The grammar for a linear expresion is the following:

 tokens
 {
  PLUS= '+';
  MINUS   = '-';
  MUL = '*';
  DIV = '/';
 }

 linexpr : (MINUS|PLUS)? linterm ((PLUS|MINUS) linterm)*;
 linterm : factor? ID;

 expr returns [double value]
  : e=term {$value = $e.value;}
  (   PLUS e=term {$value += $e.value;}
  |   MINUS e=term {$value -= $e.value;}
  )*;

 term returns [double value]
  : f=factor {$value = $f.value;}
  (   MUL f=factor {$value *= $f.value;}
  |   DIV f=factor {$value /= $f.value;}
  )*;

 factor returns [double value]
  : DOUBLE {$value = Double.parseDouble($DOUBLE.text);}
  | '(' e=expr ')'{$value = $e.value;};
  
 ID  :('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;

 DOUBLE
  :   ('0'..'9')+
  |   ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
   |   '.' ('0'..'9')+ EXPONENT?
   |   ('0'..'9')+ EXPONENT
   ;

 fragment EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;

 NEWLINE:'\r'? '\n' { $channel = HIDDEN; };

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


 But the problem ocurrs when, for example, 

[il-antlr-interest: 31330] Re: [antlr-interest] Which approach for an Interpreter: Tree Grammar vs AST-Visitor

2011-02-02 Thread Hiran Chaudhuri
So would it be better to have an example like the Pie language use a
tree grammar or is the hand-written visitor code a better approach? What
are the pros and cons? Any help appreciated.

A pure interpreter would read statements one by one and execute them directly.
A compiler would read the whole source in one chunk and translate it before 
execution.
You can take any approach inbetween as well.

That said, I believe parsing the whole file and translating into an AST is not 
really interpretation.
But there is still the approach to parse statement for statement, and here I 
would definitely separate the grammar from execution code through an AST.

file: statement+ EOF;
statement: .

My interpreter would open the file, construct parser and lexers, manage 
variables and scope, repeatedly ask the parser to return the AST for the next 
statement and send it for execution.
This way you would get the typical behaviour that syntax errors or else are 
detected during execution time. And if you were to change your mind towards a 
compiler, the grammar and AST construction would not change as you simply call 
the file method on the parser.

Of course it is more intersting if your interpreted language allows 
methods/procedures/functions as then parsing the source file would not be 
linear.

Hiran
___
Neu: WEB.DE De-Mail - Einfach wie E-Mail, sicher wie ein Brief!  
Jetzt De-Mail-Adresse reservieren: https://produkte.web.de/go/demail02

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.